feat: auth routes and service

This commit is contained in:
Louis Auzuret
2022-06-20 15:49:48 +02:00
parent 10730d17f3
commit 9221bb461e
8 changed files with 152 additions and 2 deletions

View File

@@ -0,0 +1,32 @@
import { Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service';
import { JwtService } from '@nestjs/jwt';
import * as bcrypt from 'bcryptjs';
import PayloadInterface from './interface/payload.interface';
@Injectable()
export class AuthService {
constructor(
private userService: UsersService,
private jwtService: JwtService
) {}
async validateUser(username: string, password: string): Promise<PayloadInterface> {
const user = await this.userService.user({username});
if (user && bcrypt.compareSync(password, user.password)) {
return {
username: user.username,
id: user.id
};
}
return null;
}
async login(user: PayloadInterface) {
const payload = { username: user.username, id: user.id };
const access_token = this.jwtService.sign(payload);
return {
access_token
};
}
}