-
Notifications
You must be signed in to change notification settings - Fork 5
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 #24 from Daaaiii/JWT
Adiciona endpoint de login com jwt
- Loading branch information
Showing
10 changed files
with
684 additions
and
500 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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { AccountModule } from './modules'; | ||
import { AuthModule } from './modules/jwtModule/account.auth.module'; | ||
|
||
@Module({ | ||
imports: [AccountModule], | ||
imports: [AccountModule, AuthModule], | ||
}) | ||
export class AppModule {} |
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,19 @@ | ||
import { Body, Controller, Post, HttpCode } from '@nestjs/common'; | ||
|
||
import { ApiTags } from '@nestjs/swagger'; | ||
|
||
import { AuthService } from './account.auth.service'; | ||
import { LoginDto } from './account.auth.login.dto'; | ||
|
||
@Controller('/auth') | ||
export class AuthController { | ||
constructor(private readonly authService: AuthService) {} | ||
|
||
@ApiTags() | ||
@Post('/login') | ||
@HttpCode(200) | ||
async login(@Body() { email, password }: LoginDto) { | ||
const token = await this.authService.login(email, password); | ||
return { token, message: 'Login efetuado com sucesso' }; | ||
} | ||
} |
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 { PickType } from '@nestjs/swagger'; | ||
import { CreateAccountDto } from '../account/account.dto'; | ||
|
||
export class LoginDto extends PickType(CreateAccountDto, [ | ||
'email', | ||
'password', | ||
]) {} |
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,23 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import { AuthController } from './account.auth.controller'; | ||
|
||
import { PrismaModule } from 'src/prisma/prisma.module'; | ||
import { AccountModule } from '../account/account.module'; | ||
import { AuthService } from './account.auth.service'; | ||
import { AccountService } from '../account/account.service'; | ||
import { AccountRepository } from '../account/account.repository'; | ||
|
||
@Module({ | ||
imports: [ | ||
JwtModule.register({ | ||
secret: process.env.SECRET_JWT, | ||
}), | ||
AccountModule, | ||
PrismaModule, | ||
AuthModule, | ||
], | ||
controllers: [AuthController], | ||
providers: [AuthService, AccountService, AccountRepository], | ||
}) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { BadRequestException, Injectable } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { PrismaService } from 'src/prisma/prisma.service'; | ||
import { Credential } from '@prisma/client'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { hashDataAsync } from '../../globals/utils'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
private readonly jwtService: JwtService, | ||
private readonly prisma: PrismaService, | ||
) {} | ||
|
||
async createToken(user: Credential) { | ||
const token = { | ||
accessToken: this.jwtService.sign( | ||
{ | ||
id: user.id, | ||
}, | ||
{ | ||
expiresIn: '1 h', | ||
subject: String(user.id), | ||
}, | ||
), | ||
|
||
refreshToken: this.jwtService.sign( | ||
{ | ||
id: user.id, | ||
}, | ||
{ | ||
expiresIn: '3 h', | ||
subject: String(user.id), | ||
}, | ||
), | ||
}; | ||
|
||
return token; | ||
} | ||
|
||
async login(email: string, password: string) { | ||
const hashedEmail = await hashDataAsync({ | ||
unhashedData: email, | ||
salt: process.env.SALT_DATA_HASH, | ||
}); | ||
|
||
const user = await this.prisma.credential.findUnique({ | ||
where: { | ||
email: hashedEmail, | ||
}, | ||
}); | ||
|
||
if (!user) { | ||
throw new BadRequestException('Usuário não encontrado.'); | ||
} | ||
|
||
const comparePassword = await bcrypt.compare(password, user.password); | ||
|
||
if (!comparePassword) { | ||
throw new BadRequestException('A senha está incorreta.'); | ||
} | ||
|
||
return this.createToken(user); | ||
} | ||
} |
Oops, something went wrong.