Skip to content

Commit

Permalink
fix(socials): resolve redis session issue (#112)
Browse files Browse the repository at this point in the history
* use redis as a store for sessions

* fix path for tests

* fix tests
  • Loading branch information
adelinaenache authored and hsb1007 committed Jun 13, 2024
1 parent a7703dc commit 3ee33d0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"cheerio": "1.0.0-rc.12",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"connect-redis": "^7.1.1",
"crypto-js": "^4.2.0",
"expo-server-sdk": "^3.10.0",
"express": "^4.19.2",
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

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

20 changes: 18 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { Inject, MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { AppController } from './app.controller';
import { AuthModule } from '../auth/auth.module';
import { UserModule } from '../user/user.module';
Expand All @@ -14,6 +14,10 @@ import { ConnectionsModule } from '../../src/connections/connections.module';
import { AppLoggerMiddleware } from '../../src/middlewares/logger.middleware';
import { CommonModule } from '../common/common.module';
import { NotificationsModule } from '../../src/notifications/notifications.module';
import RedisStore from 'connect-redis';
import * as session from 'express-session';
import Redis from 'ioredis';
import { REDIS_CLIENT } from '../../src/provider/redis.provider';

@Module({
imports: [
Expand All @@ -40,7 +44,19 @@ import { NotificationsModule } from '../../src/notifications/notifications.modul
],
})
export class AppModule implements NestModule {
constructor(@Inject(REDIS_CLIENT) private redis: Redis) {}

configure(consumer: MiddlewareConsumer): void {
consumer.apply(AppLoggerMiddleware).forRoutes('*');
consumer
.apply(
AppLoggerMiddleware,
session({
secret: process.env.SESSION_SECRET || 'lala',
resave: false,
saveUninitialized: false,
store: new RedisStore({ client: this.redis }),
}),
)
.forRoutes('*');
}
}
4 changes: 2 additions & 2 deletions src/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ export class AuthService {
}

async handleAppleOAuthLogin(req: any) {
const { email, name } = req.user;
const displayName = name.firstName + ' ' + name.lastName;
const { email, displayName } = req.user;
const user = await this.createUserIfNotExists(
email,
AuthType.APPLE,
displayName,
null,
false,
);

const token = await this.generateToken(user);
Expand Down
2 changes: 0 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { AppModule } from './app/app.module';
import { ValidationPipe } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { json } from 'express';
import * as session from 'express-session';

function initializeSwagger(app: any) {
const config = new DocumentBuilder()
Expand All @@ -19,7 +18,6 @@ function initializeSwagger(app: any) {
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const globalPrefix = 'api';
app.use(session({ secret: process.env.SESSION_SECRET }));

app.setGlobalPrefix(globalPrefix);
app.enableCors();
Expand Down

0 comments on commit 3ee33d0

Please sign in to comment.