Skip to content

Instantly share code, notes, and snippets.

@guzmonne
Created June 25, 2017 16:13
Show Gist options
  • Select an option

  • Save guzmonne/23d266ee3dd4090a63f40adf214b641d to your computer and use it in GitHub Desktop.

Select an option

Save guzmonne/23d266ee3dd4090a63f40adf214b641d to your computer and use it in GitHub Desktop.
cognito-auth.Welcome-mount.js
/**
* Function to handle the display of the Welcome function only for authenticated
* users. If they are not authenticated, or an error occurs, we'll redirect them
* to the login page.
*/
EventEmitter.on('Welcome:mount', function() {
// Check if the user is authenticated.
Cognito.isAuthenticated()
.then(function() {
// Mount the component on the DOM.
$root.appendChild($container);
// Show the loading banner.
loading();
// Get the user data.
Cognito.getUser()
.then(function(user) {
// Toggle the loading banner for the Welcome template.
$container.innerHTML = tmpl('Welcome', user);
$button = $container.getElementsByTagName('button')[0];
$button.addEventListener('click', handleSignOut);
})
})
// Handle any errors that pop up and redirect the user to the login page.
.catch(function(error) {
redirectToLoginPage({
type: 'error',
message: error.message,
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment