Ride-The-Lightning-RTL/server/utils/csrf.ts

24 lines
803 B
TypeScript
Raw Normal View History

2021-12-30 00:08:41 +01:00
import csurf from 'csurf/index.js';
import { Application } from 'express';
import { Logger, LoggerService } from './logger.js';
import { Common, CommonService } from './common.js';
2021-12-30 00:08:41 +01:00
class CSRF {
public csrfProtection = csurf({ cookie: true });
public logger: LoggerService = Logger;
public common: CommonService = Common;
2021-12-30 00:08:41 +01:00
public mount(app: Application): Application {
this.logger.log({ selectedNode: this.common.initSelectedNode, level: 'INFO', fileName: 'CSRF', msg: 'Setting up CSRF..' });
2021-12-30 00:08:41 +01:00
if (process.env.NODE_ENV !== 'development') {
app.use((req, res, next) => this.csrfProtection(req, res, next));
}
this.logger.log({ selectedNode: this.common.initSelectedNode, level: 'INFO', fileName: 'CSRF', msg: 'CSRF Set' });
2021-12-30 00:08:41 +01:00
return app;
};
}
export default new CSRF;