feat: chuẩn bị backend cho tính năng anonymous user upload ảnh không cần đăng nhập

This commit is contained in:
2026-06-19 08:46:59 +07:00
parent 6f2dd0fc01
commit ef11309399
6 changed files with 125 additions and 5 deletions
+55
View File
@@ -262,6 +262,61 @@ class AuthController {
@Inject(CACHE_MANAGER) private cacheManager: Cache
) {}
@Post('convert-guest')
async convertGuestToOfficial(@Body() body: any) {
const { guestId, email, password, name } = body;
if (!guestId || !email || !password) {
throw new BadRequestException('Vui lòng cung cấp đủ guestId, email và mật khẩu.');
}
// 1. Kiểm tra xem email đã tồn tại với một tài khoản chính thức khác chưa
const existingOfficialUser = await this.prisma.user.findFirst({
where: {
email: email,
isAnonymous: false,
},
});
if (existingOfficialUser) {
throw new BadRequestException('Email này đã được một tài khoản khác sử dụng.');
}
// 2. Tìm tài khoản khách
const guestUser = await this.prisma.user.findUnique({
where: { id: guestId },
});
if (!guestUser || !guestUser.isAnonymous) {
throw new NotFoundException('Không tìm thấy tài khoản khách hoặc tài khoản này đã được chuyển đổi.');
}
// 3. Mã hóa mật khẩu và cập nhật người dùng
const passwordHash = await bcrypt.hash(password, 10);
const updatedUser = await this.prisma.user.update({
where: { id: guestId },
data: {
email: email,
passwordHash: passwordHash,
name: name || guestUser.name, // Cập nhật tên mới nếu có, nếu không giữ lại tên ẩn danh cũ
isAnonymous: false, // Đánh dấu đây là tài khoản chính thức
},
});
// 4. Tạo và trả về token để người dùng đăng nhập ngay lập tức
const payload = { email: updatedUser.email, sub: updatedUser.id };
return {
access_token: this.jwtService.sign(payload),
user: {
id: updatedUser.id,
email: updatedUser.email,
name: updatedUser.name,
isAdmin: updatedUser.isAdmin,
},
};
}
@Get('status')
async getStatus() {
const userCount = await this.prisma.user.count();