34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import { UserService } from 'src/objects/user/user.service';
|
|
import { BadRequestException, Injectable } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { UserLoginDto } from './dto/user-login.dto';
|
|
import { UserLoginResponseDto } from './dto/user-login-response.dto';
|
|
import { AuthService } from 'src/shared/auth/auth.service';
|
|
|
|
@Injectable()
|
|
export class AppUserService {
|
|
constructor(
|
|
private readonly userService: UserService,
|
|
private readonly config: ConfigService,
|
|
private readonly auth: AuthService,
|
|
) {}
|
|
|
|
async login({ email, password }: UserLoginDto) {
|
|
const user = await this.userService.getUserByEmail(email);
|
|
if (!user) {
|
|
throw new BadRequestException('Invalid username or password');
|
|
}
|
|
|
|
if (!(await this.auth.comparePassword(user, password))) {
|
|
throw new BadRequestException('Invalid username or password');
|
|
}
|
|
|
|
const token = await this.auth.issueJWT(user);
|
|
|
|
return new UserLoginResponseDto({
|
|
accessToken: token,
|
|
expiresIn: this.auth.expiry,
|
|
});
|
|
}
|
|
}
|