mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-27 05:09:26 +00:00
add email account validation
This commit is contained in:
@@ -5,48 +5,48 @@ import {
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import * as jwt from 'jsonwebtoken';
|
||||
import { UsersRepository } from '../../users/infrastructure/users.repository';
|
||||
} 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();
|
||||
const request = context.switchToHttp().getRequest()
|
||||
if (!request.headers || !request.headers.authorization) {
|
||||
throw new BadRequestException([{ message: 'No any auth headers' }]);
|
||||
throw new BadRequestException([{ message: 'No any auth headers' }])
|
||||
}
|
||||
const authorizationData = request.headers.authorization.split(' ');
|
||||
const token = authorizationData[1];
|
||||
const tokenName = authorizationData[0];
|
||||
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);
|
||||
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);
|
||||
console.log(e)
|
||||
throw new UnauthorizedException([
|
||||
{
|
||||
message: 'login or password invalid',
|
||||
},
|
||||
]);
|
||||
])
|
||||
}
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,22 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { Observable } from 'rxjs';
|
||||
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';
|
||||
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' }]);
|
||||
throw new UnauthorizedException([{ message: 'No any auth headers' }])
|
||||
} else {
|
||||
if (request.headers.authorization != exceptedAuthInput) {
|
||||
throw new UnauthorizedException([
|
||||
{
|
||||
message: 'login or password invalid',
|
||||
},
|
||||
]);
|
||||
])
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,22 @@
|
||||
import {
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
UsePipes,
|
||||
ValidationPipe,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { ExecutionContext, Injectable, UsePipes, ValidationPipe } from '@nestjs/common'
|
||||
import { AuthGuard } from '@nestjs/passport'
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||
constructor() {
|
||||
super();
|
||||
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;
|
||||
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;
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { AuthGuard } from '@nestjs/passport'
|
||||
|
||||
@Injectable()
|
||||
export class LocalAuthGuard extends AuthGuard('local') {}
|
||||
|
||||
Reference in New Issue
Block a user