Fix error login

This commit is contained in:
sianida26 2024-03-22 16:53:48 +07:00
parent e795419b78
commit 852459fdc2
11 changed files with 50 additions and 6 deletions

View File

@ -0,0 +1,7 @@
import React from 'react'
export default function Page() {
return (
<div>page</div>
)
}

View File

@ -1,6 +1,12 @@
import checkPermission from '@/modules/auth/utils/checkPermission'
import dashboardConfig from '@/modules/dashboard/dashboard.config'
import { redirect } from 'next/navigation'
import React from 'react' import React from 'react'
export default async function Dashboard() { export default async function Dashboard() {
if (!await checkPermission("authenticated-only")) redirect(`${dashboardConfig.baseRoute}/login`)
return ( return (
<div> <div>
<h1 className='font-bold bg-red-500'>Dashboard</h1> <h1 className='font-bold bg-red-500'>Dashboard</h1>

View File

@ -1,7 +1,12 @@
import { redirect } from 'next/navigation'
import React from 'react' import React from 'react'
export default function page() { export default function page() {
redirect("/dashboard")
return ( return (
<div>page</div> <div>page</div>
) )
} }

View File

@ -35,10 +35,11 @@ export default async function getMyDetailAction(): Promise<ServerResponseAction<
if (e instanceof AuthError && e.errorCode === "INVALID_JWT_TOKEN") { if (e instanceof AuthError && e.errorCode === "INVALID_JWT_TOKEN") {
return { return {
success: false, success: false,
error: new BaseError({ error: {
errorCode: e.errorCode, errorCode: "UNAUTHENTICATED",
message: "You are not authenticated", message: "You are not authenticated",
}), },
dashboardError: true
}; };
} }
// Handle other types of errors. // Handle other types of errors.

View File

@ -6,6 +6,7 @@ import React, { ReactElement, ReactNode, createContext, useCallback, useContext,
import { notifications } from "@mantine/notifications"; import { notifications } from "@mantine/notifications";
import getMyDetailAction from "../actions/getMyDetailAction"; import getMyDetailAction from "../actions/getMyDetailAction";
import withServerAction from "@/modules/dashboard/utils/withServerAction"; import withServerAction from "@/modules/dashboard/utils/withServerAction";
import ClientError from "@/core/error/ClientError";
// Defining the structure for user data within the authentication context. // Defining the structure for user data within the authentication context.
interface UserData { interface UserData {
@ -48,6 +49,9 @@ export const AuthContextProvider = ({ children }: Props): ReactElement => {
setUser(response.data); setUser(response.data);
}) })
.catch((error) => { .catch((error) => {
if (error instanceof ClientError){
if (error.errorCode === "UNAUTHENTICATED") return;
}
notifications.show({ notifications.show({
title: 'Error', title: 'Error',
message: 'Error while retrieving user data', message: 'Error while retrieving user data',

View File

@ -1,11 +1,13 @@
"use client"; "use client";
import React, { useEffect } from "react"; import React, { useEffect, useState } from "react";
import { AppShell } from "@mantine/core"; import { AppShell } from "@mantine/core";
import { useDisclosure } from "@mantine/hooks"; import { useDisclosure } from "@mantine/hooks";
import AppHeader from "./AppHeader"; import AppHeader from "./AppHeader";
import AppNavbar from "./AppNavbar"; import AppNavbar from "./AppNavbar";
import { useAuth } from "@/modules/auth/contexts/AuthContext"; import { useAuth } from "@/modules/auth/contexts/AuthContext";
import { usePathname } from "next/navigation";
import dashboardConfig from "../dashboard.config";
interface Props { interface Props {
children: React.ReactNode; children: React.ReactNode;
@ -20,16 +22,25 @@ interface Props {
* @returns A React element representing the dashboard layout. * @returns A React element representing the dashboard layout.
*/ */
export default function DashboardLayout(props: Props) { export default function DashboardLayout(props: Props) {
const pathname = usePathname();
// State and toggle function for handling the disclosure of the navigation bar // State and toggle function for handling the disclosure of the navigation bar
const [openNavbar, { toggle }] = useDisclosure(false); const [openNavbar, { toggle }] = useDisclosure(false);
const {fetchUserData} = useAuth(); const {fetchUserData} = useAuth();
const [withAppShell, setWithAppShell] = useState(false)
useEffect(() => { useEffect(() => {
fetchUserData() fetchUserData()
}, [fetchUserData]) }, [fetchUserData])
return ( useEffect(() => {
setWithAppShell(!dashboardConfig.routesWithoutAppShell.some(v => `${dashboardConfig.baseRoute}${v}` === pathname))
}, [pathname])
return withAppShell ? (
<AppShell <AppShell
padding="md" padding="md"
header={{ height: 70 }} header={{ height: 70 }}
@ -49,5 +60,5 @@ export default function DashboardLayout(props: Props) {
{props.children} {props.children}
</AppShell.Main> </AppShell.Main>
</AppShell> </AppShell>
); ) : props.children;
} }

View File

@ -0,0 +1,9 @@
const dashboardConfig = {
routesWithoutAppShell : [
"/login",
"/register",
],
baseRoute: "/dashboard"
} as const;
export default dashboardConfig;

View File

@ -1,5 +1,6 @@
import ClientError from "@/core/error/ClientError"; import ClientError from "@/core/error/ClientError";
import ServerResponseAction from "../types/ServerResponseAction"; import ServerResponseAction from "../types/ServerResponseAction";
import { error } from "console";
/** /**
* A higher-order function that wraps an async function and provides structured error handling. * A higher-order function that wraps an async function and provides structured error handling.