Closed Lirontal1 closed 4 months ago
Hello, @Lirontal1 and thanks for opening this issue. It looks like you are trying to call the v5 Auth.fetchAuthSession
API rather than the v6 fetchAuthSession
as seen here. That's likely one reason you don't have anything being returned by the await Auth.fetchAuthSession
.
It looks like you're trying to check for a session on your user after sign-in, so you may want to do this by calling fetchAuthSession
just after any sign-in events that you're listening for via the Hub Auth channel. Let me know if this clears things up or if you have further questions!
hello @Lirontal1. Can you upgrade to the latest version of the library and see if you still face any issue?
Also something that I notice. The above code might lead to a race condition. The library will dispatch the customOAuthState
event first — I imagine that's why the setTimeout
is used. And then the signInWithRedirect
event. The issue that I see with this approach is that setTimeout
will be called sync and delegate the navigate(decodeURIComponent(payload.data))
code to the event queue
. Then the signInWithRedirect
event will resolve and call the getUser
function, so any async code inside that method will also be pushed to the event queue
.
So the order of async operations should resolve in that way:
1.navigate(decodeURIComponent(payload.data))
fetchAuthSession
A potential solution is to move the navigate
logic to the signInWithRedirect
event — You can assign whatever data is returned from the customOAuthState
paylaod to a variable and then access it when the signInWithRedirect
is dispatched.
I'm having a similar issue, but just happening in production.
In my case, this private-route works as expected in localhost, but when in production the authStatus is coming as unauthenticated
. If I go to the login page after signed-in (which is a Authenticator component) and try to login again it says: There is already a signed in user.
but still getting the unauthenticated
I'm stuck in this more than a week and don't know what is the issue, and why it's just happening in prod.
// PrivateRoute.tsx
import { Loader } from '@platform/patterns';
import { useAuthenticator } from '@aws-amplify/ui-react';
import { PropsWithChildren } from 'react';
import { Navigate } from 'react-router-dom';
type PrivateRouteProps = {
redirectTo: string;
};
const PrivateRoute: React.FC<PropsWithChildren<PrivateRouteProps>> = ({
children,
redirectTo,
}) => {
const { authStatus } = useAuthenticator();
return authStatus === 'configuring' ? (
<Loader isLoading />
) : authStatus !== 'authenticated' ? (
<Navigate to={redirectTo} />
) : (
children
);
};
export default PrivateRoute;
// SignIn.tsx
import { Authenticator, useAuthenticator } from '@aws-amplify/ui-react';
import '@aws-amplify/ui-react/styles.css';
import { Box } from '@mui/material';
const SignInPage: React.FC = () => {
const { authStatus } = useAuthenticator();
return (
<Box display={'flex'} alignItems={'center'} justifyContent={'center'}>
<Authenticator hideSignUp>
{({ signOut, user }) => (
<main>
<h3>Status {authStatus}</h3>
<h1>Hello {user?.username}</h1>
<button onClick={signOut}>Sign out</button>
</main>
)}
</Authenticator>
</Box>
);
};
export default SignInPage;
Closing this issue as we have not heard back from you. If you are still experiencing this, please feel free to reply back and provide any information previously requested and we'd be happy to re-open the issue.
Thank you!
Before opening, please confirm:
JavaScript Framework
React
Amplify APIs
Authentication
Amplify Version
v6
Amplify Categories
auth
Backend
None
Environment information
Describe the bug
Hi! We have the following login configuration:
We would like to know if this code is well written and if there are any ways to improve this, as we have some race condition in which users successfully pass the IDP login, but still have no session in
await Auth.fetchAuthSession
. those flows are successfully saved as we call againgetUser
function when we receive asignInWithRedirect
eventExpected behavior
Users will be able to login without a race condition
Reproduction steps
No real way to reproduce.
Code Snippet
Log output
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