-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56188c0
commit 6cd789a
Showing
5 changed files
with
112 additions
and
17 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 |
---|---|---|
@@ -1,16 +1,27 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
import { Provider } from 'urql'; | ||
|
||
import client from '@/config/client'; | ||
import { generateClient } from '@/config/client'; | ||
import { AuthProvider, useAuthContext } from '@/contexts/AuthContext'; | ||
|
||
// TODO: Fix any type | ||
type ProvidersProps = { | ||
children: any; | ||
children: React.ReactNode; | ||
}; | ||
|
||
const Providers: React.FC<ProvidersProps> = ({ children }) => { | ||
return <Provider value={client}>{children}</Provider>; | ||
const { accessToken } = useAuthContext(); | ||
|
||
const client = React.useMemo(() => { | ||
return generateClient(accessToken); | ||
}, [accessToken]); | ||
|
||
return ( | ||
<AuthProvider> | ||
<Provider value={client}>{children}</Provider> | ||
</AuthProvider> | ||
); | ||
}; | ||
|
||
export default Providers; |
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 |
---|---|---|
@@ -1,8 +1,13 @@ | ||
import { createClient, cacheExchange, fetchExchange } from 'urql'; | ||
|
||
const client = createClient({ | ||
url: process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4000/graphql', | ||
exchanges: [cacheExchange, fetchExchange] | ||
}); | ||
|
||
export default client; | ||
export const generateClient = (accessToken?: string) => { | ||
return createClient({ | ||
url: process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4000/graphql', | ||
exchanges: [cacheExchange, fetchExchange], | ||
fetchOptions: { | ||
headers: { | ||
Authorization: `Bearer ${accessToken}` | ||
} | ||
} | ||
}); | ||
}; |
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,65 @@ | ||
import React from 'react'; | ||
|
||
interface AuthContextType { | ||
accessToken?: string; | ||
userId?: string; | ||
onLogin: (accessToken: string, userId: string) => void; | ||
} | ||
|
||
const AuthContext = React.createContext<AuthContextType>({ | ||
accessToken: undefined, | ||
userId: undefined, | ||
onLogin: () => {} | ||
// isLoggedIn: false, | ||
// onLogout: () => {}, | ||
// onLogin: (email, password) => {} | ||
}); | ||
|
||
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ | ||
children | ||
}) => { | ||
const [accessToken, setAccessToken] = React.useState<string>(); | ||
const [userId, setUserId] = React.useState<string>(); | ||
|
||
React.useEffect(() => { | ||
const accessToken = window.localStorage.getItem('accessToken'); | ||
const userId = window.localStorage.getItem('userId'); | ||
|
||
if (accessToken && userId) { | ||
setAccessToken(accessToken); | ||
setUserId(userId); | ||
} | ||
}, []); | ||
|
||
const handleLogin = (accessToken: string, userId: string) => { | ||
setAccessToken(accessToken); | ||
setUserId(userId); | ||
window.localStorage.setItem('accessToken', accessToken); | ||
window.localStorage.setItem('userId', userId); | ||
}; | ||
|
||
return ( | ||
<AuthContext.Provider | ||
value={{ | ||
accessToken, | ||
userId, | ||
onLogin: handleLogin | ||
// isLoggedIn: false, | ||
// onLogout: () => {}, | ||
// onLogin: (email, password) => {} | ||
}} | ||
> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export const useAuthContext = () => { | ||
const context = React.useContext(AuthContext); | ||
|
||
if (!context) { | ||
throw new Error('useAuthContext must be used within an AuthProvider'); | ||
} | ||
|
||
return context; | ||
}; |