All files / src/modules/api/auth auth.service.ts

40% Statements 4/10
0% Branches 0/2
25% Functions 1/4
33.33% Lines 3/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                    1x   1x 1x                                    
import { UserEntity } from "@/modules/shared/database/entities";
import { Services } from "@/types/Constants";
import { Inject, Injectable } from "@nestjs/common";
import { JwtService } from "@nestjs/jwt";
import { IUserService } from "../user/interfaces/IUserService";
import { UserDTO } from "../user/user.dto";
import { IAuthService } from "./interfaces/IAuthService.interface";
import { JwtPayload } from "./types";
 
@Injectable()
export class AuthService implements IAuthService {
	public constructor(
		@Inject(Services.User) private readonly userService: IUserService,
		private readonly jwtService: JwtService,
	) {}
 
	public async validateUser(details: UserDTO): Promise<UserEntity> {
		const user = await this.userService.get(details.id);
		return user ? await this.userService.update(details) : ((await this.userService.create(details)) as UserEntity);
	}
 
	public async get(payload: JwtPayload) {
		const user = await this.userService.get(payload.id);
		return user;
	}
 
	public async login(payload: JwtPayload): Promise<string> {
		const token = await this.jwtService.signAsync({ payload });
		return token;
	}
}