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 ServerResponseAction from "@/modules/dashboard/types/ServerResponseAction";
|
||||||
import handleCatch from "@/modules/dashboard/utils/handleCatch";
|
import handleCatch from "@/modules/dashboard/utils/handleCatch";
|
||||||
import "server-only";
|
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.
|
* 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>>>> {
|
export default async function getMyDetailAction(): Promise<ServerResponseAction<Awaited<ReturnType<typeof getMyDetail>>>> {
|
||||||
try {
|
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.
|
// Attempt to fetch and return the user's details.
|
||||||
const userDetails = await getMyDetail();
|
const userDetails = await getMyDetail(token.value);
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: userDetails,
|
data: userDetails,
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import getMyDetail from "../services/getMyDetail";
|
import getMyDetail from "../services/getMyDetail";
|
||||||
|
import { cookies } from "next/headers";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enforces a guest-only access policy by redirecting authenticated users to the dashboard.
|
* 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.
|
* @returns A promise that resolves when the operation completes. The function itself does not return a value.
|
||||||
*/
|
*/
|
||||||
export default async function guestOnly(): Promise<void> {
|
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 an authenticated user is detected, redirect them to the dashboard.
|
||||||
if (user) {
|
if (user) {
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,22 @@
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import getUserFromToken from "../utils/getUserFromToken";
|
import getUserFromToken from "../utils/getUserFromToken";
|
||||||
|
import AuthError from "../error/AuthError";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the details of the currently authenticated user based on the JWT token.
|
* 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.
|
* 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.
|
* 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.
|
* @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() {
|
export default async function getMyDetail(token?: string) {
|
||||||
const token = cookies().get("token");
|
if (!token)
|
||||||
|
throw new AuthError({
|
||||||
|
errorCode: "INVALID_JWT_TOKEN",
|
||||||
|
message: "You are not authenticated",
|
||||||
|
});
|
||||||
|
|
||||||
// Return null if token is not present
|
const user = await getUserFromToken(token);
|
||||||
if (!token) return null;
|
|
||||||
|
|
||||||
const user = await getUserFromToken(token.value);
|
|
||||||
|
|
||||||
// Return null if user is not found
|
// Return null if user is not found
|
||||||
if (!user) return null;
|
if (!user) return null;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user