-
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.
fix test env route, add signup tests
- Loading branch information
Showing
24 changed files
with
664 additions
and
103 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 was deleted.
Oops, something went wrong.
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
24 changes: 22 additions & 2 deletions
24
api/src/modules/auth/authentication/authentication.controller.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,4 +1,24 @@ | ||
import { Controller } from '@nestjs/common'; | ||
import { Body, Controller, Post, UseGuards } from '@nestjs/common'; | ||
import { User } from '@shared/entities/users/user.entity'; | ||
import { AuthenticationService } from '@api/modules/auth/authentication/authentication.service'; | ||
import { LoginDto } from '@api/modules/auth/dtos/login.dto'; | ||
import { LocalAuthGuard } from '@api/modules/auth/guards/local-auth.guard'; | ||
import { GetUser } from '@api/modules/auth/decorators/get-user.decorator'; | ||
import { Public } from '@api/modules/auth/decorators/is-public.decorator'; | ||
|
||
@Controller('authentication') | ||
export class AuthenticationController {} | ||
export class AuthenticationController { | ||
constructor(private authService: AuthenticationService) {} | ||
|
||
@Public() | ||
@Post('signup') | ||
async signup(@Body() signupDto: LoginDto) { | ||
return this.authService.signup(signupDto); | ||
} | ||
|
||
@UseGuards(LocalAuthGuard) | ||
@Post('login') | ||
async login(@GetUser() user: User) { | ||
return this.authService.signIn(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
42 changes: 40 additions & 2 deletions
42
api/src/modules/auth/authentication/authentication.service.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,4 +1,42 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { UsersService } from '@api/modules/users/users.service'; | ||
import { User } from '@shared/entities/users/user.entity'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { LoginDto } from '@api/modules/auth/dtos/login.dto'; | ||
import { JwtPayload } from '@api/modules/auth/strategies/jwt.strategy'; | ||
|
||
@Injectable() | ||
export class AuthenticationService {} | ||
export class AuthenticationService { | ||
constructor( | ||
private readonly usersService: UsersService, | ||
private readonly jwt: JwtService, | ||
) {} | ||
async validateUser(email: string, password: string): Promise<User> { | ||
const user = await this.usersService.findByEmail(email); | ||
if (user && (await bcrypt.compare(password, user.password))) { | ||
return user; | ||
} | ||
throw new UnauthorizedException(`Invalid credentials`); | ||
} | ||
|
||
async signup(signupDto: LoginDto): Promise<void> { | ||
const passwordHash = await bcrypt.hash(signupDto.password, 10); | ||
await this.usersService.createUser({ | ||
email: signupDto.email, | ||
password: passwordHash, | ||
}); | ||
} | ||
|
||
async login(loginDto: LoginDto): Promise<{ access_token: string }> { | ||
const user = await this.validateUser(loginDto.email, loginDto.password); | ||
return { | ||
access_token: this.jwt.sign({ id: user.id }), | ||
}; | ||
} | ||
async signIn(user: User): Promise<{ user: User; accessToken: string }> { | ||
const payload: JwtPayload = { id: user.id }; | ||
const accessToken: string = this.jwt.sign(payload); | ||
return { user, accessToken }; | ||
} | ||
} |
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 @@ | ||
/** | ||
* @note: Depending on how we will proceed with the repo structure, we might need to move this file to the shared module. | ||
*/ | ||
|
||
export class LoginDto { | ||
email: string; | ||
password: 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
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 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class LocalAuthGuard extends AuthGuard('local') {} |
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,29 +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; | ||
// } | ||
// } | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
|
||
import { Strategy } from 'passport-local'; | ||
import { User } from '@shared/entities/users/user.entity'; | ||
import { AuthenticationService } from '@api/modules/auth/authentication/authentication.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: AuthenticationService) { | ||
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { join } from 'path'; | ||
import { readdirSync } from 'fs'; | ||
|
||
// TODO: Workaround: This should be prob fixed in the jest conf | ||
|
||
const TEST_RELATIVE_PATH = '../../../../'; | ||
const DEFAULT_RELATIVE_PATH = '../../../../../../'; | ||
|
||
/** | ||
* @description: Resolve the path of the config file depending on the environment | ||
*/ | ||
export function resolveConfigPath(relativePath: string): string { | ||
const rootDir = | ||
process.env.NODE_ENV === 'test' | ||
? TEST_RELATIVE_PATH | ||
: DEFAULT_RELATIVE_PATH; | ||
return join(__dirname, rootDir, relativePath); | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.