Skip to content

Commit

Permalink
middle added and fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Syed007Hassan committed Jul 19, 2023
1 parent 7360a61 commit 7bdb651
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
14 changes: 6 additions & 8 deletions mycarvalue/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ReportsModule } from './reports/reports.module';
import { User } from './users/user.entity';
import { Report } from './reports/report.entity';
import { ConfigModule, ConfigService } from '@nestjs/config';
import cookieSession from 'cookie-session';
const cookieSession = require('cookie-session');

@Module({
imports: [
Expand All @@ -33,12 +33,10 @@ import cookieSession from 'cookie-session';
})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(
cookieSession({
keys: ['asdfasdf'],
}),
)
.forRoutes('*');
consumer.apply(
cookieSession({
keys: ['mysecret'],
}),
);
}
}
2 changes: 1 addition & 1 deletion mycarvalue/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function bootstrap() {

// app.use(cookieParser());

app.use(cookieSession({ keys: ['mysecret'] }));
// app.use(cookieSession({ keys: ['mysecret'] }));

app.useGlobalPipes(
new ValidationPipe({
Expand Down
18 changes: 18 additions & 0 deletions mycarvalue/src/users/middlewares/current-user.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { UsersService } from '../users.service';

@Injectable()
export class CurrentUserMiddleware implements NestMiddleware {
constructor(private userService: UsersService) {}

async use(req: Request, res: Response, next: NextFunction) {
const { userId } = req.session || {};
if (userId) {
const user = await this.userService.findOne(userId);
// @ts-ignore
req.currentUser = user;
}
next();
}
}
23 changes: 9 additions & 14 deletions mycarvalue/src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { Module } from '@nestjs/common';
import { Module, MiddlewareConsumer } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
import { User } from './user.entity';
import { AuthService } from './auth.service';
import { CurrentUserInterceptor } from './interceptors/current-user-intetrceptor';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { CurrentUserMiddleware } from './middlewares/current-user.middleware';

@Module({
imports: [TypeOrmModule.forFeature([User])],
controllers: [UsersController],
providers: [
UsersService,
AuthService,
CurrentUserInterceptor,
//Globally scoped interceptor
{
provide: APP_INTERCEPTOR,
useClass: CurrentUserInterceptor,
},
],
providers: [UsersService, AuthService],
})
export class UsersModule {}
export class UsersModule {
// This is a middleware that will be applied to all routes
configure(consumer: MiddlewareConsumer) {
consumer.apply(CurrentUserMiddleware).forRoutes('*');
}
}

0 comments on commit 7bdb651

Please sign in to comment.