-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from DevKor-github/api/signUp
feat: AuthProvider
- Loading branch information
Showing
4 changed files
with
67 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use client'; | ||
|
||
import { refresh } from '@/libs/clients/refresh'; | ||
import { useAuthStore } from '@/libs/store/useAuthStore'; | ||
import { useCallback, useEffect } from 'react'; | ||
|
||
export function AuthProvider() { | ||
const { accessToken, refreshToken, clearTokens, setTokens } = useAuthStore(); | ||
|
||
const setTokenFromCookie = useCallback(() => { | ||
let accessToken = null, | ||
refreshToken = null; | ||
|
||
document.cookie.split(';').forEach((c) => { | ||
if (c.trim().startsWith('access-token=')) accessToken = c.split('=')[1]; | ||
if (c.trim().startsWith('refresh-token=')) refreshToken = c.split('=')[1]; | ||
}); | ||
if (!accessToken || !refreshToken) { | ||
clearTokens(); | ||
return false; | ||
} | ||
|
||
document.cookie = 'access-token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; | ||
document.cookie = 'refresh-token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; | ||
|
||
setTokens(accessToken, refreshToken); | ||
|
||
return true; | ||
}, [clearTokens, setTokens]); | ||
|
||
const checkAuth = useCallback(async () => { | ||
if (refreshToken) { | ||
await refresh(process.env.NEXT_PUBLIC_API_URL ?? ''); | ||
} else { | ||
setTokenFromCookie(); | ||
} | ||
}, [refreshToken, setTokenFromCookie]); | ||
|
||
useEffect(() => { | ||
if (accessToken === null) checkAuth(); | ||
}, [accessToken, checkAuth]); | ||
|
||
return <></>; | ||
} |
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
import { create } from 'zustand'; | ||
import { persist } from 'zustand/middleware'; | ||
|
||
interface AuthStore { | ||
accessToken: string | null; | ||
refreshToken: string | null; | ||
setTokens: (accessToken: string, refreshToken: string) => void; | ||
clearTokens: () => void; | ||
} | ||
|
||
export const useAuthStore = create<AuthStore>()( | ||
persist( | ||
(set) => ({ | ||
accessToken: null, | ||
refreshToken: null, | ||
setTokens: (accessToken, refreshToken) => set({ accessToken, refreshToken }), | ||
clearTokens: () => set({ accessToken: null, refreshToken: null }), | ||
}), | ||
{ name: 'user auth' }, | ||
), | ||
); |