Spaces:
Running
Running
Merge pull request #42 from Modarb-Ai-Trainer:MoWafy001/issue36
Browse files
src/modules/console/modules/auth/services/auth.service.ts
CHANGED
@@ -7,12 +7,13 @@ import { JwtHelper } from "@helpers/jwt.helper";
|
|
7 |
|
8 |
export class ConsoleAuthService extends CrudService(Admin) {
|
9 |
async login(loginRequest: ILogin) {
|
10 |
-
const admin = await this.
|
|
|
11 |
const isPasswordCorrect = await bcrypt.compare(
|
12 |
loginRequest.password,
|
13 |
admin.password
|
14 |
);
|
15 |
-
if (!isPasswordCorrect) throw new HttpError(401, "
|
16 |
const token = JwtHelper.generateToken({
|
17 |
id: admin._id,
|
18 |
email: admin.email,
|
|
|
7 |
|
8 |
export class ConsoleAuthService extends CrudService(Admin) {
|
9 |
async login(loginRequest: ILogin) {
|
10 |
+
const admin = await this.findOne({ email: loginRequest.email });
|
11 |
+
if (!admin) throw new HttpError(401, "Invalid Credentials");
|
12 |
const isPasswordCorrect = await bcrypt.compare(
|
13 |
loginRequest.password,
|
14 |
admin.password
|
15 |
);
|
16 |
+
if (!isPasswordCorrect) throw new HttpError(401, "Invalid Credentials");
|
17 |
const token = JwtHelper.generateToken({
|
18 |
id: admin._id,
|
19 |
email: admin.email,
|
src/modules/users/modules/auth/controllers/auth.controller.ts
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
import { UsersAuthService } from "../services/users.service";
|
2 |
import { loginValidationSchema } from "../validation/login.validation";
|
3 |
import { Request, Response } from "express";
|
4 |
import { JsonResponse } from "@lib/responses/json-response";
|
@@ -12,6 +11,7 @@ import { BaseController } from "@lib/controllers/controller.base";
|
|
12 |
import { Prefix } from "@lib/decorators/prefix.decorator";
|
13 |
import { serialize } from "@helpers/serialize";
|
14 |
import { UserSerialization } from "@common/serializers/user.serialization";
|
|
|
15 |
|
16 |
@Prefix("/users/auth")
|
17 |
export class UsersAuthController extends BaseController {
|
|
|
|
|
1 |
import { loginValidationSchema } from "../validation/login.validation";
|
2 |
import { Request, Response } from "express";
|
3 |
import { JsonResponse } from "@lib/responses/json-response";
|
|
|
11 |
import { Prefix } from "@lib/decorators/prefix.decorator";
|
12 |
import { serialize } from "@helpers/serialize";
|
13 |
import { UserSerialization } from "@common/serializers/user.serialization";
|
14 |
+
import { UsersAuthService } from "../services/users-auth.service";
|
15 |
|
16 |
@Prefix("/users/auth")
|
17 |
export class UsersAuthController extends BaseController {
|
src/modules/users/modules/auth/services/users-auth.service.ts
CHANGED
@@ -12,12 +12,14 @@ export class UsersAuthService extends CrudService(User) {
|
|
12 |
}
|
13 |
|
14 |
async login(loginRequest: ILogin) {
|
15 |
-
const user = await this.
|
|
|
|
|
16 |
const isPasswordCorrect = await bcrypt.compare(
|
17 |
loginRequest.password,
|
18 |
user.password
|
19 |
);
|
20 |
-
if (!isPasswordCorrect) throw new HttpError(401, "
|
21 |
const token = JwtHelper.generateToken({
|
22 |
id: user._id,
|
23 |
email: user.email,
|
|
|
12 |
}
|
13 |
|
14 |
async login(loginRequest: ILogin) {
|
15 |
+
const user = await this.findOne({ email: loginRequest.email });
|
16 |
+
if (!user) throw new HttpError(401, "Invalid Credentials");
|
17 |
+
|
18 |
const isPasswordCorrect = await bcrypt.compare(
|
19 |
loginRequest.password,
|
20 |
user.password
|
21 |
);
|
22 |
+
if (!isPasswordCorrect) throw new HttpError(401, "Invalid Credentials");
|
23 |
const token = JwtHelper.generateToken({
|
24 |
id: user._id,
|
25 |
email: user.email,
|
src/modules/users/modules/auth/services/users.service.ts
DELETED
@@ -1,29 +0,0 @@
|
|
1 |
-
import bcrypt from "bcrypt";
|
2 |
-
import { ILogin } from "../validation/login.validation";
|
3 |
-
import { HttpError } from "@lib/error-handling/http-error";
|
4 |
-
import { JwtHelper } from "@helpers/jwt.helper";
|
5 |
-
import { User } from "@common/models/user.model";
|
6 |
-
import { IUserRegister } from "@common/validations/user-register.validation";
|
7 |
-
import { CrudService } from "@lib/services/crud.service";
|
8 |
-
|
9 |
-
export class UsersAuthService extends CrudService(User) {
|
10 |
-
async register(createParams: IUserRegister) {
|
11 |
-
return this.create(createParams);
|
12 |
-
}
|
13 |
-
|
14 |
-
async login(loginRequest: ILogin) {
|
15 |
-
const user = await this.findOneOrFail({ email: loginRequest.email });
|
16 |
-
const isPasswordCorrect = await bcrypt.compare(
|
17 |
-
loginRequest.password,
|
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 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|