forked from nestjs/typescript-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #4 from Matheus-kb/master
login authentication
- Loading branch information
Showing
22 changed files
with
167 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ node_modules/ | |
|
||
# Ignorar arquivos de build | ||
dist/ | ||
|
||
|
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,5 @@ | ||
{ | ||
"singleQuote": true, | ||
"trailingComma": "all" | ||
"trailingComma": "all", | ||
"endOfLine": "lf" | ||
} |
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,3 @@ | ||
{ | ||
"files.eol": "\n" | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import configuration from './config/configuration'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
import { EventModule } from './event/event.module'; | ||
import { OrganizerModule } from './organizer/organizer.module'; | ||
import { VolunteerModule } from './volunteer/volunteer.module'; | ||
import { AuthModule } from './auth/auth.module'; | ||
import { PrismaModule } from './prisma/prisma.module'; | ||
|
||
@Module({ | ||
imports: [EventModule, OrganizerModule, VolunteerModule], | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
load: [configuration], | ||
isGlobal: true, | ||
}), | ||
EventModule, | ||
OrganizerModule, | ||
VolunteerModule, | ||
AuthModule, | ||
PrismaModule, | ||
], | ||
controllers: [AppController], | ||
providers: [AppService], | ||
}) | ||
export class AppModule { } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Controller, Post, Body, UnauthorizedException } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor( | ||
private authService: AuthService, | ||
private jwtService: JwtService, | ||
) {} | ||
|
||
@Post('login') | ||
async login(@Body() body: { email: string; password: string }) { | ||
const user = await this.authService.validateUser(body.email, body.password); | ||
if (!user) { | ||
throw new UnauthorizedException('Invalid credentials'); | ||
} | ||
|
||
const payload = { email: user.email, sub: user.id, type: user.type }; | ||
const accessToken = this.jwtService.sign(payload); | ||
return { | ||
access_token: accessToken, | ||
user: 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,23 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
|
||
import { PrismaModule } from '../prisma/prisma.module'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
|
||
import { PassportModule } from '@nestjs/passport'; | ||
import { AuthController } from './auth.controller'; | ||
import { JwtStrategy } from './jwt.strategy'; | ||
|
||
@Module({ | ||
imports: [ | ||
PrismaModule, | ||
PassportModule, | ||
JwtModule.register({ | ||
secret: 'your_jwt_secret_key', | ||
signOptions: { expiresIn: '60m' }, | ||
}), | ||
], | ||
providers: [AuthService, JwtStrategy], | ||
controllers: [AuthController], | ||
}) | ||
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,27 @@ | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async validateUser(email: string, password: string): Promise<any> { | ||
const organizer = await this.prisma.organizer.findUnique({ | ||
where: { email }, | ||
}); | ||
|
||
if (organizer && organizer.password === password) { | ||
return { ...organizer, type: 'organizer' }; | ||
} | ||
|
||
const volunteer = await this.prisma.volunteer.findUnique({ | ||
where: { email }, | ||
}); | ||
|
||
if (volunteer && volunteer.password === password) { | ||
return { ...volunteer, type: 'volunteer' }; | ||
} | ||
|
||
throw new UnauthorizedException('Invalid credentials'); | ||
} | ||
} |
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 { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor(private readonly configService: ConfigService) { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
ignoreExpiration: false, | ||
secretOrKey: configService.get<string>('JWT_SECRET'), | ||
}); | ||
} | ||
|
||
async validate(payload: any) { | ||
return { userId: payload.sub, email: payload.email, type: payload.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default () => ({ | ||
JWT_SECRET: process.env.JWT_SECRET, | ||
}); |
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 @@ | ||
declare namespace NodeJS { | ||
interface ProcessEnv { | ||
JWT_SECRET: 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
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
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 { Module } from '@nestjs/common'; | ||
import { PrismaService } from './prisma.service'; | ||
|
||
@Module({ | ||
providers: [PrismaService], | ||
exports: [PrismaService], | ||
}) | ||
export class PrismaModule {} |
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,16 @@ | ||
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common'; | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
@Injectable() | ||
export class PrismaService | ||
extends PrismaClient | ||
implements OnModuleInit, OnModuleDestroy | ||
{ | ||
async onModuleInit() { | ||
await this.$connect(); | ||
} | ||
|
||
async onModuleDestroy() { | ||
await this.$disconnect(); | ||
} | ||
} |
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