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 } }