amplify-js: signInWithRedirect sometimes does not forward to the auth provider
Before opening, please confirm:
- I have searched for duplicate or closed issues and discussions.
- I have read the guide for submitting bug reports.
- I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
JavaScript Framework
React
Amplify APIs
Authentication
Amplify Version
v6
Amplify Categories
auth
Backend
CDK
Environment information
# Put output below this line
Describe the bug
Sometimes, totally random, when a user tries to login via signInWithRedirect with a custom provider, it seems the function get called, but the user is not redirected to the provider and therefore no user is in the session. There is not a clear pattern here, but it is only happens to users we use adminGlobalSignOut in the cdk (in the backend) to log them out of our app and revoke all of the tokens. We do it for the users after an hour if the user is not active in the app.
Expected behavior
Always redirect the user to the auth provider or throw a relevant error.
Reproduction steps
I can’t reproduce as it is random, but I think it has a link to the Admin global sign out, so I would start there.
Code Snippet
const [email, setEmail] = useState();
const [user, setUser] = useState();
const submit = () => {
if (email) {
setAmplify();
signIn();
}
};
const setAmplify = () => {
Amplify.configure({
Auth: {
Cognito: {
userPoolClientId: <userPoolWebClientId>,
userPoolId: <userPoolId>,
loginWith: {
oauth: {
domain: <userPoolDomain>,
scopes: ['email', 'profile', 'openid', 'aws.cognito.signin.user.admin'],
redirectSignIn: [`${window.location.origin}`],
redirectSignOut: [`${window.location.origin}`],
responseType: 'code',
},
},
},
},
});
};
export const signIn = async (state?) => {
const signInDetails: any = {
customState: `${encodeURIComponent(state || window.location.pathname + window.location.search)}`,
};
if (<userPoolIdentityProviderName>) {
signInDetails.provider = { custom: <userPoolIdentityProviderName> };
}
return Auth.signInWithRedirect(signInDetails).catch(e => console.error(`Login error: ${e}`));
};
const unsubscribe = Hub.listen("auth", ({ payload }) => {
const errorText = JSON.stringify(payload?.data?.error?.message);
switch (payload.event) {
case "customOAuthState":
setTimeout(() => navigate(decodeURIComponent(payload.data)));
break;
case "signInWithRedirect":
getUser();
break;
case "signInWithRedirect_failure":
logout();
navigate("");
console.error(`[authentication error]: LOGIN FAIL: ${errorText}`);
document.location.reload();
break;
case "tokenRefresh_failure":
logout();
console.error(
`[authentication error]: REFRESH TOKEN FAIL: ${errorText}`
);
document.location.reload();
break;
default:
break;
}
});
const getUser() => {
try {
const tokenUser = getCurrentUser();
setUser(tokenUser);
} catch (error){
log.error(`no user is found ${error}`);
}
};
return user ? (<Box> <div>welcome</div></Box>) :
(<Box>
<TextInput
type="email"
label="Email address"
value={email}
onChange={setEmail}
/>
<Button variant="contained" disabled={isPending} onClick={submit()}>
Continue
</Button>
</Box>)
Log output
// Put your logs below this line
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 15 (8 by maintainers)
Hello @ronbhomri. In addition to @cwomack comment, I think the issue lies in how your App updates state. You can use conditional rendering to update the
disabledoption of the button untilemailis defined. It is also recommended to callAmplify.configurewhen the App renders. Below I added some code snippets. Let me know what you thinkBefore
After