Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@nestjs/cli": "^11.0.0",
"@nestjs/schematics": "^11.0.0",
"@nestjs/testing": "^11.0.1",
"@types/bcrypt": "^5.0.2",
"@types/bip32": "^2.0.4",
"@types/cron": "^2.4.3",
"@types/express": "^5.0.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable, BadRequestException, UnauthorizedException } from '@nestjs/common';
import * as bcrypt from 'bcrypt';
import { JwtService } from '@nestjs/jwt';
import { PrismaService } from '../prisma/prisma.service';
import { RegisterMerchantDto } from './dto/register-merchant.dto';
import { LoginMerchantDto } from './dto/login-merchant.dto';
import { comparePassword, hashPassword } from './password.util';

@Injectable()
export class AuthService {
Expand All @@ -26,7 +26,7 @@ export class AuthService {
const existing = await this.prisma.merchant.findUnique({ where: { email: dto.email } });
if (existing) throw new BadRequestException('Email already in use');

const hash = await bcrypt.hash(dto.password, 12);
const hash = await hashPassword(dto.password);

const merchant = await this.prisma.merchant.create({
data: { email: dto.email, passwordHash: hash },
Expand All @@ -39,7 +39,7 @@ export class AuthService {
const merchant = await this.prisma.merchant.findUnique({ where: { email: dto.email } });
if (!merchant) throw new UnauthorizedException('Invalid credentials');

const ok = await bcrypt.compare(dto.password, merchant.passwordHash);
const ok = await comparePassword(dto.password, merchant.passwordHash);
if (!ok) throw new UnauthorizedException('Invalid credentials');

const payload = { merchant_id: merchant.id };
Expand Down
11 changes: 11 additions & 0 deletions apps/api/src/auth/password.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as bcrypt from 'bcrypt';

const SALT_ROUNDS = 12;

export function hashPassword(password: string): Promise<string> {
return bcrypt.hash(password, SALT_ROUNDS);
}

export function comparePassword(password: string, hash: string): Promise<boolean> {
return bcrypt.compare(password, hash);
}
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.