diff --git a/src/app/dashboard/layout.tsx b/src/app/dashboard/layout.tsx index 5ca4783..e8f1116 100644 --- a/src/app/dashboard/layout.tsx +++ b/src/app/dashboard/layout.tsx @@ -4,7 +4,7 @@ import Image from "next/image"; import React from "react"; import logo from "@/assets/logos/logo.png"; import DashboardLayout from "@/modules/dashboard/components/DashboardLayout"; -import getUser from "@/modules/auth/actions/getUser"; +import getUser from "@/modules/auth/actions/getMyDetailAction"; import { redirect } from "next/navigation"; import { Notifications } from "@mantine/notifications"; diff --git a/src/modules/auth/actions/getUser.ts b/src/modules/auth/actions/getMyDetailAction.ts similarity index 50% rename from src/modules/auth/actions/getUser.ts rename to src/modules/auth/actions/getMyDetailAction.ts index 7af6a47..fa7407e 100644 --- a/src/modules/auth/actions/getUser.ts +++ b/src/modules/auth/actions/getMyDetailAction.ts @@ -1,9 +1,11 @@ "use server"; -import { cookies } from "next/headers"; import "server-only"; -import getUserFromToken from "../utils/getUserFromToken"; import AuthError from "../error/AuthError"; +import getMyDetail from "../services/getMyDetail"; +import ServerResponseAction from "@/modules/dashboard/types/ServerResponseAction"; +import handleCatch from "@/modules/dashboard/utils/handleCatch"; +import BaseError from "@/core/error/BaseError"; /** * Retrieves the user details based on the JWT token from cookies. @@ -14,26 +16,27 @@ import AuthError from "../error/AuthError"; * @returns A promise that resolves to the user's details object or null if the user cannot be authenticated or an error occurs. * @throws an error if an unexpected error occurs during execution. */ -export default async function getUser() { +export default async function getMyDetailAction(): Promise< + ServerResponseAction>> +> { try { - const token = cookies().get("token"); - - if (!token) return null; - - const user = await getUserFromToken(token.value); - - if (!user) return null; - return { - name: user.name ?? "", - email: user.email ?? "", - photoUrl: user.photoProfile ?? null, + success: true, + data: await getMyDetail(), }; } catch (e: unknown) { - // Handle specific authentication errors gracefully - if (e instanceof AuthError && e.errorCode === "INVALID_JWT_TOKEN") { - return null; + if ( + e instanceof AuthError && + ["INVALID_JWT_TOKEN"].includes(e.errorCode) + ) { + return { + success: false, + error: new BaseError({ + errorCode: e.errorCode, + message: "You are not authenticated", + }), + }; } - throw e; + return handleCatch(e); } } diff --git a/src/modules/auth/actions/guestOnly.ts b/src/modules/auth/actions/guestOnly.ts index 86d5011..473e25f 100644 --- a/src/modules/auth/actions/guestOnly.ts +++ b/src/modules/auth/actions/guestOnly.ts @@ -1,7 +1,7 @@ "use server"; import { redirect } from "next/navigation"; -import getUser from "./getUser"; +import getUser from "./getMyDetailAction"; export default async function guestOnly() { const user = await getUser(); diff --git a/src/modules/auth/contexts/AuthContext.tsx b/src/modules/auth/contexts/AuthContext.tsx index 0875164..98534d8 100644 --- a/src/modules/auth/contexts/AuthContext.tsx +++ b/src/modules/auth/contexts/AuthContext.tsx @@ -9,7 +9,10 @@ import React, { useMemo, useState, } from "react"; -import getUser from "../actions/getUser"; +import getUser from "../actions/getMyDetailAction"; +import withServerAction from "@/modules/dashboard/utils/withServerAction"; +import getMyDetailAction from "../actions/getMyDetailAction"; +import { notifications } from "@mantine/notifications"; interface UserData { name: string; @@ -34,14 +37,14 @@ export const AuthContextProvider = ({ children }: Props) => { const [user, setUser] = useState(null); const fetchUserData = useCallback(() => { - const getUserData = async () => { - const user = await getUser(); - setUser(user); - }; - - getUserData() - .then(() => {}) - .catch(() => {}); + + withServerAction(getMyDetailAction) + .then((response) => { + setUser(response.data); + }) + .catch((error) => { + console.error("Error while retrieving user data") + }) }, []); useEffect(() => { diff --git a/src/modules/auth/services/createUser.ts b/src/modules/auth/services/createUser.ts index a903658..39445f2 100644 --- a/src/modules/auth/services/createUser.ts +++ b/src/modules/auth/services/createUser.ts @@ -23,7 +23,7 @@ export default async function createUser(userData: CreateUserSchema) { }); } - //Check email exists + //Check email exists if ( await db.user.findFirst({ where: { email: validatedFields.data.email }, diff --git a/src/modules/auth/services/getMyDetail.ts b/src/modules/auth/services/getMyDetail.ts new file mode 100644 index 0000000..295989d --- /dev/null +++ b/src/modules/auth/services/getMyDetail.ts @@ -0,0 +1,18 @@ +import { cookies } from "next/headers"; +import getUserFromToken from "../utils/getUserFromToken"; + +export default async function getMyDetail() { + const token = cookies().get("token"); + + if (!token) return null; + + const user = await getUserFromToken(token.value); + + if (!user) return null; + + return { + name: user.name ?? "", + email: user.email ?? "", + photoUrl: user.photoProfile ?? null, + }; +} diff --git a/src/modules/userManagement/tables/UsersTable/UsersTable.tsx b/src/modules/userManagement/tables/UsersTable/UsersTable.tsx index 7f44662..b947cbf 100644 --- a/src/modules/userManagement/tables/UsersTable/UsersTable.tsx +++ b/src/modules/userManagement/tables/UsersTable/UsersTable.tsx @@ -1,5 +1,4 @@ "use client"; -import getUser from "@/modules/auth/actions/getUser"; import CrudPermissions from "@/modules/dashboard/types/CrudPermissions"; import { Table, Text, Flex, Button, Center } from "@mantine/core"; import {