Skip to content

Instantly share code, notes, and snippets.

@Fabianopb
Created September 17, 2018 17:51
Show Gist options
  • Save Fabianopb/78e7ee9a45a3f11df631492c4ffa8d58 to your computer and use it in GitHub Desktop.
Save Fabianopb/78e7ee9a45a3f11df631492c4ffa8d58 to your computer and use it in GitHub Desktop.
Session management for express-react-ts-ci Medium story
// 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