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') ...

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.