-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7360a61
commit 7bdb651
Showing
4 changed files
with
34 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
mycarvalue/src/users/middlewares/current-user.middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('*'); | ||
} | ||
} |