Skip to content

Instantly share code, notes, and snippets.

View guzmonne's full-sized avatar

Guzman Monne guzmonne

  • Uruguay
  • Montevideo, Uruguay
View GitHub Profile
@guzmonne
guzmonne / flatten.js
Created July 10, 2017 11:45
Arbitrary flattens a nested array.
/**
* Flattens an array of arbitrarily nested arrays into a flat array.
* E.g. [[1,2,[3]],4] -> [1,2,3,4].
* @param {array} array Array to be flatten.
*/
function flatten(array) {
/**
* Checks if the given array has any nested array.
* @param {array} array Array to check if it is flat.
*/
@guzmonne
guzmonne / signOut.js
Created June 25, 2017 16:18
cognito-auth.signOut.js
/**
* Cognito signOut function wrapped inside a promise.
*/
function signOut() {
User || (User = UserPool.getCurrentUser())
if (!User) {
return Promise.reject('Current user session not found');
}
return Promise.resolve(User.signOut());
}
@guzmonne
guzmonne / Welcome-mount.js
Created June 25, 2017 16:13
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.
@guzmonne
guzmonne / sessions.js
Created June 25, 2017 16:07
cognito-auth.sessions.js
/**
* Function to get a valid current user session from localstorage.
*/
function getSession() {
User || (User = UserPool.getCurrentUser());
return new Promise(function(resolve, reject) {
if (User === null) {
reject('No current session found.');
return;
}
@guzmonne
guzmonne / Confirm-example.js
Created June 25, 2017 14:09
cognito-auth.Confirm-example.js
/**
* Email confirmation function that uses AWS Cognito SDK.
* It wraps its API into a promise to better consume it from other parts of
* the application.
*/
function confirm(username, code) {
User = new CognitoUser({
Username : username,
Pool: UserPool,
});
@guzmonne
guzmonne / Login-example.js
Created June 25, 2017 14:02
cognito-auth.Login-example.js
/**
* Signup function that uses AWS Cognito SDK. It wraps its API into a promise
* to consume it easily from other parts of the app.
*/
function logIn(username, password) {
var authenticationDetails = new AuthenticationDetails({
Username: username,
Password: password,
});
User = new CognitoUser({
@guzmonne
guzmonne / Signup-example.js
Created June 25, 2017 02:25
cognito-auth.Signup-example.js
/**
* Signup function that uses AWS Cognito SDK. It wraps its API into a promise
* to consume it easily from other parts of the app.
*/
function signUp(email, password) {
var attributes = [new CognitoUserAttribute({
Name: 'email',
Value: email,
})]
return new Promise(function(resolve, reject) {
@guzmonne
guzmonne / SignupForm.template.html
Created June 24, 2017 23:50
cognito-auth.SignupForm.template.html
<script type="text/html" id="SignupForm">
<div class="SignupForm">
<div class="title">Sign Up</div>
<form class="form">
<div class="Control">
<label class="Control__label" for="user">User</label>
<input class="Control__input" name="user" type="text" placeholder="User" />
</div>
<div class="Control">
<label class="Control__label" for="password">Password</label>
@guzmonne
guzmonne / SignupForm.js
Created June 24, 2017 23:49
cognito-auth.SignupForm.js
(function(EventEmitter, tmpl) {
var $root = document.getElementById('root'),
$container = document.createElement('div'),
$loginLink;
function handleLoginLink(event) {
event.preventDefault();
EventEmitter.emit('SignupForm:unmount')
EventEmitter.emit('LoginForm:mount')
@guzmonne
guzmonne / index.js
Created June 24, 2017 22:01
cognito-auth.index.js
(function(ready, EventEmitter) {
ready(function() {
EventEmitter.emit('LoginForm:mount')
})
})(window.ready, window.EventEmitter)