-
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.
Merge pull request #17 from Brints/refresh-tokens
feat: implemented refresh tokens
- Loading branch information
Showing
10 changed files
with
152 additions
and
37 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { IsNotEmpty, IsString } from 'class-validator'; | ||
|
||
export class RefreshTokenDto { | ||
@IsNotEmpty() | ||
@IsString() | ||
refresh_token: string; | ||
} |
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,6 @@ | ||
POST http://localhost:3001/auth/refresh-tokens | ||
Content-Type: application/json | ||
|
||
{ | ||
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJlZjI2N2IxMS02NmYzLTQ3MjAtYjM1MS01ZjlkNDc0OGQ3M2YiLCJpYXQiOjE3MjU0NjE2MjIsImV4cCI6MTcyNTU0ODAyMiwiYXVkIjoibG9jYWxob3N0OjMwMDEiLCJpc3MiOiJsb2NhbGhvc3Q6MzAwMSJ9.RFAQdr2rIZlmESj7ayjqRr6g0DPruQaSUBFHi0Z0_gk" | ||
} |
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
54 changes: 54 additions & 0 deletions
54
brints-estate-api/src/auth/providers/generate-tokens.provider.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,54 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import jwtConfig from '../config/jwt.config'; | ||
import { ConfigType } from '@nestjs/config'; | ||
import { User } from 'src/users/entities/user.entity'; | ||
import { IActiveUser } from '../interfaces/active-user.interface'; | ||
|
||
@Injectable() | ||
export class GenerateTokensProvider { | ||
constructor( | ||
private readonly jwtService: JwtService, | ||
|
||
@Inject(jwtConfig.KEY) | ||
private readonly jwtConfiguration: ConfigType<typeof jwtConfig>, | ||
) {} | ||
|
||
public async signToken<T>(userId: string, expiresIn: number, payload?: T) { | ||
const data = { | ||
sub: userId, | ||
...payload, | ||
}; | ||
|
||
return await this.jwtService.signAsync(data, { | ||
secret: this.jwtConfiguration.secret, | ||
expiresIn, | ||
audience: this.jwtConfiguration.audience, | ||
issuer: this.jwtConfiguration.issuer, | ||
}); | ||
} | ||
|
||
public async generateTokens(user: User) { | ||
const [access_token, refresh_token] = await Promise.all([ | ||
this.signToken<Partial<IActiveUser>>( | ||
user.id, | ||
this.jwtConfiguration.expiresIn, | ||
{ | ||
first_name: user.first_name, | ||
last_name: user.last_name, | ||
email: user.email, | ||
phone_number: user.phone_number, | ||
role: user.role, | ||
verified: user.isVerified, | ||
}, | ||
), | ||
|
||
this.signToken<Partial<IActiveUser>>( | ||
user.id, | ||
this.jwtConfiguration.refresh_token_expires, | ||
), | ||
]); | ||
|
||
return { access_token, refresh_token }; | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
brints-estate-api/src/auth/providers/refresh-tokens.provider.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,46 @@ | ||
import { HttpStatus, Inject, Injectable } from '@nestjs/common'; | ||
import { RefreshTokenDto } from '../dto/refresh-token.dto'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { ConfigType } from '@nestjs/config'; | ||
import jwtConfig from '../config/jwt.config'; | ||
import { Repository } from 'typeorm'; | ||
import { User } from 'src/users/entities/user.entity'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { GenerateTokensProvider } from './generate-tokens.provider'; | ||
import { CustomException } from 'src/exceptions/custom.exception'; | ||
import { IActiveUser } from '../interfaces/active-user.interface'; | ||
|
||
@Injectable() | ||
export class RefreshTokensProvider { | ||
constructor( | ||
private readonly jwtService: JwtService, | ||
|
||
@Inject(jwtConfig.KEY) | ||
private readonly jwtConfiguration: ConfigType<typeof jwtConfig>, | ||
|
||
@InjectRepository(User) | ||
private readonly userRepository: Repository<User>, | ||
|
||
private readonly generateTokensProvider: GenerateTokensProvider, | ||
) {} | ||
|
||
public async refreshTokens(refreshTokenDto: RefreshTokenDto) { | ||
const { sub } = await this.jwtService.verifyAsync<Pick<IActiveUser, 'sub'>>( | ||
refreshTokenDto.refresh_token, | ||
{ | ||
secret: this.jwtConfiguration.secret, | ||
audience: this.jwtConfiguration.audience, | ||
issuer: this.jwtConfiguration.issuer, | ||
}, | ||
); | ||
|
||
const user = await this.userRepository.findOneBy({ id: sub }); | ||
// const user = await this.userRepository.findOne(sub); | ||
|
||
if (!user) { | ||
throw new CustomException(HttpStatus.NOT_FOUND, 'User not found'); | ||
} | ||
|
||
return await this.generateTokensProvider.generateTokens(user); | ||
} | ||
} |
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