使用AWS Cognito的API进行登录,即在注册后直接调用API进行登录。
示例代码:
import AWS from 'aws-sdk';
import { CognitoUserPool, CognitoUserAttribute, CognitoUser } from 'amazon-cognito-identity-js';
const region = 'us-east-1';
const identityPoolId = 'YOUR_IDENTITY_POOL_ID';
// 在邀请注册策略下注册并直接登录
const signUpAndLogin = async (name, email, password, invite) => {
// create callback promise
const callbackPromise = () => {
return new Promise((resolve, reject) => {
const cognitoUserPool = new CognitoUserPool({
UserPoolId: 'YOUR_USER_POOL_ID',
ClientId: 'YOUR_APP_CLIENT_ID',
region: region,
});
const attributeList = [
new CognitoUserAttribute({
Name: 'custom:invite',
Value: invite,
}),
new CognitoUserAttribute({
Name: 'email',
Value: email,
}),
new CognitoUserAttribute({
Name: 'name',
Value: name,
}),
];
cognitoUserPool.signUp(email, password, attributeList, null, (err) => {
if (err) {
reject(err);
}
resolve();
});
});
};
await callbackPromise();
// login after sign up
const getIdToken = () => {
return new Promise((resolve, reject) => {
AWS.config.region = region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: identityPoolId,
});
const cognitoUser = new CognitoUser({
Username: email,
Pool: new CognitoUserPool({
UserPoolId: 'YOUR_USER_POOL_ID',
ClientId: 'YOUR_APP_CLIENT_ID',
region: region,
}),
});
const authenticationDetails = new AuthenticationDetails({
Username: email,
Password: password,
});
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
resolve(result.getIdToken().getJwtToken());
},
onFailure:
上一篇:AutoShardedBot和普通Bot之间有任何明显的区别吗?
下一篇:autoSignIn不起作用,如果在不同的会话中调用了Auth.confirmSignUp而不是Auth.signUp。