add: progress authentication
This commit is contained in:
10
src/helper/hash_password.ts
Normal file
10
src/helper/hash_password.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import * as crypto from 'crypto';
|
||||
|
||||
export function hashPassword(password, salt): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
crypto.pbkdf2(password, salt, 50, 100, 'sha512', (err, values) => {
|
||||
if (err) return reject(err);
|
||||
resolve(values.toString('hex'));
|
||||
});
|
||||
});
|
||||
}
|
||||
22
src/helper/jwt.strategy.ts
Normal file
22
src/helper/jwt.strategy.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(private readonly authService: AuthService) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
secretOrKey: process.env.SECRETKEY,
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: JwtPayload): Promise<UserDto> {
|
||||
const user = await this.authService.validateUser(payload);
|
||||
if (!user) {
|
||||
throw new HttpException('Invalid token', HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user