mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-26 20:59:28 +00:00
add auth
This commit is contained in:
52
src/modules/auth/guards/auth.guard.ts
Normal file
52
src/modules/auth/guards/auth.guard.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import * as jwt from 'jsonwebtoken';
|
||||
import { UsersRepository } from '../../users/infrastructure/users.repository';
|
||||
|
||||
@Injectable()
|
||||
export class AuthGuard implements CanActivate {
|
||||
constructor(private readonly usersRepository: UsersRepository) {}
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
if (!request.headers || !request.headers.authorization) {
|
||||
throw new BadRequestException([{ message: 'No any auth headers' }]);
|
||||
}
|
||||
const authorizationData = request.headers.authorization.split(' ');
|
||||
const token = authorizationData[1];
|
||||
const tokenName = authorizationData[0];
|
||||
if (tokenName != 'Bearer') {
|
||||
throw new UnauthorizedException([
|
||||
{
|
||||
message: 'login or password invalid',
|
||||
},
|
||||
]);
|
||||
}
|
||||
try {
|
||||
const secretKey = process.env.JWT_SECRET_KEY;
|
||||
const decoded: any = jwt.verify(token, secretKey!);
|
||||
const user = await this.usersRepository.findUserById(decoded.userId);
|
||||
if (!user) {
|
||||
throw new NotFoundException([
|
||||
{
|
||||
field: 'token',
|
||||
message: 'user not found',
|
||||
},
|
||||
]);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
throw new UnauthorizedException([
|
||||
{
|
||||
message: 'login or password invalid',
|
||||
},
|
||||
]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
29
src/modules/auth/guards/base-auth.guard.ts
Normal file
29
src/modules/auth/guards/base-auth.guard.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
@Injectable()
|
||||
export class BaseAuthGuard implements CanActivate {
|
||||
canActivate(
|
||||
context: ExecutionContext,
|
||||
): boolean | Promise<boolean> | Observable<boolean> {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const exceptedAuthInput = 'Basic YWRtaW46cXdlcnR5';
|
||||
if (!request.headers || !request.headers.authorization) {
|
||||
throw new UnauthorizedException([{ message: 'No any auth headers' }]);
|
||||
} else {
|
||||
if (request.headers.authorization != exceptedAuthInput) {
|
||||
throw new UnauthorizedException([
|
||||
{
|
||||
message: 'login or password invalid',
|
||||
},
|
||||
]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
25
src/modules/auth/guards/jwt-auth.guard.ts
Normal file
25
src/modules/auth/guards/jwt-auth.guard.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import {
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
UsePipes,
|
||||
ValidationPipe,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
@UsePipes(new ValidationPipe())
|
||||
validateLoginDto(): void {}
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
|
||||
const res: boolean = await (super.canActivate(context) as Promise<boolean>);
|
||||
if (!res) return false;
|
||||
|
||||
// check DTO
|
||||
return res;
|
||||
}
|
||||
}
|
||||
5
src/modules/auth/guards/local-auth.guard.ts
Normal file
5
src/modules/auth/guards/local-auth.guard.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@Injectable()
|
||||
export class LocalAuthGuard extends AuthGuard('local') {}
|
||||
Reference in New Issue
Block a user