mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-23 17:08:34 +00:00
36 lines
983 B
TypeScript
36 lines
983 B
TypeScript
|
|
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||
|
|
import { UsersService } from '../users/users.service';
|
||
|
|
import { JwtService } from '@nestjs/jwt';
|
||
|
|
import { User } from 'src/users/user.entity';
|
||
|
|
const bcrypt = require('bcrypt');
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class AuthService {
|
||
|
|
constructor(
|
||
|
|
private usersService: UsersService,
|
||
|
|
private jwtService: JwtService
|
||
|
|
) { }
|
||
|
|
|
||
|
|
async validateUser(email: string, password: string): Promise<User> {
|
||
|
|
const user = await this.usersService.findByEmail(email);
|
||
|
|
|
||
|
|
const isVerified = await bcrypt.compare(password, user.passwordDigest)
|
||
|
|
|
||
|
|
return isVerified ? user : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
async login(params: any) {
|
||
|
|
const user = await this.validateUser(params.email, params.password);
|
||
|
|
|
||
|
|
if (user) {
|
||
|
|
const payload = { username: user.id, sub: user.email };
|
||
|
|
|
||
|
|
return {
|
||
|
|
access_token: this.jwtService.sign(payload),
|
||
|
|
};
|
||
|
|
} else {
|
||
|
|
throw new UnauthorizedException('Invalid credentials');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|