-
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
16 changed files
with
376 additions
and
8 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
30 changes: 29 additions & 1 deletion
30
api/src/modules/auth/authentication/authentication.module.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 |
---|---|---|
@@ -1,9 +1,37 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AuthenticationService } from './authentication.service'; | ||
import { AuthenticationController } from './authentication.controller'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import { ApiConfigModule } from '@api/modules/config/app-config.module'; | ||
import { ApiConfigService } from '@api/modules/config/app-config.service'; | ||
import { JwtStrategy } from '@auth/strategies/jwt.strategy'; | ||
import { UsersService } from '@api/modules/users/users.service'; | ||
import { UsersModule } from '@api/modules/users/users.module'; | ||
|
||
@Module({ | ||
providers: [AuthenticationService], | ||
imports: [ | ||
PassportModule.register({ defaultStrategy: 'jwt' }), | ||
JwtModule.registerAsync({ | ||
imports: [ApiConfigModule], | ||
inject: [ApiConfigService], | ||
useFactory: (config: ApiConfigService) => ({ | ||
secret: config.getJWTConfig().secret, | ||
signOptions: { expiresIn: config.getJWTConfig().expiresIn }, | ||
}), | ||
}), | ||
UsersModule, | ||
], | ||
providers: [ | ||
AuthenticationService, | ||
{ | ||
provide: JwtStrategy, | ||
useFactory: (users: UsersService, config: ApiConfigService) => { | ||
return new JwtStrategy(users, config); | ||
}, | ||
inject: [UsersService, ApiConfigService], | ||
}, | ||
], | ||
controllers: [AuthenticationController], | ||
}) | ||
export class AuthenticationModule {} |
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,9 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
import { User } from '@shared/entities/users/user.entity'; | ||
|
||
export const GetUser = createParamDecorator( | ||
(data: unknown, ctx: ExecutionContext): User => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
return request.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
|
||
/** | ||
* @description Decorator to inject a IS_PUBLIC_KEY metadata to the handler, which will be read by the JwtAuthGuard to allow public access to the handler. | ||
*/ | ||
|
||
export const IS_PUBLIC_KEY = 'isPublic'; | ||
export const Public = () => SetMetadata(IS_PUBLIC_KEY, true); |
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,27 @@ | ||
import { Injectable, ExecutionContext } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { Observable } from 'rxjs'; | ||
import { IS_PUBLIC_KEY } from '@auth/decorators/is-public.decorator'; | ||
|
||
@Injectable() | ||
export class JwtAuthGuard extends AuthGuard('jwt') { | ||
constructor(private readonly reflector: Reflector) { | ||
super(); | ||
} | ||
|
||
canActivate( | ||
context: ExecutionContext, | ||
): boolean | Promise<boolean> | Observable<boolean> { | ||
const isPublic: boolean = this.reflector.get<boolean>( | ||
IS_PUBLIC_KEY, | ||
context.getHandler(), | ||
); | ||
|
||
if (isPublic) { | ||
return true; | ||
} | ||
|
||
return super.canActivate(context); | ||
} | ||
} |
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,30 @@ | ||
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'; | ||
|
||
export type JwtPayload = { id: string }; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor( | ||
private readonly userService: UsersService, | ||
private readonly config: ApiConfigService, | ||
) { | ||
const { secret } = config.getJWTConfig(); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
// import { PassportStrategy } from '@nestjs/passport'; | ||
// | ||
// import { Strategy } from 'passport-local'; | ||
// import { User } from '@shared/dto/users/user.entity'; | ||
// import { AuthService } from '@api/modules/auth/auth.service'; | ||
// | ||
// /** | ||
// * @description: LocalStrategy is used by passport to authenticate by email and password rather than a token. | ||
// */ | ||
// | ||
// @Injectable() | ||
// export class LocalStrategy extends PassportStrategy(Strategy) { | ||
// constructor(private readonly authService: AuthService) { | ||
// super({ usernameField: 'email' }); | ||
// } | ||
// | ||
// async validate(email: string, password: string): Promise<User> { | ||
// const user: User | null = await this.authService.validateUser( | ||
// email, | ||
// password, | ||
// ); | ||
// | ||
// 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
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,4 +1,15 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { User } from '@shared/entities/users/user.entity'; | ||
import { Repository } from 'typeorm'; | ||
|
||
@Injectable() | ||
export class UsersService {} | ||
export class UsersService { | ||
constructor(@InjectRepository(User) private repo: Repository<User>) {} | ||
|
||
async findOneBy(id: string) { | ||
return this.repo.findOne({ | ||
where: { id }, | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.