Cài đặt thành công tính năng người dùng đăng nhập
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user