File

src/auth/auth.service.ts

Index

Methods

Constructor

constructor(userRepository: UserRepository, jwtService: JwtService)
Parameters :
Name Type Optional
userRepository UserRepository No
jwtService JwtService No

Methods

Async signIn
signIn(authCredentialDto: AuthCredentialDto)
Parameters :
Name Type Optional
authCredentialDto AuthCredentialDto No
Returns : Promise<literal type>
Async signUp
signUp(authCredentialDto: AuthCredentialDto)
Parameters :
Name Type Optional
authCredentialDto AuthCredentialDto No
Returns : Promise<void>
import { JwtPayload } from './jwt.payload.interface';
import { UserRepository } from './user.repository';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { AuthCredentialDto } from './dto/auth-credential.dto';
import { JwtService } from '@nestjs/jwt';

@Injectable()
export class AuthService {
    constructor(
        @InjectRepository(UserRepository)
        private userRepository: UserRepository,
        private jwtService: JwtService,
    ) {}

    async signUp(authCredentialDto: AuthCredentialDto): Promise<void> {
       return await this.userRepository.signUp(authCredentialDto);
    }

    async signIn(authCredentialDto: AuthCredentialDto): Promise<{ accessToken: string }> {
        const username = await this.userRepository.validateUserPassword(authCredentialDto);
        if (!username) {
          throw new UnauthorizedException('Invalid credentials');
        }

        const payload: JwtPayload = { username };
        const accessToken = await this.jwtService.sign(payload);

        return { accessToken };
    }
}

result-matching ""

    No results matching ""