Skip to content

Instantly share code, notes, and snippets.

@sethtrain
Created April 22, 2016 14:39
Show Gist options
  • Select an option

  • Save sethtrain/334c5e54f098052a071c679d5e0d4a3b to your computer and use it in GitHub Desktop.

Select an option

Save sethtrain/334c5e54f098052a071c679d5e0d4a3b to your computer and use it in GitHub Desktop.

Revisions

  1. sethtrain created this gist Apr 22, 2016.
    220 changes: 220 additions & 0 deletions authentication.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,220 @@
    /* global AWSCognito */

    import actionTypes from './types';

    AWSCognito.config.region = 'us-east-1';
    AWSCognito.config.credentials = {
    accessKeyId: "...",
    secretAccessKey: "...",
    };

    const userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool({
    UserPoolId: '...',
    ClientId: '...',
    });

    /**
    * @returns {Object} Action data
    */
    export function gotoLogin() {
    return {
    type: actionTypes.GOTO_LOGIN,
    };
    }

    /**
    * @returns {Object} Action data
    */
    export function gotoRegistration() {
    return {
    type: actionTypes.GOTO_REGISTRATION,
    };
    }

    /**
    * @returns {Object} Action data
    */
    export function gotoUsers() {
    return {
    type: actionTypes.GOTO_USERS,
    };
    }

    /**
    * @param {string} email - user email address
    * @returns {Object} Action data
    */
    export function registerSuccess(email) {
    return {
    type: actionTypes.REGISTRATION_SUCCESS,
    email,
    };
    }

    /**
    * @param {string} message - registration error message
    * @returns {Object} Action data
    */
    export function registerFailure(message) {
    return {
    type: actionTypes.REGISTRATION_FAILURE,
    message,
    };
    }

    /**
    * @param {string} email - user email address
    * @param {string} password - user password
    * @returns {Object} Action data
    */
    export function registerUser(email, password) {
    return dispatch => {
    const attributeEmail = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute({
    Name: "email",
    Value: email,
    });

    userPool.signUp(email, password, [attributeEmail], null, (err, result) => {
    if (err) {
    dispatch(registerFailure(err.message));
    }

    dispatch(registerSuccess(result.user.username));
    });
    };
    }

    /**
    * @param {string} message - confirmation error message
    * @returns {Object} Action data
    */
    export function confirmationError(message){
    return {
    type: actionTypes.CONFIRMATION_ERROR,
    message,
    };
    }

    /**
    * @param {string} email - user email address
    * @returns {Object} Action data
    */
    export function confirmationSuccess(email){
    return {
    type: actionTypes.CONFIRMATION_SUCCESS,
    email,
    };
    }

    /**
    * @param {string} email - user email address
    * @param {string} activationCode - user activationCode
    * @returns {Object} Action data
    */
    export function activateUser(email, activationCode) {
    return dispatch => {
    const userData = {
    Username: email,
    Pool: userPool,
    };

    const cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);

    cognitoUser.confirmRegistration(activationCode, true, err => {
    if (err){
    dispatch(confirmationError(err));
    }

    dispatch(confirmationSuccess(email));
    });
    };
    }


    /**
    * @param {string} email - user email address
    * @param {Object} token - authenticated JSON Web token
    * @returns {Object} Action data
    */
    export function authenticationSuccess(email, token) {
    return {
    type: actionTypes.AUTHENTICATION_SUCCESS,
    email,
    token,
    };
    }

    /**
    * @param {string} email - user email address
    * @param {Object} error - authentication error
    * @returns {Object} Action data
    */
    export function authenticationFailure(email, error) {
    return {
    type: actionTypes.AUTHENTICATION_FAILURE,
    email,
    error,
    };
    }

    /**
    * @param {string} email - user email address
    * @param {Object} error - authentication error
    * @returns {Object} Action data
    */
    export function logoutSuccessful() {
    return {
    type: actionTypes.LOGOUT_SUCCESSFUL,
    };
    }

    /**
    * @param {string} email - user email
    * @param {string} password - user password
    * @returns {object} new state
    */
    export function authenticateUser(email, password) {
    return dispatch => {
    const userData = {
    Username: email,
    Pool: userPool,
    };

    const cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);

    const authenticationData = {
    Username: email,
    Password: password,
    };

    const authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);

    cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: result => {
    dispatch(authenticationSuccess(cognitoUser, result.getAccessToken().getJwtToken()));
    },

    onFailure: err => {
    dispatch(authenticationFailure(cognitoUser, err));
    },
    });
    };
    }

    /**
    * @param {string} email - user email
    * @returns {object} new state
    */
    export function logoutUser(email) {
    return dispatch => {
    const userData = {
    Username: email,
    Pool: userPool,
    };

    const cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
    cognitoUser.signOut();

    dispatch(logoutSuccessful());
    };
    }