-
Notifications
You must be signed in to change notification settings - Fork 17
fix(auth): update auth to use localStorage #540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a8a8fa1
6b11ad2
c37d0e7
127ac2c
4e98fb0
4271b68
3dab1ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,12 @@ | ||
| import bodyParser from 'body-parser'; | ||
| import connectRedis, { RedisStore } from 'connect-redis'; | ||
| import cors from 'cors'; | ||
| import express from 'express'; | ||
| import session from 'express-session'; | ||
| import passport from 'passport'; | ||
| import redis from 'redis'; | ||
| import { | ||
| cookieExpiry, | ||
| redisConfiguration, | ||
| sessionSecret, | ||
| allowedOrigins, | ||
| } from '../config/index'; | ||
| import AuthController from './controllers/auth'; | ||
| import Controller from './controllers/base'; | ||
| import GistController from './controllers/gist'; | ||
| import HomeController from './controllers/home'; | ||
|
|
||
| /** | ||
|
|
@@ -24,67 +17,28 @@ class App { | |
| * Stores the constructor of Express application. | ||
| */ | ||
| public app: express.Application; | ||
| public redisClient: redis.RedisClient; | ||
| public redisStore: RedisStore; | ||
|
|
||
| /** | ||
| * Constructor to initialize application. | ||
| */ | ||
| constructor() { | ||
| this.app = express(); | ||
| this.redisClient = redis.createClient( | ||
| redisConfiguration.REDIS_PORT, | ||
| redisConfiguration.REDIS_HOST | ||
| ); | ||
| if (redisConfiguration.REDIS_PASSWORD) { | ||
| this.redisClient.auth(redisConfiguration.REDIS_PASSWORD, error => { | ||
| console.error(error); | ||
| }); | ||
| } | ||
| this.redisStore = connectRedis(session); | ||
| this.initializeMiddleWares(); | ||
| this.initializeControllers([ | ||
| new AuthController(), | ||
| new HomeController(), | ||
| new GistController(), | ||
| new HomeController() | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Initializes middleware for accessing request and response objects. | ||
| */ | ||
| private initializeMiddleWares() { | ||
| // Configuration for creating session cookies. | ||
| const redisStore = this.redisStore; | ||
| this.app.use( | ||
| session({ | ||
| name: 'vega_session', | ||
| secret: sessionSecret, | ||
| resave: false, | ||
| saveUninitialized: false, | ||
| store: new redisStore({ | ||
| host: redisConfiguration.REDIS_HOST, | ||
| port: redisConfiguration.REDIS_PORT, | ||
| client: this.redisClient, | ||
| ttl: cookieExpiry, | ||
| }), | ||
| /** | ||
| * `cookieExpiry` is converted to milliseconds. Reference: | ||
| * https://www.npmjs.com/package/express-session#cookiemaxage | ||
| */ | ||
| cookie: { | ||
| maxAge: cookieExpiry * 1000, | ||
| secure: process.env.NODE_ENV === 'production', | ||
| sameSite: 'none', | ||
| }, | ||
| rolling: true, | ||
| }) | ||
| ); | ||
| this.app.use(bodyParser.json()); | ||
|
|
||
| const corsOptions = { | ||
| origin: (origin, callback) => { | ||
| if (!origin || allowedOrigins.includes(origin)) { | ||
| if (!origin || origin === 'null' || allowedOrigins.includes(origin)) { | ||
| callback(null, true); | ||
| } else { | ||
| callback(new Error('Not allowed by CORS')); | ||
|
|
@@ -94,7 +48,28 @@ class App { | |
| }; | ||
| this.app.use(cors(corsOptions)); | ||
| this.app.use(passport.initialize()); | ||
| this.app.use(passport.session()); | ||
|
|
||
| // Handle preflight OPTIONS requests explicitly (required for CORS) | ||
| this.app.options('*', (req, res) => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to handle preflight? Is that needed for something in auth?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I was getting a bunch of CORS issues without them
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see let's say in the comment that it's needed for cors |
||
| const origin = req.headers.origin || '*'; | ||
|
|
||
| if (origin === 'null') { | ||
| res.header('Access-Control-Allow-Origin', 'null'); | ||
| } else { | ||
| res.header('Access-Control-Allow-Origin', origin); | ||
| } | ||
|
|
||
| // Needed to handle CORS preflight requests. | ||
| // i.e. tell browsers which origins, methods, are allowed. | ||
|
|
||
|
|
||
| res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need these? |
||
| res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Auth-Token, Cache-Control, Pragma, Expires'); | ||
| // Without this, browsers may block cross-origin requests, especially when credentials are involved. | ||
| res.header('Access-Control-Allow-Credentials', 'true'); | ||
| res.header('Access-Control-Expose-Headers', 'Content-Type, Authorization, X-Auth-Token'); | ||
| res.status(200).end(); | ||
| }); | ||
|
|
||
| // Put IP of https://vega.github.io/editor instead of 1 | ||
| this.app.set('trust proxy', 1); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the dependency then