Change get user detail to service
This commit is contained in:
parent
5d8afc7c0f
commit
e6ffe4112d
|
|
@ -4,7 +4,7 @@ import Image from "next/image";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import logo from "@/assets/logos/logo.png";
|
import logo from "@/assets/logos/logo.png";
|
||||||
import DashboardLayout from "@/modules/dashboard/components/DashboardLayout";
|
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 { redirect } from "next/navigation";
|
||||||
import { Notifications } from "@mantine/notifications";
|
import { Notifications } from "@mantine/notifications";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { cookies } from "next/headers";
|
|
||||||
import "server-only";
|
import "server-only";
|
||||||
import getUserFromToken from "../utils/getUserFromToken";
|
|
||||||
import AuthError from "../error/AuthError";
|
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.
|
* 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.
|
* @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.
|
* @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 {
|
try {
|
||||||
const token = cookies().get("token");
|
|
||||||
|
|
||||||
if (!token) return null;
|
|
||||||
|
|
||||||
const user = await getUserFromToken(token.value);
|
|
||||||
|
|
||||||
if (!user) return null;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: user.name ?? "",
|
success: true,
|
||||||
email: user.email ?? "",
|
data: await getMyDetail(),
|
||||||
photoUrl: user.photoProfile ?? null,
|
|
||||||
};
|
};
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
// Handle specific authentication errors gracefully
|
if (
|
||||||
if (e instanceof AuthError && e.errorCode === "INVALID_JWT_TOKEN") {
|
e instanceof AuthError &&
|
||||||
return null;
|
["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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import getUser from "./getUser";
|
import getUser from "./getMyDetailAction";
|
||||||
|
|
||||||
export default async function guestOnly() {
|
export default async function guestOnly() {
|
||||||
const user = await getUser();
|
const user = await getUser();
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,10 @@ import React, {
|
||||||
useMemo,
|
useMemo,
|
||||||
useState,
|
useState,
|
||||||
} from "react";
|
} 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 {
|
interface UserData {
|
||||||
name: string;
|
name: string;
|
||||||
|
|
@ -34,14 +37,14 @@ export const AuthContextProvider = ({ children }: Props) => {
|
||||||
const [user, setUser] = useState<UserData | null>(null);
|
const [user, setUser] = useState<UserData | null>(null);
|
||||||
|
|
||||||
const fetchUserData = useCallback(() => {
|
const fetchUserData = useCallback(() => {
|
||||||
const getUserData = async () => {
|
|
||||||
const user = await getUser();
|
withServerAction(getMyDetailAction)
|
||||||
setUser(user);
|
.then((response) => {
|
||||||
};
|
setUser(response.data);
|
||||||
|
})
|
||||||
getUserData()
|
.catch((error) => {
|
||||||
.then(() => {})
|
console.error("Error while retrieving user data")
|
||||||
.catch(() => {});
|
})
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ export default async function createUser(userData: CreateUserSchema) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check email exists
|
//Check email exists
|
||||||
if (
|
if (
|
||||||
await db.user.findFirst({
|
await db.user.findFirst({
|
||||||
where: { email: validatedFields.data.email },
|
where: { email: validatedFields.data.email },
|
||||||
|
|
|
||||||
18
src/modules/auth/services/getMyDetail.ts
Normal file
18
src/modules/auth/services/getMyDetail.ts
Normal 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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
"use client";
|
"use client";
|
||||||
import getUser from "@/modules/auth/actions/getUser";
|
|
||||||
import CrudPermissions from "@/modules/dashboard/types/CrudPermissions";
|
import CrudPermissions from "@/modules/dashboard/types/CrudPermissions";
|
||||||
import { Table, Text, Flex, Button, Center } from "@mantine/core";
|
import { Table, Text, Flex, Button, Center } from "@mantine/core";
|
||||||
import {
|
import {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user