Skip to content

Instantly share code, notes, and snippets.

@viralsavaniIM
Last active November 21, 2022 19:45
Show Gist options
  • Save viralsavaniIM/b71bfe09870b02baf0e5b480d1b73b33 to your computer and use it in GitHub Desktop.
Save viralsavaniIM/b71bfe09870b02baf0e5b480d1b73b33 to your computer and use it in GitHub Desktop.
Facebank OAuth Integration Guide: AWS Cognito Authenticate with ClientMetadata
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2021 Facebank, Inc. //
// All rights reserved. //
// //
// Redistribution and use in source and binary forms, with or without //
// modification, are permitted provided that the following conditions are //
// met: //
// //
// Redistributions of source code must retain the above copyright //
// notice, this list of conditions and the following disclaimer. //
// Redistributions in bytecode form must reproduce the above copyright //
// notice, this list of conditions and the following disclaimer in //
// the documentation and/or other materials provided with the //
// distribution. //
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS //
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT //
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR //
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT //
// HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, //
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, //
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS //
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND //
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR //
// TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE //
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH //
// DAMAGE. //
///////////////////////////////////////////////////////////////////////////////
import {
AuthenticationDetails,
CognitoUser,
CognitoUserPool
} from "amazon-cognito-identity-js";
/**
* The key of client metadata you pass to the cognito's authenticate method
*/
const EXTERNAL_IDENTITY_LINK_CLIENT_METADATA_KEY = 'CONFIRM_IDENTITY_LINK';
const CognitoConfig = {
UserPoolId: '****',
ClientId: '****',
}
/**
* Performs authentication via Cognito SDK
* @param {String} username, cognito username
* @param {String} password, cognito password
* @param {String} externalIdentityToken, token received as error message from Facebank OAuth work-flow
*/
const authenticateWithCognito = async (username, password, token) => {
const authenticationData = {
Username: username,
Password: password
};
if (externalIdentityToken !== undefined) {
authenticationData.ClientMetadata = { [ EXTERNAL_IDENTITY_LINK_CLIENT_METADATA_KEY ]: externalIdentityToken };
}
const authDetails = new AuthenticationDetails(authenticationData);
const userPool = new CognitoUserPool(CognitoConfig);
const userData = {
Username: username,
Pool: userPool,
};
const congnitoUser = new CognitoUser(userData);
return new Promise((resolve, reject) => {
congnitoUser.authenticateUser(authDetails, {
onSuccess: (authResponse => {
resolve();
}),
onFailure: (error) => {
reject(err)
},
});
});
}
export {
loginWithCognito
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment