mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2026-01-23 21:02:08 +00:00
add auth
This commit is contained in:
21
src/modules/auth/strategies/jwt.strategy.ts
Normal file
21
src/modules/auth/strategies/jwt.strategy.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { AppSettings } from '../../../settings/app-settings';
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(
|
||||
@Inject(AppSettings.name) private readonly appSettings: AppSettings,
|
||||
) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
ignoreExpiration: true,
|
||||
secretOrKey: appSettings.auth.ACCESS_JWT_SECRET_KEY,
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: any) {
|
||||
return { userId: payload.userId };
|
||||
}
|
||||
}
|
||||
21
src/modules/auth/strategies/local.strategy.ts
Normal file
21
src/modules/auth/strategies/local.strategy.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { Strategy } from 'passport-local';
|
||||
import { AuthService } from '../auth.service';
|
||||
|
||||
@Injectable()
|
||||
export class LocalStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(private readonly authService: AuthService) {
|
||||
super({
|
||||
usernameField: 'login',
|
||||
});
|
||||
}
|
||||
|
||||
async validate(login: string, password: string): Promise<any> {
|
||||
const user = await this.authService.checkCredentials(login, password);
|
||||
if (user.resultCode === 1) {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user