-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add auth module and jwt service
- Loading branch information
1 parent
6365bb6
commit 1e99946
Showing
14 changed files
with
231 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const JWT_SECRET = 'JWT_SECRET'; | ||
export const JWT_REFRESH_SECRET = 'JWT_REFRESH_SECRET'; | ||
|
||
export const JWT_EXPIRES_IN = 'JWT_EXPIRES_IN'; | ||
export const JWT_REFRESH_EXPIRES_IN = 'JWT_REFRESH_EXPIRES_IN'; |
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,13 @@ | ||
import { fromNullable, getOrThrowWith } from 'effect/Option'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
export const getConfigValue = <T>( | ||
configService: ConfigService, | ||
key: string, | ||
): T => { | ||
console.log('AAA'); | ||
return getOrThrowWith<T>( | ||
fromNullable(configService.get<T>(key)), | ||
() => new Error(`Missing ${key}`), | ||
); | ||
}; |
17 changes: 17 additions & 0 deletions
17
src/modules/auth/application/interface/jwt-authentication.interface.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,17 @@ | ||
import { AuthUser } from '../presentation/dto/auth-user.dto'; | ||
import { Option } from 'effect/Option'; | ||
import { JwtUser } from '../presentation/dto/jwt-user.dto'; | ||
|
||
export interface JwtAuthentication { | ||
verifyToken(token: string): Promise<Option<AuthUser>>; | ||
|
||
verifyRefreshToken(refreshToken: string): Promise<Option<AuthUser>>; | ||
|
||
generateToken(user: AuthUser): Promise<string>; | ||
|
||
generateRefreshToken(user: AuthUser): Promise<string>; | ||
|
||
generateJwtUser(user: AuthUser): Promise<JwtUser>; | ||
|
||
generateJwtUserFromRefresh(token: string): Promise<JwtUser>; | ||
} |
29 changes: 29 additions & 0 deletions
29
src/modules/auth/application/presentation/body/login.body.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,29 @@ | ||
import { | ||
IsEmail, | ||
IsNotEmpty, | ||
IsString, | ||
IsStrongPassword, | ||
MaxLength, | ||
} from 'class-validator'; | ||
|
||
export class LoginBody { | ||
@IsNotEmpty() | ||
@IsEmail() | ||
@IsString() | ||
email!: string; | ||
|
||
@IsNotEmpty() | ||
@IsString() | ||
@MaxLength(20) | ||
@IsStrongPassword( | ||
{ | ||
minLength: 8, | ||
minSymbols: 1, | ||
}, | ||
{ | ||
message: | ||
'Password must be at least 8 characters long and contain at least one symbol', | ||
}, | ||
) | ||
password!: string; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/modules/auth/application/presentation/body/signup.body.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,12 @@ | ||
import { LoginBody } from './login.body'; | ||
import { IsNotEmpty, IsString } from 'class-validator'; | ||
|
||
export class SignupBody extends LoginBody { | ||
@IsNotEmpty() | ||
@IsString() | ||
firstName!: string; | ||
|
||
@IsNotEmpty() | ||
@IsString() | ||
lastName!: string; | ||
} |
5 changes: 5 additions & 0 deletions
5
src/modules/auth/application/presentation/dto/auth-user.dto.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,5 @@ | ||
export interface AuthUser { | ||
id: string; | ||
email: string; | ||
role: number; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/modules/auth/application/presentation/dto/jwt-user.dto.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,9 @@ | ||
import { AuthUser } from './auth-user.dto'; | ||
|
||
export interface JwtUser { | ||
token: string; | ||
expiresIn: number; | ||
refreshToken: string; | ||
refreshExpiresIn: number; | ||
user: AuthUser; | ||
} |
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,95 @@ | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { | ||
JWT_EXPIRES_IN, | ||
JWT_REFRESH_EXPIRES_IN, | ||
JWT_REFRESH_SECRET, | ||
JWT_SECRET, | ||
} from '../../../../config/env/configuration.constant'; | ||
import { isNone, liftThrowable, Option } from 'effect/Option'; | ||
import { JwtAuthentication } from '../interface/jwt-authentication.interface'; | ||
import { AuthUser } from '../presentation/dto/auth-user.dto'; | ||
import * as jwt from 'jsonwebtoken'; | ||
import { getConfigValue } from '../../../../libs/util/config.util'; | ||
import { JwtUser } from '../presentation/dto/jwt-user.dto'; | ||
|
||
@Injectable() | ||
export class JwtService implements JwtAuthentication { | ||
private readonly tokenSecret: string; | ||
private readonly refreshTokenSecret: string; | ||
private readonly tokenExpiration: number; | ||
private readonly refreshTokenExpiration: number; | ||
|
||
constructor(private readonly configService: ConfigService) { | ||
this.tokenSecret = getConfigValue(this.configService, JWT_SECRET); | ||
this.refreshTokenSecret = getConfigValue<string>( | ||
this.configService, | ||
JWT_REFRESH_SECRET, | ||
); | ||
this.tokenExpiration = getConfigValue<number>( | ||
this.configService, | ||
JWT_EXPIRES_IN, | ||
); | ||
this.refreshTokenExpiration = getConfigValue<number>( | ||
this.configService, | ||
JWT_REFRESH_EXPIRES_IN, | ||
); | ||
} | ||
|
||
async generateToken(user: AuthUser): Promise<string> { | ||
return jwt.sign(user, this.tokenSecret, { | ||
expiresIn: this.tokenExpiration, | ||
}); | ||
} | ||
|
||
async generateRefreshToken(user: AuthUser): Promise<string> { | ||
return jwt.sign(user, this.refreshTokenSecret, { | ||
expiresIn: this.refreshTokenExpiration, | ||
}); | ||
} | ||
|
||
async generateJwtUser(authUser: AuthUser): Promise<JwtUser> { | ||
const token = await this.generateToken(authUser); | ||
const refreshToken = await this.generateRefreshToken(authUser); | ||
return { | ||
token, | ||
expiresIn: this.tokenExpiration, | ||
refreshToken, | ||
refreshExpiresIn: this.refreshTokenExpiration, | ||
user: authUser, | ||
}; | ||
} | ||
|
||
async generateJwtUserFromRefresh(refreshToken: string): Promise<JwtUser> { | ||
const authUser = this.verifyJwt<AuthUser>( | ||
refreshToken, | ||
this.refreshTokenSecret, | ||
); | ||
if (isNone(authUser)) throw new UnauthorizedException('Invalid Token!'); | ||
return this.generateJwtUser(this.convertToAuthUser(authUser.value)); | ||
} | ||
|
||
async verifyToken(token: string): Promise<Option<AuthUser>> { | ||
return this.verifyJwt<AuthUser>(token, this.tokenSecret); | ||
} | ||
|
||
async verifyRefreshToken(refreshToken: string): Promise<Option<AuthUser>> { | ||
return this.verifyJwt<AuthUser>(refreshToken, this.refreshTokenSecret); | ||
} | ||
|
||
/** | ||
* Generic JWT verification method. | ||
*/ | ||
private verifyJwt<T>(token: string, secret: string): Option<T> { | ||
return liftThrowable(() => jwt.verify(token, secret) as T)(); | ||
} | ||
|
||
/** | ||
* Helper method to clean the AuthUser object. | ||
*/ | ||
convertToAuthUser = (authUser: AuthUser): AuthUser => ({ | ||
id: authUser.id, | ||
email: authUser.email, | ||
role: authUser.role, | ||
}); | ||
} |
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,18 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { APP_GUARD } from '@nestjs/core'; | ||
import { ThrottlerGuard } from '@nestjs/throttler'; | ||
import { JwtService } from './application/service/jwt.service'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [], | ||
providers: [ | ||
JwtService, | ||
{ | ||
provide: APP_GUARD, | ||
useClass: ThrottlerGuard, | ||
}, | ||
], | ||
exports: [], | ||
}) | ||
export class AuthModule {} |
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,4 @@ | ||
export enum UserRole { | ||
ADMIN = 0, | ||
USER = 1, | ||
} |
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,4 @@ | ||
export enum UserState { | ||
ACTIVE = 'ACTIVE', | ||
DISABLED = 'DISABLED', | ||
} |