feat: backend API NestJS + Prisma + JWT auth + todos los módulos

This commit is contained in:
Lizandro Guarnizo
2026-06-01 21:23:04 -05:00
parent b31f778b3b
commit 3c5d81603a
69 changed files with 35734 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
import { Injectable, UnauthorizedException, ConflictException, BadRequestException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import * as bcrypt from 'bcryptjs';
import { PrismaService } from '../prisma/prisma.service';
@Injectable()
export class AuthService {
constructor(
private prisma: PrismaService,
private jwt: JwtService,
) {}
async register(email: string, password: string, name: string) {
const existing = await this.prisma.users.findUnique({ where: { email } });
if (existing) throw new ConflictException('Email ya registrado');
const password_hash = await bcrypt.hash(password, 10);
const user = await this.prisma.users.create({
data: { email, password_hash, name, is_email_verified: true },
});
return this.generateToken(user);
}
async login(email: string, password: string) {
const user = await this.prisma.users.findUnique({ where: { email } });
if (!user || !user.password_hash) throw new UnauthorizedException('Credenciales inválidas');
const valid = await bcrypt.compare(password, user.password_hash);
if (!valid) throw new UnauthorizedException('Credenciales inválidas');
return this.generateToken(user);
}
async loginOrCreateByPhone(phone: string, name?: string) {
let user = await this.prisma.users.findUnique({ where: { phone } });
if (!user) {
user = await this.prisma.users.create({
data: { phone, name: name || phone, is_phone_verified: true },
});
}
return this.generateToken(user);
}
async me(userId: string) {
return this.prisma.users.findUnique({
where: { id: userId },
include: {
professionals: {
include: { schedules: true, payment_methods: true, specializations: true },
},
reputations: true,
},
});
}
private generateToken(user: any) {
const payload = { sub: user.id, email: user.email, phone: user.phone };
return {
access_token: this.jwt.sign(payload),
user: {
id: user.id,
email: user.email,
phone: user.phone,
name: user.name,
},
};
}
}