Skip to content

Commit

Permalink
Merge pull request #118 from LerianStudio/fix/token-expired
Browse files Browse the repository at this point in the history
💥 fix: Fixed token expired issue
  • Loading branch information
caioaletroca authored Jan 16, 2025
2 parents 44cf689 + 00dd588 commit 3c89b29
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/app/(auth-routes)/signout/page.tsx
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 4 additions & 1 deletion src/core/infrastructure/errors/midaz-error.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 2 additions & 1 deletion src/core/infrastructure/utils/midaz-error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export async function handleMidazError(
intl.formatMessage({
id: 'error.midaz.unauthorized',
defaultMessage: 'Error Midaz unauthorized'
})
}),
midazError.code
)

default:
Expand Down
5 changes: 4 additions & 1 deletion src/lib/fetcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -63,7 +64,9 @@ export const serverFetcher = async <T = void>(action: () => Promise<T>) => {
try {
return await action()
} catch (error) {
redirect('/signin')
if (error instanceof MidazError && error.code === '0042') {
redirect('/signout')
}
return null
}
}
Expand Down

0 comments on commit 3c89b29

Please sign in to comment.