Skip to content

Commit 1205d9c

Browse files
We now make use of the @nestjs/passport package so we can use AuthGuard for invoking the JWT strategy at route level - perfect for graphql resolvers, for example
1 parent e3a09d7 commit 1205d9c

File tree

5 files changed

+14
-22
lines changed

5 files changed

+14
-22
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"@nestjs/core": "5.0.1",
9090
"@nestjs/graphql": "2.0.0",
9191
"@nestjs/microservices": "5.0.1",
92+
"@nestjs/passport": "1.1.0",
9293
"@nestjs/testing": "5.0.1",
9394
"@nestjs/websockets": "5.0.1",
9495
"@nguniversal/express-engine": "6.0.0",

src/server/modules/auth/auth.controller.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Controller, Post, Get, Req } from '@nestjs/common';
1+
import { Controller, Post, Get, Req, UseGuards } from '@nestjs/common';
22
import { AuthService } from './auth.service';
33
import { IToken } from './interfaces/token.interface';
44
import { Request } from 'express';
5+
import { AuthGuard } from '@nestjs/passport';
56

67
@Controller('api/auth')
78
export class AuthController {
@@ -63,6 +64,7 @@ export class AuthController {
6364
}
6465

6566
@Get('authorized')
67+
@UseGuards(AuthGuard('jwt'))
6668
public async authorized() {
6769
console.log('Authorized route...');
6870
}

src/server/modules/auth/auth.module.ts

-4
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,5 @@ export class AuthModule implements NestModule {
6060
consumer
6161
.apply(authenticate('google', { session: false }))
6262
.forRoutes('api/auth/google/token');
63-
64-
consumer
65-
.apply(authenticate('jwt', { session: false }))
66-
.forRoutes('api/auth/authorized');
6763
}
6864
}

src/server/modules/auth/passport/jwt.strategy.ts

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
import { Injectable, UnauthorizedException } from '@nestjs/common';
2-
import { Request } from 'express';
3-
import { use } from 'passport';
2+
import { PassportStrategy } from '@nestjs/passport';
43
import { ExtractJwt, Strategy } from 'passport-jwt';
54

65
import { AuthService } from '../auth.service';
7-
import { SERVER_CONFIG, MESSAGES } from '../../../server.constants';
6+
import { SERVER_CONFIG } from '../../../server.constants';
87
import { IUser } from '../../user/interfaces/user.interface';
98
import { IJwtPayload } from '../interfaces/jwt-payload.interface';
109

1110
@Injectable()
12-
export class JwtStrategy extends Strategy {
11+
export class JwtStrategy extends PassportStrategy(Strategy) {
1312
constructor(private readonly authService: AuthService) {
14-
super(
15-
{
16-
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
17-
passReqToCallback: true,
18-
secretOrKey: SERVER_CONFIG.jwtSecret,
19-
},
20-
async (req: Request, payload: IJwtPayload, next: Function) =>
21-
await this.verify(payload, next)
22-
);
23-
use('jwt', this);
13+
super({
14+
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
15+
secretOrKey: SERVER_CONFIG.jwtSecret
16+
});
2417
}
2518

26-
public async verify(payload: IJwtPayload, done: Function) {
19+
public async validate(payload: IJwtPayload, done: Function) {
2720
const user: IUser = await this.authService.findUserById(payload.sub);
2821

2922
if (!user) {
30-
return done(new UnauthorizedException(MESSAGES.UNAUTHORIZED_UNRECOGNIZED_BEARER), false);
23+
return done(new UnauthorizedException(), false);
3124
}
3225

3326
done(null, user);

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"noImplicitAny": true,
77
"removeComments": true,
88
"sourceMap": true,
9-
"target": "es5",
9+
"target": "es6",
1010
"module": "commonjs",
1111
"moduleResolution": "node",
1212
"typeRoots": [

0 commit comments

Comments
 (0)