Skip to content

Instantly share code, notes, and snippets.

@sethtrain
Created April 22, 2016 14:39
Show Gist options
  • Save sethtrain/334c5e54f098052a071c679d5e0d4a3b to your computer and use it in GitHub Desktop.
Save sethtrain/334c5e54f098052a071c679d5e0d4a3b to your computer and use it in GitHub Desktop.
/* 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());
};
}
@ljovanovic
Copy link

@sethtrain thanks for posting this. It looks like you are bypassing the whole CognitoCredentials portion of the process and instead are explicitly providing a specific IAM user - am I reading this correct? I say this because I don't see any reference to CognitoIdentityCredentials, and instead see you set the access key id / secret access key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment