본문으로 바로가기

nodejs aws-amplify cognito 연동하기

category Language/Node.js 2019.05.08 14:49

nodejs cognito 연동하기

 

 

global.fetch = require('node-fetch');

const AmplifyCore = require('aws-amplify')
const Amplify = require('aws-amplify').default
const { API, Auth } = Amplify

const testLogin = (email, password, provider) => {
console.log(email);

let COGNITO = {
REGION: 'us-west-2',
USER_POOL_ID: 'us-west-2_xRKVaj5ls',
CLIENT_ID: '5084o932i7age4c0tc9j2unmff',
}

COGNITO = {
REGION: 'ap-northeast-2',
USER_POOL_ID: 'ap-northeast-2_CHGQe7flY',
CLIENT_ID: '29ilv9idglfh0spnbe9tpfb19m',
}

Amplify.configure({
Auth: {
userPoolId: COGNITO.USER_POOL_ID,
userPoolWebClientId: COGNITO.CLIENT_ID,
region: COGNITO.REGION,
// authenticationFlowType: 'CUSTOM_AUTH',
}
});
Auth.signIn(email, password)
.then(user => {
// console.log(user);
console.log(user.challengeName);
if (user.challengeName) {
const { requiredAttributes } = user.challengeParam; // the array of required attributes, e.g ['email', 'phone_number']
Auth.completeNewPassword(
user, // the Cognito User Object
"1234@Aoeuu", // the new password
// OPTIONAL, the required attributes
{
email: 'aa@example.com',
phone_number: '+1234567890'
}
).then(user => {
// at this time the user is logged in if no MFA required
console.log(user);
}).catch(e => {
console.log(e);
});

}
})
.catch(err => console.log("err:", err));
};


const callCognito = (userId, password, cb) => {
const email = userId;
const COGNITO2 = {
REGION: 'ap-northeast-2',
USER_POOL_ID: 'ap-northeast-2_CHGQe7flY',
CLIENT_ID: '29ilv9idglfh0spnbe9tpfb19m',
}

testLogin(userId, password, () => {
});

}


module.exports.callCognito = callCognito;


댓글을 달아 주세요