source-code/
portofolio-backend
Public
typescript35 lines1 KB
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { PrismaService } from '../prisma/prisma.service';
import * as argon2 from 'argon2';
@Injectable()
export class AuthService {
constructor(
private readonly prisma: PrismaService,
private readonly jwtService: JwtService,
) {}
/**
* Verifies user credentials and generates a JWT.
* Uses Argon2 for secure password verification.
* Throws an UnauthorizedException if the user is not found or the password does not match.
*/
async login(email: string, pass: string) {
const user = await this.prisma.user.findUnique({ where: { email } });
if (!user) {
throw new UnauthorizedException('Invalid credentials');
}
const isPasswordValid = await argon2.verify(user.password, pass);
if (!isPasswordValid) {
throw new UnauthorizedException('Invalid credentials');
}
const payload = { email: user.email, sub: user.id };
return {
access_token: this.jwtService.sign(payload),
};
}
}
About
Fullstack portfolio backend built with NestJS, Prisma, and PostgreSQL. Features JWT authentication, throttler rate limits, cache management, and automated GitHub repository synchronization.
linknre.codes
TypeScriptNestJSPostgreSQLPrisma