我使用的是react-native,开箱即用的aws-amplify-react-native用于sigin注册用户。用户能够成功进行身份验证,但以“无当前用户”登录形式出现以下错误
我提高了日志级别以在应用程序中进行调试。我可以看到用户成功进行了身份验证,并且取回了JWT令牌,但是在日志中看到以下内容:
[DEBUG] 22:47.149 AuthClass - Failed to get user from user pool
[ERROR] 22:47.154 AuthClass - Failed to get the signed in user No current user
[DEBUG] 22:47.161 AuthPiece - No current user
下面是我的代码段:
import { ConfirmSignIn, ConfirmSignUp, ForgotPassword, RequireNewPassword, SignIn, SignUp, VerifyContact, withAuthenticator } from 'aws-amplify-react-native';
const RootStack = createStackNavigator(
{
Login: LoginScreen,
Main: MainScreen,
Customer: CustomerScreen,
Reports: ReportsScreen,
Signup: SignupScreen
},
{
initialRouteName: 'Main',
}
);
const AppContainer = createAppContainer(RootStack);
export class App extends React.Component {
render() {
return (
<AppContainer />
);
}
}
export default withAuthenticator(App);
运行我的应用程序时。我看到默认的Amplify登录表单,我用它输入用户名和密码,然后单击“登录”按钮,该按钮成功进行了身份验证,但是出现了“无当前用户错误”,如上所示。
答案 0 :(得分:1)
我有类似的问题。我从configure方法中删除了cookie存储块,它起作用了。
答案 1 :(得分:0)
您是否正在使用cookieStore?如果为true,您是否使用安全标志?如果是这样,请在开发环境中将其值更改为false
。
答案 2 :(得分:-1)
该错误字面意思是 No current user
- 您需要使用支持的身份提供商登录。
我的解决方案:
import { Amplify } from "@aws-amplify/core";
import { Auth } from "@aws-amplify/auth";
import { CookieStorage } from 'amazon-cognito-identity-js';
import amplifyConfig from "../lib/Amplify";
Amplify.configure(amplifyConfig);
const cookieStorage = new CookieStorage(amplifyConfig.Auth.cookieStorage);
// cookie that is set before Cognito redirect to prevent infinite loop if authorization fails due to other reason than "No current user"
const redirectedFromAuthorize = cookieStorage.getItem("redirected-from-authorize");
...
Auth.currentAuthenticatedUser()
.then(user => {
setUser(user); // your custom function to do something with user attributes
// authorization was successfull, we can remove the redirect cookie
cookieStorage.removeItem("redirected-from-authorize");
})
.catch(err => {
console.error(err);
// if the cookie is set, it means the authorization failed again and you should not redirect back to Cognito
if (!redirectedFromAuthorize) {
// set redirect cookie, so that we know next time the error is reocurring
cookieStorage.setItem("redirected-from-authorize", 'true');
// redirect to Cognito hosted UI
return Auth.federatedSignIn();
}
});