Reputation: 7709
I have a javascript project where I use the aws-sdk. No I want to use amazon-cognito-identity-js. On the page it says:
Note that the Amazon Cognito AWS SDK for JavaScript is just a slimmed down version of the AWS Javascript SDK namespaced as AWSCognito instead of AWS. It references only the Amazon Cognito Identity service.
and indeed, I can for example create CognitoIdentityServiceProvider with:
CognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider();
But how do I do thinks like authenticate a user? According to the amazon-cognito-identity-js documentation:
authenticationDetails = new CognitoIdentityServiceProvider.AuthenticationDetails({Userame: ..., Password: ...});
cognitoUser.authenticateUser(authenticationDetails, ...)
But the CognitoIdentityServiceProvider object does not have a AuthenticationDetails property.
Do I have to do something different when I use the aws-sdk instead of amazon-cognito-identity-js?
Or is my assumption wrong, and I need both, the aws-sdk and amazon-cognito-identity-js?
Upvotes: 3
Views: 4443
Reputation: 156
for any coming in the future
the following is what i did for register and login using
private cognito = new AWS.CognitoIdentityServiceProvider();
register:
public async register(event) {
try {
if(!this.validator.isObjectNotEmpty(event.body)){
throw new Error("you have to include body to insert into DB !!");
}
const usermodel = event.body;
if (this.validator.isEmailNotValid(usermodel)) {
throw new Error("Not Valid username or password");
}
const email = usermodel.email.toLowerCase();
const password = usermodel.password;
const createUserParams = {
UserPoolId: Cognito_User_Pool, // From Cognito dashboard 'Pool Id'
Username: email,
MessageAction: "SUPPRESS", // Do not send welcome email
TemporaryPassword: password,
};
const user = await this.cognito.adminCreateUser(createUserParams).promise();
this.logger.info("created user: ",user);
const initiateAuthParams = {
AuthFlow: "ADMIN_USER_PASSWORD_AUTH",
ClientId: Cognito_User_Pool_Client,
UserPoolId: Cognito_User_Pool,
AuthParameters: {
USERNAME: email,
PASSWORD: password
}
};
const challengesResponse = await this.cognito.adminInitiateAuth(initiateAuthParams).promise();
this.logger.info("challengesResponse: ",challengesResponse);
const userPasswordParams = {
Password: password,
UserPoolId: Cognito_User_Pool,
Username: email,
Permanent: true
};
return this.cognito.adminSetUserPassword(userPasswordParams).promise();
} catch (error) {
console.log("error: ", error);
throw error;
}
}
login
public async login(event) {
try {
this.logger.info("login - event: ",JSON.stringify(event));
this.logger.info("register - Cognito_User_Pool: ",Cognito_User_Pool);
this.logger.info("register - Cognito_User_Pool_Client: ",Cognito_User_Pool_Client);
if(!this.validator.isObjectNotEmpty(event.body)){
throw new Error("you have to include body to insert into DB !!");
}
const usermodel = event.body;
if (this.validator.isEmailNotValid(usermodel)) {
throw new Error("Not Valid username or password");
}
const email = usermodel.email.toLowerCase();
const password = usermodel.password;
const initiateAuthParams = {
AuthFlow: "ADMIN_USER_PASSWORD_AUTH",
ClientId: Cognito_User_Pool_Client,
UserPoolId: Cognito_User_Pool,
AuthParameters: {
USERNAME: email,
PASSWORD: password
}
};
return await this.cognito.adminInitiateAuth(initiateAuthParams).promise();
} catch (error) {
console.log("error: ", error);
throw error;
}
}
the login function return response like this:
"tokenObj": {
"ChallengeParameters": {},
"AuthenticationResult": {
"AccessToken": "eyJraWQiOiI1ZFN5R0tEUFE2WnhTbzJ4N01QTXpxaVRPNXZoK3p5NkJaOFZhRzV1M0Z3PSIsImFsZyI6IlJTMjU2In0.eyJvcmlnaW5fanRpIjoiMGYzY2RmZTItZTM3Zi00NGRiLWJiMWQtMDIzYjFiZTdlZGVjIiwic3ViIjoiZWJjN2M4YzctZjllYS00MTMxLTgxNDQtNjdkM2ZmYzg1ZTdlIiwiZXZlbnRfaWQiOiJhYWJhNzgyZi1iZTVhLTQxMjItYjljYS0yNjg5NTY2ZDVkMTgiLCJ0b2tlbl91c2UiOiJhY2Nlc3MiLCJzY29wZSI6ImF3cy5jb2duaXRvLnNpZ25pbi51c2VyLmFkbWluIiwiYXV0aF90aW1lIjoxNjM4MTI0NTY3LCJpc3MiOiJodHRwczpcL1wvY29nbml0by1pZHAudXMtd2VzdC0yLmFtYXpvbmF3cy5jb21cL3VzLXdlc3QtMl9vVmFnb3hFd0wiLCJleHAiOjE2MzgxMjgxNjcsImlhdCI6MTYzODEyNDU2NywianRpIjoiZjE0MTBjMG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Xr0_2JgM2KuvB0lKL0v4heHK8e2ht_VuKBPNpVvMIbIA2xN22LRhivfG8EMeHmF3jhhQYggi1Pczb2WEviPUqebmLhdvjJpHC9OMVhvN4GMcr0JVv-GepxSCrNSFoXPDNZaznJbDJW0Zt3xY9bWek_l62v1Wm0a1h7V7nxw8a0Csw"
}
}
}
Upvotes: 0
Reputation: 1341
For userName password authentication, it needs to have ADMIN_NO_SRP_AUTH. I also added function to handle err and success. For more details about response and token sent in data, you can check was reference.
const cognito = new AWS.CognitoIdentityServiceProvider();
cognito.adminInitiateAuth({
AuthFlow: 'ADMIN_NO_SRP_AUTH',
ClientId: 'clientId',
UserPoolId: 'poolId'
AuthParameters: {
USERNAME: 'userName',
PASSWORD: 'password',
},
}, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
console.log(data);
}
});
Upvotes: 1
Reputation: 2725
No, you don't. You can login using aws-sdk like this:
const cognito = new aws.CognitoIdentityServiceProvider({ region });
cognito.adminInitiateAuth({
AuthFlow: 'ADMIN_NO_SRP_AUTH',
ClientId: clientId,
UserPoolId: poolId,
AuthParameters: {
USERNAME: email,
PASSWORD: password,
},
});
Upvotes: 5