finish user login via password
This commit is contained in:
15
src/helpers/http/userHeader/cookies/setCookies.ts
Normal file
15
src/helpers/http/userHeader/cookies/setCookies.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { serialize } from "cookie";
|
||||||
|
|
||||||
|
export const setCookie = async (set: any, payload: string) => {
|
||||||
|
const cookieLifetime = Number(process.env.SESSION_EXPIRE!);
|
||||||
|
const serializedCookie = serialize("auth_token", payload, {
|
||||||
|
httpOnly: true,
|
||||||
|
secure: true,
|
||||||
|
sameSite: "strict",
|
||||||
|
maxAge: cookieLifetime,
|
||||||
|
path: "/",
|
||||||
|
});
|
||||||
|
|
||||||
|
set.headers["set-cookie"] = serializedCookie;
|
||||||
|
return serializedCookie;
|
||||||
|
};
|
||||||
@ -3,11 +3,12 @@ import { loginWithPasswordService } from "../services/loginWithPassword.service"
|
|||||||
import { loginWithPasswordSchema } from "../auth.schema";
|
import { loginWithPasswordSchema } from "../auth.schema";
|
||||||
import {
|
import {
|
||||||
returnErrorResponse,
|
returnErrorResponse,
|
||||||
returnReadResponse,
|
returnWriteResponse,
|
||||||
} from "../../../helpers/callback/httpResponse";
|
} 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/http/userHeader/getUserHeaderInformation";
|
import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation";
|
||||||
|
import { setCookie } from "../../../helpers/http/userHeader/cookies/setCookies";
|
||||||
|
|
||||||
export const loginWithPassword = async (
|
export const loginWithPassword = async (
|
||||||
ctx: Context & { body: LoginWithPasswordRequest }
|
ctx: Context & { body: LoginWithPasswordRequest }
|
||||||
@ -19,17 +20,10 @@ export const loginWithPassword = async (
|
|||||||
const userHeaderInfo = getUserHeaderInformation(ctx);
|
const userHeaderInfo = getUserHeaderInformation(ctx);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const processAuth = await loginWithPasswordService(
|
const jwtToken = await loginWithPasswordService(ctx.body, userHeaderInfo);
|
||||||
ctx.body,
|
|
||||||
userHeaderInfo
|
|
||||||
);
|
|
||||||
|
|
||||||
return returnReadResponse(
|
const cookie = setCookie(ctx.set, jwtToken);
|
||||||
ctx.set,
|
return returnWriteResponse(ctx.set, 200, "Autentication Success", cookie);
|
||||||
200,
|
|
||||||
"Autentication Success",
|
|
||||||
processAuth
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return mainErrorHandler(ctx.set, error);
|
return mainErrorHandler(ctx.set, error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user