@@ -12,7 +12,10 @@ export const enum AuthMessage {
|
||||
PASSWORD_UPDATE_SUCCESS = "رمز عبور شما تغییر کرد",
|
||||
INVALID_OTP = "کد صحیح نمیباشد",
|
||||
PASSWORD_MISMATCH = "رمز عبور یکسان نیست",
|
||||
INVALID_PASSWORD = "رمز عبور اشتباه است",
|
||||
INVALID_PASSWORD = "ایمیل یا رمز عبور اشتباه است",
|
||||
EmailNotEmpty = "ایمیل نمیتواند خالی باشد.",
|
||||
IncorrectEmail = "ایمیل صحیح نیست",
|
||||
PasswordNotEmpty = "پسورد نمیتواند خالی باشد.",
|
||||
USER_NOT_FOUND = "کاربری با این شماره وجود ندارد",
|
||||
USER_EXISTS = "با این شماره قبلا ثبت نام شده است",
|
||||
USER_REGISTER_SUCCESS = "ثبت نام موفقیت آمیز بود",
|
||||
@@ -26,3 +29,7 @@ export const enum AuthMessage {
|
||||
Permission_NOT_FOUND = "دسترسی یافت نشد",
|
||||
ADMIN_NOT_FOUND = "ادمین یافت نشد",
|
||||
}
|
||||
|
||||
export const enum UserMessage {
|
||||
USER_NOT_FOUND = "کاربری با این مشخصات یافت نشد",
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { IsEmail, IsNotEmpty } from "@nestjs/class-validator";
|
||||
import { AuthMessage } from "../../../common/enums/message.enum";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
|
||||
export class LoginPasswordDTO {
|
||||
@IsNotEmpty({ message: AuthMessage.EmailNotEmpty })
|
||||
@IsEmail(undefined, { message: AuthMessage.IncorrectEmail })
|
||||
@ApiProperty({ description: "email of user", default: "user@abrclick.ir" })
|
||||
email: string;
|
||||
|
||||
@IsNotEmpty({ message: AuthMessage.PasswordNotEmpty })
|
||||
@ApiProperty({ type: "string", description: "password user", example: "@1234" })
|
||||
password: string;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { Controller } from "@nestjs/common";
|
||||
|
||||
@Controller("auth")
|
||||
export class AuthController {}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
|
||||
import { AuthController } from "./auth.controller";
|
||||
import { AuthService } from "./auth.service";
|
||||
import { UtilsModule } from "../utils/utils.module";
|
||||
|
||||
@Module({
|
||||
imports: [UtilsModule],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService],
|
||||
})
|
||||
export class AuthModule {}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { UsersService } from "../users/providers/users.service";
|
||||
import { LoginPasswordDTO } from "./DTOs/loginPassword.dto";
|
||||
import { PasswordService } from "../utils/providers/password.service";
|
||||
import { AuthMessage, UserMessage } from "../../common/enums/message.enum";
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(
|
||||
private readonly userSerivce: UsersService,
|
||||
private readonly passwordService: PasswordService,
|
||||
) {}
|
||||
|
||||
async loginWithPassword(loginDto: LoginPasswordDTO) {
|
||||
const { email, password } = loginDto;
|
||||
|
||||
const user = await this.userSerivce.findOneWithEmail(email);
|
||||
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
||||
|
||||
const passCompare = await this.passwordService.comparePassword(password, user.password);
|
||||
if (!passCompare) throw new BadRequestException(AuthMessage.INVALID_PASSWORD);
|
||||
}
|
||||
}
|
||||
@@ -9,4 +9,10 @@ export class UserRepository extends Repository<User> {
|
||||
constructor(@InjectRepository(User) userRepository: Repository<User>) {
|
||||
super(userRepository.target, userRepository.manager, userRepository.queryRunner);
|
||||
}
|
||||
|
||||
async findOneWithEmail(email: string): Promise<User | null> {
|
||||
return await this.findOneBy({
|
||||
email,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
|
||||
// import { UserRepository } from "./users.repository";
|
||||
import { UserRepository } from "./users.repository";
|
||||
import { User } from "../entities/user.entity";
|
||||
import { UserMessage } from "../../../common/enums/message.enum";
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
// constructor(private userRepository: UserRepository) {}
|
||||
constructor(private userRepository: UserRepository) {}
|
||||
|
||||
async findOneWithEmail(email: string): Promise<User> {
|
||||
const user = await this.userRepository.findOneWithEmail(email);
|
||||
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user