Cài đặt thành công tính năng người dùng đăng nhập

This commit is contained in:
2026-06-13 17:57:00 +07:00
parent f38d7e01a1
commit ea5799ffb5
15 changed files with 176 additions and 21 deletions
+21 -1
View File
@@ -1,5 +1,5 @@
import { NestFactory } from '@nestjs/core';
import { Module, Controller, Get, Post, Body, Param, ParseIntPipe, NotFoundException, BadRequestException } from '@nestjs/common';
import { Module, Controller, Get, Post, Body, Param, ParseIntPipe, NotFoundException, BadRequestException, UnauthorizedException } from '@nestjs/common';
import { PrismaService } from './prisma.service.js';
import 'dotenv/config';
import * as bcrypt from 'bcrypt';
@@ -23,6 +23,26 @@ class AuthController {
return { isInitialSetup: userCount === 0 };
}
@Post('login')
async login(@Body() body: any) {
const { email, password } = body;
const user = await this.prisma.user.findUnique({ where: { email } });
if (!user || !(await bcrypt.compare(password, user.passwordHash))) {
throw new UnauthorizedException('Email hoặc mật khẩu không chính xác');
}
return {
access_token: 'simulated-jwt-token',
user: {
id: user.id,
email: user.email,
name: user.name,
isAdmin: user.isAdmin,
},
};
}
@Post('signup')
async signup(@Body() body: any) {
const { email, password, name } = body;