Cài đặt thành công tính năng người dùng đăng nhập
This commit is contained in:
Vendored
+24
-1
@@ -11,7 +11,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
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';
|
||||
@@ -38,6 +38,22 @@ let AuthController = class AuthController {
|
||||
console.log(`[Status Check] Users found: ${userCount}`);
|
||||
return { isInitialSetup: userCount === 0 };
|
||||
}
|
||||
async login(body) {
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
async signup(body) {
|
||||
const { email, password, name } = body;
|
||||
const existingUser = await this.prisma.user.findUnique({ where: { email } });
|
||||
@@ -58,6 +74,13 @@ __decorate([
|
||||
__metadata("design:paramtypes", []),
|
||||
__metadata("design:returntype", Promise)
|
||||
], AuthController.prototype, "getStatus", null);
|
||||
__decorate([
|
||||
Post('login'),
|
||||
__param(0, Body()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], AuthController.prototype, "login", null);
|
||||
__decorate([
|
||||
Post('signup'),
|
||||
__param(0, Body()),
|
||||
|
||||
Reference in New Issue
Block a user