Skip to content

Commit

Permalink
fix: redis connect failed (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppodds committed Jun 22, 2023
2 parents 55fdd6d + c91947b commit 84d1628
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 51 deletions.
3 changes: 1 addition & 2 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
"passport-oauth2": "^1.7.0",
"redis": "^3.1.2",
"redis": "^4.6.7",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"typeorm": "^0.3.17"
Expand All @@ -57,7 +57,6 @@
"@nestjs/cli": "^10.0.3",
"@nestjs/schematics": "^10.0.1",
"@nestjs/testing": "^10.0.3",
"@types/connect-redis": "^0.0.20",
"@types/cookie-parser": "^1.4.3",
"@types/csurf": "^1.11.2",
"@types/express": "^4.17.17",
Expand Down
13 changes: 8 additions & 5 deletions apps/backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { createClient } from 'redis';
import * as connectRedis from 'connect-redis';
import RedisStore from 'connect-redis';
import helmet from 'helmet';
import * as session from 'express-session';
import * as passport from 'passport';
Expand Down Expand Up @@ -33,11 +33,14 @@ async function bootstrap() {
saveUninitialized: false,
};
if (process.env.NODE_ENV === 'production') {
const redisClient = createClient({
url: process.env.REDIS_URL,
});
try {
const RedisStore = connectRedis(session);
const redisClient = await createClient({
url: process.env.REDIS_URL,
});
const redisStore = new RedisStore({
client: redisClient,
prefix: 'session:',
});
sessionOptions.store = new RedisStore({ client: redisClient });
} catch (e) {
Logger.error('Unable to connect to Redis');
Expand Down
115 changes: 71 additions & 44 deletions pnpm-lock.yaml

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

0 comments on commit 84d1628

Please sign in to comment.