-
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
Showing
7 changed files
with
110 additions
and
10 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
38 changes: 38 additions & 0 deletions
38
api/src/modules/auth/strategies/reset-password.strategy.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,38 @@ | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { UsersService } from '@api/modules/users/users.service'; | ||
import { ApiConfigService } from '@api/modules/config/app-config.service'; | ||
import { TOKEN_TYPE_ENUM } from '@shared/schemas/auth/token-type.schema'; | ||
|
||
export type JwtPayload = { id: string }; | ||
|
||
const ResetPasswordStrategyName = 'reset-password'; | ||
|
||
@Injectable() | ||
export class ResetPasswordJwtStrategy extends PassportStrategy( | ||
Strategy, | ||
ResetPasswordStrategyName, | ||
) { | ||
constructor( | ||
private readonly userService: UsersService, | ||
private readonly config: ApiConfigService, | ||
) { | ||
const { secret } = config.getJWTConfigByType( | ||
TOKEN_TYPE_ENUM.RESET_PASSWORD, | ||
); | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: secret, | ||
}); | ||
} | ||
|
||
async validate(payload: JwtPayload) { | ||
const { id } = payload; | ||
const user = await this.userService.findOneBy(id); | ||
if (!user) { | ||
throw new UnauthorizedException(); | ||
} | ||
return 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
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,42 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { TOKEN_TYPE_ENUM } from '@shared/schemas/auth/token-type.schema'; | ||
|
||
@Injectable() | ||
export class JwtConfigHandler { | ||
constructor(private readonly configService: ConfigService) {} | ||
|
||
getJwtConfigByType(tokenType: TOKEN_TYPE_ENUM): { | ||
secret: string; | ||
expiresIn: string; | ||
} { | ||
switch (tokenType) { | ||
case TOKEN_TYPE_ENUM.ACCESS: | ||
return { | ||
secret: this.configService.get<string>('ACCESS_TOKEN_SECRET'), | ||
expiresIn: this.configService.get<string>('ACCESS_TOKEN_EXPIRES_IN'), | ||
}; | ||
|
||
case TOKEN_TYPE_ENUM.RESET_PASSWORD: | ||
return { | ||
secret: this.configService.get<string>('RESET_PASSWORD_TOKEN_SECRET'), | ||
expiresIn: this.configService.get<string>( | ||
'RESET_PASSWORD_TOKEN_EXPIRES_IN', | ||
), | ||
}; | ||
|
||
case TOKEN_TYPE_ENUM.EMAIL_CONFIRMATION: | ||
return { | ||
secret: this.configService.get<string>( | ||
'EMAIL_CONFIRMATION_TOKEN_SECRET', | ||
), | ||
expiresIn: this.configService.get<string>( | ||
'EMAIL_CONFIRMATION_TOKEN_EXPIRES_IN', | ||
), | ||
}; | ||
|
||
default: | ||
throw new Error('Invalid token type'); | ||
} | ||
} | ||
} |
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