Created
September 17, 2018 17:51
-
-
Save Fabianopb/78e7ee9a45a3f11df631492c4ffa8d58 to your computer and use it in GitHub Desktop.
Session management for express-react-ts-ci Medium story
This file contains hidden or 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
// Set the session in the local storage | |
export const setSession = (token: string, expiry: string): void => { | |
localStorage.setItem('token', token); | |
localStorage.setItem('expiry', expiry); | |
}; | |
// Clear the session from the local storage | |
export const clearSession = (): void => { | |
localStorage.removeItem('token'); | |
localStorage.removeItem('expiry'); | |
}; | |
// Checks if the session is valid (locally) according to the expiration time | |
export const isSessionValid = (): boolean => { | |
const expiry = localStorage.getItem('expiry'); | |
if (expiry) { | |
return +new Date(expiry) > +new Date(); | |
} | |
return false; | |
}; | |
// Creates the authorization header using the bearer token | |
export const getAuthHeaders = () => ({ | |
Authorization: `Bearer ${localStorage.getItem('token')}` | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment