-
Notifications
You must be signed in to change notification settings - Fork 52
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
1 parent
2a5d428
commit 80c8758
Showing
29 changed files
with
189 additions
and
211 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
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,6 +1,4 @@ | ||
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './jwt-auth.guard'; | ||
export * from './local-auth.guard'; | ||
export * from './local-auth.guard'; |
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,15 +1,13 @@ | ||
import { Injectable, UnauthorizedException } from "@nestjs/common"; | ||
import { AuthGuard } from "@nestjs/passport"; | ||
|
||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class JwtAuthGuard extends AuthGuard('jwt') { | ||
|
||
handleRequest(err, user, info) { | ||
// You can throw an exception based on either "info" or "err" arguments | ||
if (err || !user) { | ||
throw err || new UnauthorizedException('NO ESTAS AUTHENTICADO'); | ||
} | ||
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { AuthGuard } from "@nestjs/passport"; | ||
import { Injectable } from "@nestjs/common"; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class LocalAuthGuard extends AuthGuard('local') {} | ||
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,2 +1,2 @@ | ||
export * from './local.strategy'; | ||
export * from './jwt.strategy'; | ||
export * from './jwt.strategy'; |
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,24 +1,21 @@ | ||
import { Strategy } from "passport-local"; | ||
import { Injectable, UnauthorizedException } from "@nestjs/common"; | ||
import { PassportStrategy } from "@nestjs/passport"; | ||
import { AuthService } from "../auth.service"; | ||
|
||
import { Strategy } from 'passport-local'; | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { AuthService } from '../auth.service'; | ||
|
||
@Injectable() | ||
export class LocalStrategy extends PassportStrategy(Strategy) { | ||
constructor( | ||
private readonly authService: AuthService | ||
) { | ||
constructor(private readonly authService: AuthService) { | ||
super({ | ||
usernameField: 'email', // 'username' | ||
passwordField: 'password' // 'passport' | ||
passwordField: 'password', // 'passport' | ||
}); | ||
} | ||
|
||
async validate(email: string, password: string) { | ||
const user = await this.authService.validateUser(email, password); | ||
if (!user) throw new UnauthorizedException('Login user or password does not match.'); | ||
if (!user) | ||
throw new UnauthorizedException('Login user or password does not match.'); | ||
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 |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import { applyDecorators, UseGuards } from "@nestjs/common"; | ||
import { ApiBearerAuth } from "@nestjs/swagger"; | ||
import { JwtAuthGuard } from "src/auth/guards"; | ||
import { ACGuard, Role, UseRoles } from "nest-access-control"; | ||
|
||
import { applyDecorators, UseGuards } from '@nestjs/common'; | ||
import { ApiBearerAuth } from '@nestjs/swagger'; | ||
import { JwtAuthGuard } from 'src/auth/guards'; | ||
import { ACGuard, Role, UseRoles } from 'nest-access-control'; | ||
|
||
export function Auth(...roles: Role[]) { | ||
return applyDecorators( | ||
UseGuards(JwtAuthGuard, ACGuard), | ||
UseRoles(...roles), | ||
ApiBearerAuth() | ||
) | ||
} | ||
ApiBearerAuth(), | ||
); | ||
} |
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,2 +1,2 @@ | ||
export * from './user.decorator'; | ||
export * from './auth.decorator'; | ||
export * from './auth.decorator'; |
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 |
---|---|---|
|
@@ -7,4 +7,4 @@ export const User = createParamDecorator( | |
|
||
return data ? user && user[data] : 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
import { getRepository } from 'typeorm' | ||
import { ConfigService } from '@nestjs/config' | ||
import { DEFAULT_USER_EMAIL, DEFAULT_USER_PASSWORD } from './constants' | ||
import { User } from 'src/user/entities' | ||
import { getRepository } from 'typeorm'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { DEFAULT_USER_EMAIL, DEFAULT_USER_PASSWORD } from './constants'; | ||
import { User } from 'src/user/entities'; | ||
|
||
export const setDefaultUser = async (config: ConfigService) => { | ||
const userRepository = getRepository<User>(User) | ||
const userRepository = getRepository<User>(User); | ||
|
||
const defaultUser = await userRepository | ||
.createQueryBuilder() | ||
.where('email = :email', { email: config.get<string>('DEFAULT_USER_EMAIL') }) | ||
.getOne() | ||
|
||
.where('email = :email', { | ||
email: config.get<string>('DEFAULT_USER_EMAIL'), | ||
}) | ||
.getOne(); | ||
|
||
if (!defaultUser) { | ||
const adminUser = userRepository.create({ | ||
email: config.get<string>(DEFAULT_USER_EMAIL), | ||
password: config.get<string>(DEFAULT_USER_PASSWORD), | ||
roles: ['ADMIN'] | ||
}) | ||
roles: ['ADMIN'], | ||
}); | ||
|
||
return await userRepository.save(adminUser) | ||
return await userRepository.save(adminUser); | ||
} | ||
} | ||
}; |
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.