File

packages/server/src/modules/auth/auth.service.ts

Index

Methods

Constructor

constructor(usersService: UserService, roleService: RoleService, jwtService: JwtService, redisService: RedisService, configService: ConfigService)
Parameters :
Name Type Optional
usersService UserService No
roleService RoleService No
jwtService JwtService No
redisService RedisService No
configService ConfigService No

Methods

Async generateAccessToken
generateAccessToken(user: UserEntity)
Parameters :
Name Type Optional
user UserEntity No
Returns : Promise<literal type>
Async login
login(body: AuthRequest)
Parameters :
Name Type Optional
body AuthRequest No
Returns : unknown
Async profile
profile(user: ActiveUserData)
Parameters :
Name Type Optional
user ActiveUserData No
Returns : Promise<Partial<UserEntity>>
Async signOut
signOut(userId: string)
Parameters :
Name Type Optional
userId string No
Returns : Promise<void>
Async validateUser
validateUser(email: string, pass: string)
Parameters :
Name Type Optional
email string No
pass string No
Returns : Promise<Partial | null>
import { randomUUID } from "crypto";
import { UserEntity } from "@bill/database/dist/entities";
import { Injectable, UnauthorizedException } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { JwtService } from "@nestjs/jwt";

import { PERMISSION_LIST } from "@/assets";
import { ActiveUserData } from "@/common/interfaces/active-user-data.interface";
import { RedisService } from "@/modules/redis/redis.service";
import { RoleService } from "@/modules/role/role.service";
import { UserService } from "@/modules/user/user.service";

import { AuthRequest } from "./auth.interface";

@Injectable()
export class AuthService {
  constructor(
    private usersService: UserService,
    private roleService: RoleService,
    private jwtService: JwtService,
    private readonly redisService: RedisService,
    private configService: ConfigService
  ) {}

  async validateUser(
    email: string,
    pass: string
  ): Promise<Partial<UserEntity> | null> {
    const user = await this.usersService.findOne(email, pass);

    if (user) {
      const { password, ...result } = user;

      return result;
    }

    return null;
  }

  async login(body: AuthRequest) {
    const user = await this.usersService.findOne(body.username, body.password);

    if (!user) {
      throw new UnauthorizedException();
    }

    const role = await this.roleService.getByIdWithPermission(
      user.role?.id ?? 0
    );

    user.permissions = role.menus;

    return await this.generateAccessToken(user);
  }

  async signOut(userId: string): Promise<void> {
    return this.redisService.delete(`user-${userId}`);
  }

  async profile(user: ActiveUserData): Promise<Partial<UserEntity>> {
    const userEntity = await this.usersService.getByIdWithError(user.id, {
      role: true,
      company: true,
    });
    const role = await this.roleService.getByIdWithPermission(
      userEntity.role?.id ?? 0
    );

    userEntity.permissions = role.menus;
    userEntity.password = "";

    return userEntity;
  }

  async generateAccessToken(
    user: UserEntity
  ): Promise<{ accessToken: string; user?: UserEntity }> {
    const tokenId = randomUUID();

    await this.redisService.insert(`user-${user.id}`, tokenId);

    const accessToken = await this.jwtService.signAsync(
      {
        id: user.id,
        email: user.email,
        companyId: user.company?.id,
        tokenId,
      } as ActiveUserData,
      {
        secret: this.configService.get("jwt").secret,
      }
    );

    return { accessToken, user };
  }
}

results matching ""

    No results matching ""