![Full-Stack React Projects](https://wfqqreader-1252317822.image.myqcloud.com/cover/866/36699866/b_36699866.jpg)
上QQ阅读APP看书,第一时间看更新
Managing auth state
In client/auth/auth-helper.js, we will define the following helper methods to store and retrieve JWT credentials from client-side sessionStorage, and also clear out the sessionStorage on user sign-out:
- authenticate(jwt, cb): Save credentials on successful sign-in:
authenticate(jwt, cb) {
if(typeof window !== "undefined")
sessionStorage.setItem('jwt', JSON.stringify(jwt))
cb()
}
- isAuthenticated(): Retrieve credentials if signed-in:
isAuthenticated() {
if (typeof window == "undefined")
return false
if (sessionStorage.getItem('jwt'))
return JSON.parse(sessionStorage.getItem('jwt'))
else
return false
}
- signout(cb): Delete credentials and sign out:
signout(cb) {
if(typeof window !== "undefined")
sessionStorage.removeItem('jwt')
cb()
signout().then((data) => {
document.cookie = "t=; expires=Thu, 01 Jan 1970 00:00:00
UTC; path=/;"
})
}
Using the methods defined here, the React components we build will be able to check and manage user auth state to restrict access in the frontend, as demonstrated in the following with the custom PrivateRoute.