File

libs/auth/application/src/lib/use-cases/sign-in.use-case.ts

Index

Methods

Constructor

constructor(authService: AuthService, jwtService: JwtService)
Parameters :
Name Type Optional
authService AuthService No
jwtService JwtService No

Methods

Async execute
execute(email: string, pass: string)
Parameters :
Name Type Optional
email string No
pass string No
Returns : Promise<literal type>
import { AuthService, AUTH_SERVICE } from '@auth/domain';
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';

@Injectable()
export class SignInUseCase {
  constructor(
    @Inject(AUTH_SERVICE)
    private authService: AuthService,
    private jwtService: JwtService,
  ) {}

  // Retrieving a user and verifying the password
  async execute(
    email: string,
    pass: string,
  ): Promise<{
    accessToken: string;
  }> {
    try {
      await this.authService.signIn(email, pass);
    } catch (error) {
      throw new UnauthorizedException(error); // TODO: return error code
    }

    try {
      const accessToken = await this.jwtService.signAsync({
        email: email,
      });

      return {
        accessToken,
      };
    } catch (error) {
      throw new UnauthorizedException('Invalid credentials'); // TODO: return error code
    }
  }
}

results matching ""

    No results matching ""