43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { UsersService } from '../users/users.service';
|
|
import { hashPassword } from '../helper/hash_password';
|
|
import { JwtService } from '@nestjs/jwt';
|
|
import { User } from '../users/entities/user.entity';
|
|
|
|
@Injectable()
|
|
export class AuthService {
|
|
constructor(
|
|
private usersService: UsersService,
|
|
private jwtService: JwtService,
|
|
) {}
|
|
|
|
async validateUser(username: string, pass: string): Promise<any> {
|
|
const user = await this.usersService.findOneByUsername(username);
|
|
|
|
if (user && user.password === (await hashPassword(pass, user.salt))) {
|
|
const { password, ...result } = user;
|
|
|
|
return result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
async login(user: User) {
|
|
const payload = {
|
|
username: user.username,
|
|
sub: user.id,
|
|
role: user.roles.name,
|
|
partner: user.partner?.id,
|
|
};
|
|
|
|
return {
|
|
access_token: this.jwtService.sign(payload),
|
|
};
|
|
}
|
|
|
|
getProfile = async (userId: string) => {
|
|
return this.usersService.findOne(userId);
|
|
};
|
|
}
|