From 00dd58803053a09b8a40b57c644dd3702b6725ed Mon Sep 17 00:00:00 2001 From: Caio Alexandre Troti Caetano Date: Thu, 16 Jan 2025 12:45:42 -0300 Subject: [PATCH] :boom: fix: Fixed token expired issue --- src/app/(auth-routes)/signout/page.tsx | 12 ++++++++++++ src/core/infrastructure/errors/midaz-error.ts | 5 ++++- src/core/infrastructure/utils/midaz-error-handler.ts | 3 ++- src/lib/fetcher/index.ts | 5 ++++- 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 src/app/(auth-routes)/signout/page.tsx diff --git a/src/app/(auth-routes)/signout/page.tsx b/src/app/(auth-routes)/signout/page.tsx new file mode 100644 index 00000000..8c1e82cb --- /dev/null +++ b/src/app/(auth-routes)/signout/page.tsx @@ -0,0 +1,12 @@ +'use client' + +import { useEffect } from 'react' +import { signOut } from 'next-auth/react' + +export default function Page() { + useEffect(() => { + signOut({ callbackUrl: '/' }) + }, []) + + return null +} diff --git a/src/core/infrastructure/errors/midaz-error.ts b/src/core/infrastructure/errors/midaz-error.ts index 14840932..a9878d3b 100644 --- a/src/core/infrastructure/errors/midaz-error.ts +++ b/src/core/infrastructure/errors/midaz-error.ts @@ -1,5 +1,8 @@ export class MidazError extends Error { - constructor(message: string) { + code = '0000' + + constructor(message: string, code: string = '0000') { super(message) + this.code = code } } diff --git a/src/core/infrastructure/utils/midaz-error-handler.ts b/src/core/infrastructure/utils/midaz-error-handler.ts index 1e6a4199..5694c82c 100644 --- a/src/core/infrastructure/utils/midaz-error-handler.ts +++ b/src/core/infrastructure/utils/midaz-error-handler.ts @@ -113,7 +113,8 @@ export async function handleMidazError( intl.formatMessage({ id: 'error.midaz.unauthorized', defaultMessage: 'Error Midaz unauthorized' - }) + }), + midazError.code ) default: diff --git a/src/lib/fetcher/index.ts b/src/lib/fetcher/index.ts index 777d2af9..ea00b14a 100644 --- a/src/lib/fetcher/index.ts +++ b/src/lib/fetcher/index.ts @@ -2,6 +2,7 @@ * TODO: Better error handling */ +import { MidazError } from '@/core/infrastructure/errors/midaz-error' import { signOut } from 'next-auth/react' import { redirect } from 'next/navigation' @@ -63,7 +64,9 @@ export const serverFetcher = async (action: () => Promise) => { try { return await action() } catch (error) { - redirect('/signin') + if (error instanceof MidazError && error.code === '0042') { + redirect('/signout') + } return null } }