first commit
This commit is contained in:
46
src/modules/auth/services/auth.service.ts
Normal file
46
src/modules/auth/services/auth.service.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import createHttpError from "http-errors";
|
||||
import { AuthMessages } from "../messages";
|
||||
import jwt from "jsonwebtoken";
|
||||
import dotenv from "dotenv";
|
||||
import User from "../../../models/User";
|
||||
import { JWT_SECRET } from "../../../core/constants";
|
||||
dotenv.config();
|
||||
|
||||
class AuthServiceClass {
|
||||
async login(username: string, password: string) {
|
||||
try {
|
||||
const user = await User.findOne({
|
||||
where: {
|
||||
nationalCode: username,
|
||||
mobile: password,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw createHttpError.Unauthorized(
|
||||
AuthMessages.error.login.incorrectData,
|
||||
);
|
||||
}
|
||||
|
||||
const token = jwt.sign(
|
||||
{
|
||||
id: user.id,
|
||||
roleId: user.roleId,
|
||||
},
|
||||
JWT_SECRET,
|
||||
{ expiresIn: "1d" },
|
||||
);
|
||||
|
||||
return token;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
throw createHttpError.InternalServerError(
|
||||
AuthMessages.error.login.loginFailed,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const AuthService = new AuthServiceClass();
|
||||
|
||||
export default AuthService;
|
||||
Reference in New Issue
Block a user