Skip to content

Commit b074b18

Browse files
committed
refresh endpoint
1 parent 9c3e5e9 commit b074b18

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed

src/auth/controllers/auth.controller.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Controller, Get, Post, Request, UseGuards } from '@nestjs/common';
1+
import { Controller, Get, Post, Req, Request, UseGuards } from '@nestjs/common';
22
import { ApiBearerAuth, ApiBody, ApiTags } from '@nestjs/swagger';
33
import { User } from '../../users/entities/user.entity';
44
import { LoginDto } from '../dto/login.dto';
55
import { JwtAuthGuard } from '../guards/jwt-auth.guard';
6+
import JwtRefreshGuard from '../guards/jwt-refresh.guard';
67
import { LocalAuthGuard } from '../guards/local-auth.guard';
78
import { AuthService } from '../services/auth.service';
89

@@ -25,4 +26,10 @@ export class AuthController {
2526
async logOut(@Request() req: { user: User }) {
2627
await this.authService.logout(req.user);
2728
}
29+
30+
@UseGuards(JwtRefreshGuard)
31+
@Get('refresh')
32+
refresh(@Req() request: { user: User }) {
33+
return this.authService.jwtRefreshToken(request.user);
34+
}
2835
}

src/auth/guards/jwt-refresh.guard.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { AuthGuard } from '@nestjs/passport';
3+
4+
@Injectable()
5+
export default class JwtRefreshGuard extends AuthGuard('jwt-refresh-token') {}

src/auth/services/auth.service.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export class AuthService {
4646
accessToken,
4747
user: userData,
4848
cookie,
49+
refreshToken,
4950
};
5051
}
5152

@@ -60,10 +61,6 @@ export class AuthService {
6061
jwtRefreshToken(user: User) {
6162
const payload: PayloadToken = { role: user.role, id: user.id };
6263

63-
console.log(
64-
this.configService.get('JWT_REFRESH_SECRET'),
65-
this.configService.get('REFRESH_TOKEN_EXPIRATION'),
66-
);
6764
const refreshToken = this.jwtService.sign(payload, {
6865
secret: this.configService.get('JWT_REFRESH_SECRET'),
6966
expiresIn: `${this.configService.get('REFRESH_TOKEN_EXPIRATION')}`,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { ConfigService } from '@nestjs/config';
3+
import { PassportStrategy } from '@nestjs/passport';
4+
import { Request } from 'express';
5+
import { ExtractJwt, Strategy } from 'passport-jwt';
6+
import { UsersService } from '../../users/services/users.service';
7+
import { PayloadToken } from '../models/token.model';
8+
9+
@Injectable()
10+
export class JwtRefreshTokenStrategy extends PassportStrategy(
11+
Strategy,
12+
'jwt-refresh-token',
13+
) {
14+
constructor(
15+
private readonly configService: ConfigService,
16+
private readonly userService: UsersService,
17+
) {
18+
super({
19+
jwtFromRequest: ExtractJwt.fromExtractors([
20+
(request: Request) => {
21+
return request?.cookies?.Refresh;
22+
},
23+
]),
24+
secretOrKey: configService.get('JWT_REFRESH_TOKEN_SECRET'),
25+
passReqToCallback: true,
26+
});
27+
}
28+
29+
async validate(request: Request, payload: PayloadToken) {
30+
const refreshToken = request.cookies?.Refresh;
31+
return this.userService.getUserIfRefreshTokenMatches(
32+
refreshToken,
33+
payload.id,
34+
);
35+
}
36+
}

src/users/services/users.service.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export class UsersService {
5555

5656
async setCurrentRefreshToken(refreshToken: string, userId: number) {
5757
const currentHashedRefreshToken = await bcrypt.hash(refreshToken, 10);
58-
console.log(currentHashedRefreshToken, userId);
5958
return await this.userRepository.update(userId, {
6059
refreshToken: currentHashedRefreshToken,
6160
});
@@ -71,4 +70,17 @@ export class UsersService {
7170
},
7271
);
7372
}
73+
74+
async getUserIfRefreshTokenMatches(refreshToken: string, userId: number) {
75+
const user = await this.findById(userId);
76+
77+
const isRefreshTokenMatching = await bcrypt.compare(
78+
refreshToken,
79+
user.refreshToken,
80+
);
81+
82+
if (isRefreshTokenMatching) {
83+
return user;
84+
}
85+
}
7486
}

0 commit comments

Comments
 (0)