-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
token varification and refresh token fix
- Loading branch information
1 parent
4472dbd
commit 8df762a
Showing
12 changed files
with
504 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as Sentry from "@sentry/nextjs"; | ||
|
||
Sentry.init({ | ||
dsn: "http://[email protected]:9000/2", | ||
integrations: [Sentry.replayIntegration()], | ||
replaysSessionSampleRate: 0.1, | ||
replaysOnErrorSampleRate: 1.0 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { jwtDecode } from 'jwt-decode'; | ||
import authClient from './authClient'; | ||
|
||
interface DecodedToken { | ||
exp: number; | ||
} | ||
|
||
export async function checkAndRefreshToken(): Promise<string | null> { | ||
const accessToken = localStorage.getItem('accessToken'); | ||
|
||
if (!accessToken) { | ||
return null; | ||
} | ||
|
||
try { | ||
const decodedToken = jwtDecode<DecodedToken>(accessToken); | ||
const currentTime = Math.floor(Date.now() / 1000); | ||
|
||
if (decodedToken.exp > currentTime) { | ||
// Token is still valid | ||
return accessToken; | ||
} | ||
|
||
// Token has expired, attempt to refresh | ||
const refreshToken = localStorage.getItem('refreshToken'); | ||
const walletAddress = localStorage.getItem('walletAddress'); | ||
|
||
if (!refreshToken || !walletAddress) { | ||
throw new Error('Refresh token or wallet address not found'); | ||
} | ||
|
||
const refreshResponse = await authClient.post('/auth/refresh', { | ||
refresh_token: refreshToken, | ||
address: walletAddress, | ||
}); | ||
|
||
if (refreshResponse.data.status === 'success') { | ||
const newAccessToken = refreshResponse.data.access_token; | ||
const newRefreshToken = refreshResponse.data.refresh_token; | ||
|
||
localStorage.setItem('accessToken', newAccessToken); | ||
localStorage.setItem('refreshToken', newRefreshToken); | ||
|
||
return newAccessToken; | ||
} else { | ||
throw new Error('Refresh token failed'); | ||
} | ||
} catch (error) { | ||
console.error('Error checking or refreshing token:', error); | ||
localStorage.removeItem('accessToken'); | ||
localStorage.removeItem('refreshToken'); | ||
localStorage.removeItem('walletAddress'); | ||
return null; | ||
} | ||
} | ||
|
Oops, something went wrong.