first commit
This commit is contained in:
74
src/modules/auth/controller/auth.controller.ts
Normal file
74
src/modules/auth/controller/auth.controller.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { NextFunction } from "express";
|
||||
import { Controller } from "../../../core/controller/main.controller";
|
||||
import { ServerResponse } from "../../../core/types";
|
||||
import AuthService from "../service/auth.service";
|
||||
import { TOKEN_NAME } from "../../../core/constant";
|
||||
import { GlobalErrorMessages } from "../../../core/messages/errors";
|
||||
|
||||
class AuthControllerClass extends Controller {
|
||||
#service;
|
||||
constructor() {
|
||||
super();
|
||||
this.#service = AuthService;
|
||||
}
|
||||
|
||||
// login function for users that controlling this form
|
||||
async userLogin(req: any, res: ServerResponse, next: NextFunction) {
|
||||
try {
|
||||
const data = await this.#service.usersLogin(
|
||||
req?.body?.email,
|
||||
req?.body?.password,
|
||||
);
|
||||
res.cookie(TOKEN_NAME, data.token, {
|
||||
httpOnly: true,
|
||||
secure: false,
|
||||
sameSite: "lax",
|
||||
maxAge: 24 * 60 * 60 * 1000,
|
||||
});
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
data,
|
||||
message: "با موفقيت وارد شديد",
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
async applicantLogin(req: any, res: ServerResponse, next: NextFunction) {
|
||||
try {
|
||||
const data = await this.#service.applicantLogin(
|
||||
req?.body?.nationalCode,
|
||||
);
|
||||
res.cookie(TOKEN_NAME, data.token, {
|
||||
httpOnly: true,
|
||||
secure: false,
|
||||
sameSite: "lax",
|
||||
maxAge: 24 * 60 * 60 * 1000,
|
||||
});
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
data,
|
||||
message: "با موفقيت وارد شديد",
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
async userLogout(req: any, res: ServerResponse, next: NextFunction) {
|
||||
try {
|
||||
res.clearCookie(TOKEN_NAME);
|
||||
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
data: {},
|
||||
message: "Ok",
|
||||
});
|
||||
} catch (error) {
|
||||
next(GlobalErrorMessages.server.internal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const AuthController = new AuthControllerClass();
|
||||
|
||||
export default AuthController;
|
||||
14
src/modules/auth/messages/auth.messages.ts
Normal file
14
src/modules/auth/messages/auth.messages.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
export const authErrorMessages = Object.freeze({
|
||||
notFound:{
|
||||
user:"كاربر يافت نشد",
|
||||
applicant:"متقاضي يافت نشد"
|
||||
},
|
||||
dosentMatch :{
|
||||
email:"ايميل اشتباه است",
|
||||
password:"پسورد اشتباه است",
|
||||
},
|
||||
login:{
|
||||
invalidData:'ايميل و يا رمز عبور اشتباه است'
|
||||
},
|
||||
logout:'خروج از حساب با خطا مواجه شده است'
|
||||
})
|
||||
13
src/modules/auth/router/auth.routes.ts
Normal file
13
src/modules/auth/router/auth.routes.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Router } from "express";
|
||||
import AuthController from "../controller/auth.controller";
|
||||
|
||||
const AuthRouter = Router();
|
||||
|
||||
// authentication applicants
|
||||
AuthRouter.post("/applicant/login", AuthController.applicantLogin);
|
||||
// AuthRouter.post('/applicant/logout',AuthController.applicantLogout)
|
||||
|
||||
// authentication users
|
||||
AuthRouter.post("/user/login", AuthController.userLogin);
|
||||
AuthRouter.post("/user/logout", AuthController.userLogout);
|
||||
export default AuthRouter;
|
||||
84
src/modules/auth/service/auth.service.ts
Normal file
84
src/modules/auth/service/auth.service.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import createHttpError from "http-errors";
|
||||
import { Controller } from "../../../core/controller/main.controller";
|
||||
import { User } from "../../../models/User";
|
||||
import bcrypt from "bcryptjs";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { Role } from "../../../models/Role";
|
||||
import { Applicant } from "../../../models/Applicant";
|
||||
import { Identity } from "../../../models/Identity";
|
||||
import { authErrorMessages } from "../messages/auth.messages";
|
||||
class AuthServiceClass extends Controller {
|
||||
async usersLogin(email: string, password: string) {
|
||||
try {
|
||||
// ۱. پیدا کردن کاربر به همراه نقش
|
||||
const user = await User.findOne({
|
||||
where: { email },
|
||||
include: [
|
||||
{
|
||||
model: Role,
|
||||
as: "role",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!user)
|
||||
throw new createHttpError.Unauthorized("ایمیل یا رمز عبور اشتباه است.");
|
||||
|
||||
// ۲. چک کردن پسورد
|
||||
const isMatch = await bcrypt.compare(password, user.password);
|
||||
if (!isMatch)
|
||||
throw new createHttpError.Unauthorized("ایمیل یا رمز عبور اشتباه است.");
|
||||
|
||||
// ۳. صدور توکن
|
||||
const token = jwt.sign(
|
||||
{ userId: user.id },
|
||||
process.env.JWT_SECRET || "secret",
|
||||
{ expiresIn: "24h" }, // طول عمر توکن
|
||||
);
|
||||
|
||||
return {
|
||||
token,
|
||||
user: { id: user.id, fullname: user.fullname, role: user },
|
||||
};
|
||||
} catch (err) {
|
||||
throw new createHttpError.InternalServerError("خطای سرور");
|
||||
}
|
||||
}
|
||||
|
||||
async applicantLogin(nationalCode: string) {
|
||||
try {
|
||||
const identity = await Identity.findOne({
|
||||
where: { nationalCode },
|
||||
include: [
|
||||
{
|
||||
model: Applicant,
|
||||
as: "applicant",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!identity?.applicantId) {
|
||||
throw new createHttpError.NotFound(
|
||||
authErrorMessages.notFound.applicant,
|
||||
);
|
||||
}
|
||||
|
||||
const token = jwt.sign(
|
||||
{ userId: identity.applicantId },
|
||||
process.env.JWT_SECRET || "secret",
|
||||
{ expiresIn: "24h" }, // طول عمر توکن
|
||||
);
|
||||
|
||||
return {
|
||||
token,
|
||||
applicant: { id: identity.applicantId, fullname: `${identity.firstName} ${identity.lastName}`, role: identity },
|
||||
};
|
||||
} catch (error) {
|
||||
throw new createHttpError.InternalServerError("خطای سرور");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const AuthService = new AuthServiceClass();
|
||||
|
||||
export default AuthService;
|
||||
Reference in New Issue
Block a user