这可能是因为缺少对用户有效的身份验证令牌所需的权限。在调用注销函数之前,请确保基于您使用的身份池执行了正确的身份验证操作。
以下代码示例可以解决此问题。通过使用CognitoUser.getSession,在注销之前,请确保用户已获得身份验证并且具有有效的身份验证令牌。
import 'package:amazon_cognito_identity_dart_2/cognito.dart';
CognitoUserPool _cognitoUserPool = CognitoUserPool(
'yourUserPoolId',
'yourClientId',
);
Future logout() async {
try {
CognitoUser cognitoUser = _cognitoUserPool.getCurrentUser();
if (cognitoUser != null) {
final authDetails = CognitoUserSession(null, null);
await cognitoUser.globalSignOut();
}
} catch (e) {
print(e);
throw e;
}
}
Future login(String email, String password) async {
final user = CognitoUser(email, _cognitoUserPool);
final authDetails = AuthenticationDetails(username: email, password: password);
final session = await user.authenticateUser(authDetails);
print('Token : ${session.idToken.jwtToken}');
}
在这个例子中,logout函数使用getCurrentUser方法获取当前已经身份验证的用户。如果用户存在,我们使用globalSignOut方法注销该用户并删除他们的身份验证令牌。globalSignOut方法返回一个String并且不会抛出错误。如果有必要,请设置try-catch块来处理任何异常。
相反,login函数使用CognitoUser.authenticateUser方法来检验用户凭证并获取身份验证令牌。如果没有任何错误发生,它将打印身份验证令牌的值。
请注意,这只是使用CognitoUserPool进行身份验证的基本示例。您可能需要更改代码以符合您的实际业务需求。
上一篇:AWSCognito多级多租户
下一篇:AWSCognitoFORCE_CHANGE_PASSWORDemail_verified:truecan'tloginwiththetemporarypasswordreceived