forked from eightants/reddium
-
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.
Get logging in and out working; fix the header on the profile page (#7)
* Add an auth context for managing the token. * Get logging in/out working. * Update the profile page to use `<Header/>`.
- Loading branch information
Showing
13 changed files
with
208 additions
and
107 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
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,43 @@ | ||
import React, { createContext, useState, useContext, useEffect } from 'react'; | ||
import Cookies from 'js-cookie'; | ||
|
||
interface AuthContextType { | ||
token: string | null; | ||
setToken: (token: string | null) => void; | ||
} | ||
|
||
const AuthContext = createContext<AuthContextType | undefined>(undefined); | ||
|
||
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { | ||
const [token, setToken] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const storedToken = Cookies.get('token'); | ||
if (storedToken) { | ||
setToken(storedToken); | ||
} | ||
}, []); | ||
|
||
const updateToken = (newToken: string | null) => { | ||
setToken(newToken); | ||
if (newToken) { | ||
Cookies.set('token', newToken, { expires: 7 }); | ||
} else { | ||
Cookies.remove('token'); | ||
} | ||
}; | ||
|
||
return ( | ||
<AuthContext.Provider value={{ token, setToken: updateToken }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export const useAuth = () => { | ||
const context = useContext(AuthContext); | ||
if (context === undefined) { | ||
throw new Error('useAuth must be used within an AuthProvider'); | ||
} | ||
return context; | ||
}; |
File renamed without changes.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
Oops, something went wrong.