feat: implement backend auth and fix pet profile payload

This commit is contained in:
parsa committed 2026-06-12 22:37:35 +03:30
1 parent 82ab454647
commit 6027e635db
13 files changed
+493 -114

No files matched your search

+69
View File
@@ -4,6 +4,7 @@ import { PrismaService } from '../prisma/prisma.service';
import { RedisService } from '../redis/redis.service';
import { SendOtpDto } from './dto/send-otp.dto';
import { VerifyOtpDto } from './dto/verify-otp.dto';
import * as bcrypt from 'bcryptjs';
@Injectable()
export class AuthService {
@@ -64,6 +65,74 @@ export class AuthService {
};
}
async register(registerDto: any) {
const { firstName, lastName, email, mobile, password } = registerDto;
const existingUser = await this.prisma.user.findUnique({ where: { mobile } });
if (existingUser) {
throw new BadRequestException({ message: 'کاربری با این شماره موبایل قبلا ثبت نام کرده است', error: 'MOBILE_EXISTS' });
}
if (email) {
const existingEmail = await this.prisma.user.findUnique({ where: { email } });
if (existingEmail) {
throw new BadRequestException({ message: 'کاربری با این ایمیل قبلا ثبت نام کرده است', error: 'EMAIL_EXISTS' });
}
}
const hashedPassword = await bcrypt.hash(password, 10);
const user = await this.prisma.user.create({
data: {
firstName,
lastName,
email: email || null,
mobile,
password: hashedPassword,
},
});
const payload = { sub: user.id, phoneNumber: user.mobile };
const accessToken = this.jwtService.sign(payload);
return {
success: true,
data: {
user,
accessToken,
},
};
}
async login(loginDto: any) {
const { mobile, password } = loginDto;
const user = await this.prisma.user.findUnique({ where: { mobile } });
if (!user) {
throw new BadRequestException({ message: 'نام کاربری یا رمز عبور اشتباه است', error: 'INVALID_CREDENTIALS' });
}
if (!user.password) {
throw new BadRequestException({ message: 'شما با رمز عبور ثبت نام نکرده‌اید. لطفاً با موبایل وارد شوید', error: 'NO_PASSWORD' });
}
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) {
throw new BadRequestException({ message: 'نام کاربری یا رمز عبور اشتباه است', error: 'INVALID_CREDENTIALS' });
}
const payload = { sub: user.id, phoneNumber: user.mobile };
const accessToken = this.jwtService.sign(payload);
return {
success: true,
data: {
user,
accessToken,
},
};
}
async adminLogin(body: any) {
if (body.email === 'admin@canina-iran.com' && body.password === 'admin123') {
const payload = { sub: '12345678-1234-1234-1234-123456789012', email: body.email, role: 'Admin' };