Move cookie check outside services
This commit is contained in:
parent
cb3969ac40
commit
a9728d74f7
|
|
@ -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<ServerResponseAction<Awaited<ReturnType<typeof getMyDetail>>>> {
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -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<void> {
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user