Spaces:
Running
Running
Commit
·
a27d3dd
1
Parent(s):
6a7bf05
feat: admin auth
Browse files
src/helpers/jwt.helper.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
import jwt from "jsonwebtoken";
|
| 2 |
import { config } from "../configs/config";
|
|
|
|
| 3 |
|
| 4 |
export class JwtHelper {
|
| 5 |
-
static generateToken(payload:
|
| 6 |
return jwt.sign(payload, config.jwt.secret, {
|
| 7 |
expiresIn: config.jwt.expiresIn,
|
| 8 |
});
|
|
|
|
| 1 |
import jwt from "jsonwebtoken";
|
| 2 |
import { config } from "../configs/config";
|
| 3 |
+
import { IJwtLoginPayload } from "@common/interfaces/jwt-payload.interface";
|
| 4 |
|
| 5 |
export class JwtHelper {
|
| 6 |
+
static generateToken(payload: IJwtLoginPayload) {
|
| 7 |
return jwt.sign(payload, config.jwt.secret, {
|
| 8 |
expiresIn: config.jwt.expiresIn,
|
| 9 |
});
|
src/modules/console/modules/auth/controllers/auth.controller.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { asyncHandler } from "@helpers/async-handler";
|
| 2 |
+
import { serialize } from "@helpers/serialize";
|
| 3 |
+
import { bodyValidator } from "@helpers/validation.helper";
|
| 4 |
+
import { BaseController } from "@lib/controllers/controller.base";
|
| 5 |
+
import { Prefix } from "@lib/decorators/prefix.decorator";
|
| 6 |
+
import { JsonResponse } from "@lib/responses/json-response";
|
| 7 |
+
import { Request, Response } from "express";
|
| 8 |
+
import { loginValidationSchema } from "modules/users/modules/auth/validation/login.validation";
|
| 9 |
+
import { ConsoleAuthService } from "../services/auth.service";
|
| 10 |
+
import { AdminSerialization } from "modules/console/common/serializers/admin.serialization";
|
| 11 |
+
|
| 12 |
+
@Prefix("/console/auth")
|
| 13 |
+
export class ConsoleAuthController extends BaseController {
|
| 14 |
+
private authService = new ConsoleAuthService();
|
| 15 |
+
|
| 16 |
+
public setRoutes(): void {
|
| 17 |
+
this.router.post(
|
| 18 |
+
"/login",
|
| 19 |
+
bodyValidator(loginValidationSchema),
|
| 20 |
+
asyncHandler(this.login)
|
| 21 |
+
);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
login = async (req: Request, res: Response): Promise<Response> => {
|
| 25 |
+
const { admin, token } = await this.authService.login(req.body);
|
| 26 |
+
|
| 27 |
+
return JsonResponse.success(
|
| 28 |
+
{
|
| 29 |
+
data: { admin: serialize(admin, AdminSerialization), token },
|
| 30 |
+
},
|
| 31 |
+
res
|
| 32 |
+
);
|
| 33 |
+
};
|
| 34 |
+
}
|
src/modules/console/modules/auth/services/auth.service.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { CrudService } from "@lib/services/crud.service";
|
| 2 |
+
import { Admin } from "modules/console/common/models/admin.model";
|
| 3 |
+
import { ILogin } from "modules/users/modules/auth/validation/login.validation";
|
| 4 |
+
import bcrypt from "bcrypt";
|
| 5 |
+
import { HttpError } from "@lib/error-handling/http-error";
|
| 6 |
+
import { JwtHelper } from "@helpers/jwt.helper";
|
| 7 |
+
|
| 8 |
+
export class ConsoleAuthService extends CrudService(Admin) {
|
| 9 |
+
async login(loginRequest: ILogin) {
|
| 10 |
+
const admin = await this.findOneOrFail({ email: loginRequest.email });
|
| 11 |
+
const isPasswordCorrect = await bcrypt.compare(
|
| 12 |
+
loginRequest.password,
|
| 13 |
+
admin.password
|
| 14 |
+
);
|
| 15 |
+
if (!isPasswordCorrect) throw new HttpError(401, "Incorrect Password");
|
| 16 |
+
const token = JwtHelper.generateToken({
|
| 17 |
+
id: admin._id,
|
| 18 |
+
email: admin.email,
|
| 19 |
+
name: admin.name,
|
| 20 |
+
type: "admin",
|
| 21 |
+
role: admin.role,
|
| 22 |
+
});
|
| 23 |
+
return { admin: admin, token };
|
| 24 |
+
}
|
| 25 |
+
}
|
src/modules/users/modules/auth/services/users-auth.service.ts
CHANGED
|
@@ -18,7 +18,12 @@ export class UsersAuthService extends CrudService(userModel) {
|
|
| 18 |
user.password
|
| 19 |
);
|
| 20 |
if (!isPasswordCorrect) throw new HttpError(401, "Incorrect Password");
|
| 21 |
-
const token = JwtHelper.generateToken({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return { user, token };
|
| 23 |
}
|
| 24 |
}
|
|
|
|
| 18 |
user.password
|
| 19 |
);
|
| 20 |
if (!isPasswordCorrect) throw new HttpError(401, "Incorrect Password");
|
| 21 |
+
const token = JwtHelper.generateToken({
|
| 22 |
+
id: user._id,
|
| 23 |
+
email: user.email,
|
| 24 |
+
name: user.name,
|
| 25 |
+
type: "user",
|
| 26 |
+
});
|
| 27 |
return { user, token };
|
| 28 |
}
|
| 29 |
}
|
src/modules/users/modules/auth/services/users.service.ts
CHANGED
|
@@ -18,7 +18,12 @@ export class UsersAuthService extends CrudService(userModel) {
|
|
| 18 |
user.password
|
| 19 |
);
|
| 20 |
if (!isPasswordCorrect) throw new HttpError(401, "Incorrect Password");
|
| 21 |
-
const token = JwtHelper.generateToken({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return { user, token };
|
| 23 |
}
|
| 24 |
}
|
|
|
|
| 18 |
user.password
|
| 19 |
);
|
| 20 |
if (!isPasswordCorrect) throw new HttpError(401, "Incorrect Password");
|
| 21 |
+
const token = JwtHelper.generateToken({
|
| 22 |
+
id: user._id,
|
| 23 |
+
email: user.email,
|
| 24 |
+
name: user.name,
|
| 25 |
+
type: "user",
|
| 26 |
+
});
|
| 27 |
return { user, token };
|
| 28 |
}
|
| 29 |
}
|