Skip to content

Commit

Permalink
Increase session cookie max age (#384)
Browse files Browse the repository at this point in the history
* Increase session length to 30 days

* fix: set session length on user cookie
  • Loading branch information
js0mmer authored Dec 10, 2023
1 parent 360c131 commit 4aeed3d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
4 changes: 3 additions & 1 deletion api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import graphqlRouter from './controllers/graphql';
import roadmapRouter from './controllers/roadmap';
import reportsRouter from './controllers/reports';

import { SESSION_LENGTH } from './config/constants';

// instantiate app
const app = express();

Expand All @@ -49,7 +51,7 @@ if (process.env.MONGO_URL) {
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: { maxAge: 1000 * 60 * 60 * 24 },
cookie: { maxAge: SESSION_LENGTH },
store: store,
}));
app.use(passport.initialize());
Expand Down
1 change: 1 addition & 0 deletions api/src/config/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SESSION_LENGTH = 30 * 86400 * 1000;
5 changes: 4 additions & 1 deletion api/src/controllers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import express, { Request, Response } from 'express';
import passport from 'passport';
import { SESSION_LENGTH } from '../config/constants';

let router = express.Router();

Expand Down Expand Up @@ -158,7 +159,9 @@ router.get('/auth/github/callback',
function successLogin(req: Request, res: Response) {
console.log('Logged in', req.user);
// set the user cookie
res.cookie('user', req.user);
res.cookie('user', req.user, {
maxAge: SESSION_LENGTH
});
// redirect browser to the page they came from
let returnTo = req.session.returnTo ?? '/';
delete req.session.returnTo;
Expand Down

0 comments on commit 4aeed3d

Please sign in to comment.