Change get user detail to service

This commit is contained in:
sianida26 2024-02-27 23:13:50 +07:00
parent 5d8afc7c0f
commit e6ffe4112d
7 changed files with 54 additions and 31 deletions

View File

@ -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";

View File

@ -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<Awaited<ReturnType<typeof getMyDetail>>>
> {
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);
}
}

View File

@ -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();

View File

@ -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<UserData | null>(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(() => {

View File

@ -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,
};
}

View File

@ -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 {