complete login with password controller and services
This commit is contained in:
11
src/helpers/cookies/jwt/encode/index.ts
Normal file
11
src/helpers/cookies/jwt/encode/index.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
|
||||||
|
export const jwtEncode = (payload: any) => {
|
||||||
|
const tokenLifetime = Number(process.env.SESSION_EXPIRE!);
|
||||||
|
const jwtSecret = process.env.JWT_SECRET!;
|
||||||
|
const jwtToken = jwt.sign(payload, jwtSecret, {
|
||||||
|
expiresIn: tokenLifetime,
|
||||||
|
});
|
||||||
|
|
||||||
|
return jwtToken;
|
||||||
|
};
|
||||||
@ -1,9 +1,13 @@
|
|||||||
import { Context } from "elysia";
|
import { Context } from "elysia";
|
||||||
import { loginWithPasswordService } from "../services/loginWithPassword.service";
|
import { loginWithPasswordService } from "../services/loginWithPassword.service";
|
||||||
import { loginWithPasswordSchema } from "../auth.schema";
|
import { loginWithPasswordSchema } from "../auth.schema";
|
||||||
import { returnErrorResponse } from "../../../helpers/callback/httpResponse";
|
import {
|
||||||
|
returnErrorResponse,
|
||||||
|
returnReadResponse,
|
||||||
|
} from "../../../helpers/callback/httpResponse";
|
||||||
import { LoginWithPasswordRequest } from "../auth.types";
|
import { LoginWithPasswordRequest } from "../auth.types";
|
||||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
|
import { getUserHeaderInformation } from "../../../helpers/cookies/userHeader/getUserHeaderInformation";
|
||||||
|
|
||||||
export const loginWithPassword = async (
|
export const loginWithPassword = async (
|
||||||
ctx: Context & { body: LoginWithPasswordRequest }
|
ctx: Context & { body: LoginWithPasswordRequest }
|
||||||
@ -12,9 +16,20 @@ export const loginWithPassword = async (
|
|||||||
if (error || !ctx.body)
|
if (error || !ctx.body)
|
||||||
return returnErrorResponse(ctx.set, 400, "Invalid user input", error);
|
return returnErrorResponse(ctx.set, 400, "Invalid user input", error);
|
||||||
|
|
||||||
|
const userHeaderInfo = getUserHeaderInformation(ctx);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await loginWithPasswordService(ctx.body);
|
const processAuth = await loginWithPasswordService(
|
||||||
return result;
|
ctx.body,
|
||||||
|
userHeaderInfo
|
||||||
|
);
|
||||||
|
|
||||||
|
return returnReadResponse(
|
||||||
|
ctx.set,
|
||||||
|
200,
|
||||||
|
"Autentication Success",
|
||||||
|
processAuth
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return mainErrorHandler(ctx.set, error);
|
return mainErrorHandler(ctx.set, error);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,9 +2,14 @@ import bcrypt from "bcrypt";
|
|||||||
import { findUserByEmailOrUsernameService } from "../../user/services/findUserByEmailOrUsername.service";
|
import { findUserByEmailOrUsernameService } from "../../user/services/findUserByEmailOrUsername.service";
|
||||||
import { LoginWithPasswordRequest } from "../auth.types";
|
import { LoginWithPasswordRequest } from "../auth.types";
|
||||||
import { AppError } from "../../../helpers/error/instances/app";
|
import { AppError } from "../../../helpers/error/instances/app";
|
||||||
|
import { UserHeaderInformation } from "../../../helpers/cookies/userHeader/getUserHeaderInformation/types";
|
||||||
|
import { createUserSessionService } from "../../userSession/services/createUserSession.service";
|
||||||
|
import { jwtEncode } from "../../../helpers/cookies/jwt/encode";
|
||||||
|
import { returnReadResponse } from "../../../helpers/callback/httpResponse";
|
||||||
|
|
||||||
export const loginWithPasswordService = async (
|
export const loginWithPasswordService = async (
|
||||||
request: LoginWithPasswordRequest
|
request: LoginWithPasswordRequest,
|
||||||
|
userHeaderInfo: UserHeaderInformation
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
// search for user data using an identifier (username or email)
|
// search for user data using an identifier (username or email)
|
||||||
@ -14,7 +19,14 @@ export const loginWithPasswordService = async (
|
|||||||
if (!(await bcrypt.compare(request.password, userData.password)))
|
if (!(await bcrypt.compare(request.password, userData.password)))
|
||||||
throw new AppError(401, "Password incorrect");
|
throw new AppError(401, "Password incorrect");
|
||||||
|
|
||||||
return userData;
|
const userSession = await createUserSessionService({
|
||||||
|
userId: userData.id,
|
||||||
|
userHeaderInformation: userHeaderInfo,
|
||||||
|
});
|
||||||
|
|
||||||
|
const jwtToken = jwtEncode(userSession);
|
||||||
|
|
||||||
|
return jwtToken;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import {
|
|||||||
returnWriteResponse,
|
returnWriteResponse,
|
||||||
} from "../../../helpers/callback/httpResponse";
|
} from "../../../helpers/callback/httpResponse";
|
||||||
import { createUserRoleService } from "../services/createUserRole.service";
|
import { createUserRoleService } from "../services/createUserRole.service";
|
||||||
import { JWTDecodeToken } from "../../../helpers/cookies/jwt/decodeToken";
|
import { JWTDecodeToken } from "../../../helpers/cookies/jwt/decode";
|
||||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
import { createUserRoleSchema } from "../schemas/createUserRole.schema";
|
import { createUserRoleSchema } from "../schemas/createUserRole.schema";
|
||||||
|
|
||||||
|
|||||||
@ -17,6 +17,12 @@ export const createUserSessionRepo = async (
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
omit: {
|
||||||
|
lastOnline: true,
|
||||||
|
deletedAt: true,
|
||||||
|
createdAt: true,
|
||||||
|
updatedAt: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
return newUserRole;
|
return newUserRole;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user