Skip to content

Commit

Permalink
feat: use redis for user session cookies (#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmwang authored Jun 24, 2024
1 parent 3b5ee90 commit 7e730ef
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
12 changes: 12 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@graphql-tools/schema": "^10.0.0",
"@graphql-tools/utils": "^10.0.7",
"axios": "^1.5.1",
"connect-redis": "^7.1.1",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.18.2",
Expand Down
5 changes: 3 additions & 2 deletions backend/src/bootstrap/loaders/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import cors from "cors";
import helmet from "helmet";
import type { ApolloServer } from "@apollo/server";
import { expressMiddleware } from "@apollo/server/express4";
import { RedisClientType } from "redis";

import passportLoader from "./passport";
import { config } from "../../config";

export default async (app: Application, server: ApolloServer) => {
export default async (app: Application, server: ApolloServer, redis: RedisClientType) => {
// Body parser only needed during POST on the graphQL path
app.use(json());

Expand All @@ -24,7 +25,7 @@ export default async (app: Application, server: ApolloServer) => {
app.use(helmet());

// load authentication
passportLoader(app);
passportLoader(app, redis);

app.use(
config.graphqlPath,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/bootstrap/loaders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default async (root: Application): Promise<void> => {

// load everything related to express. depends on apollo
console.log("Loading express...");
await expressLoader(app, server);
await expressLoader(app, server, redis);

// append backend path to all routes
root.use(config.backendPath, app);
Expand Down
10 changes: 9 additions & 1 deletion backend/src/bootstrap/loaders/passport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import passport from "passport";
import GoogleStrategy from "passport-google-oauth20";
import { UserModel } from "../../models/user";
import { config } from "../../config";
import type { RedisClientType } from "redis";
import RedisStore from "connect-redis";

const LOGIN_ROUTE = "/login";
const LOGIN_REDIRECT_ROUTE = "/login/redirect";
Expand All @@ -24,7 +26,9 @@ const FAILURE_REDIRECT = config.backendPath + "/fail";

const SCOPE = ['profile', 'email']

export default async (app: Application) => {
const CACHE_PREFIX = 'user-session:'

export default async (app: Application, redis: RedisClientType) => {
// init
app.use(session({
secret: config.SESSION_SECRET,
Expand All @@ -37,6 +41,10 @@ export default async (app: Application) => {
maxAge: 1000 * 60 * 60, // 1 hour
sameSite: 'lax',
},
store: new RedisStore({
client: redis,
prefix: CACHE_PREFIX,
}),
rolling: true,
}));
app.use(passport.initialize());
Expand Down

0 comments on commit 7e730ef

Please sign in to comment.