From a9728d74f7eef8299027ec923dc0f1cde9e69a0f Mon Sep 17 00:00:00 2001 From: sianida26 Date: Wed, 28 Feb 2024 09:29:14 +0700 Subject: [PATCH] Move cookie check outside services --- src/modules/auth/actions/getMyDetailAction.ts | 9 ++++++++- src/modules/auth/actions/guestOnly.ts | 7 ++++++- src/modules/auth/services/getMyDetail.ts | 16 +++++++++------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/modules/auth/actions/getMyDetailAction.ts b/src/modules/auth/actions/getMyDetailAction.ts index b30b264..66ef024 100644 --- a/src/modules/auth/actions/getMyDetailAction.ts +++ b/src/modules/auth/actions/getMyDetailAction.ts @@ -6,6 +6,8 @@ import BaseError from "@/core/error/BaseError"; import ServerResponseAction from "@/modules/dashboard/types/ServerResponseAction"; import handleCatch from "@/modules/dashboard/utils/handleCatch"; import "server-only"; +import { cookies } from "next/headers"; +import getUserFromToken from "../utils/getUserFromToken"; /** * Asynchronously retrieves the authenticated user's details from a server-side context in a Next.js application. @@ -17,8 +19,13 @@ import "server-only"; */ export default async function getMyDetailAction(): Promise>>> { try { + const token = cookies().get("token"); + + // Return null if token is not present + if (!token) throw new AuthError({errorCode: "INVALID_JWT_TOKEN"}); + // Attempt to fetch and return the user's details. - const userDetails = await getMyDetail(); + const userDetails = await getMyDetail(token.value); return { success: true, data: userDetails, diff --git a/src/modules/auth/actions/guestOnly.ts b/src/modules/auth/actions/guestOnly.ts index 9336aee..ad1535f 100644 --- a/src/modules/auth/actions/guestOnly.ts +++ b/src/modules/auth/actions/guestOnly.ts @@ -2,6 +2,7 @@ import { redirect } from "next/navigation"; import getMyDetail from "../services/getMyDetail"; +import { cookies } from "next/headers"; /** * Enforces a guest-only access policy by redirecting authenticated users to the dashboard. @@ -11,7 +12,11 @@ import getMyDetail from "../services/getMyDetail"; * @returns A promise that resolves when the operation completes. The function itself does not return a value. */ export default async function guestOnly(): Promise { - const user = await getMyDetail(); + const token = cookies().get("token"); + + if (!token) return; + + const user = await getMyDetail(token.value); // If an authenticated user is detected, redirect them to the dashboard. if (user) { diff --git a/src/modules/auth/services/getMyDetail.ts b/src/modules/auth/services/getMyDetail.ts index b7a9f4c..4b3b9d0 100644 --- a/src/modules/auth/services/getMyDetail.ts +++ b/src/modules/auth/services/getMyDetail.ts @@ -1,20 +1,22 @@ import { cookies } from "next/headers"; import getUserFromToken from "../utils/getUserFromToken"; +import AuthError from "../error/AuthError"; /** * Retrieves the details of the currently authenticated user based on the JWT token. * If the token is not present or the user cannot be found, it returns null. * Otherwise, it returns the user's name, email, and photo URL. - * + * * @returns An object containing the user's name, email, and photo URL, or null if the user cannot be authenticated. */ -export default async function getMyDetail() { - const token = cookies().get("token"); +export default async function getMyDetail(token?: string) { + if (!token) + throw new AuthError({ + errorCode: "INVALID_JWT_TOKEN", + message: "You are not authenticated", + }); - // Return null if token is not present - if (!token) return null; - - const user = await getUserFromToken(token.value); + const user = await getUserFromToken(token); // Return null if user is not found if (!user) return null;