Auth.CurrentSession() works on the 1st Page of my React App, but returns "not authenticated" after <Link> to a 2nd page
See original GitHub issue** Which Category is your question related to? ** React ** What AWS Services are you utilizing? ** Cognito User Pool, Amplify ** Provide additional details e.g. code snippets ** So I have a SignIn component that performs the Auth.signIn call and redirects (using <Redirect> ) to a Homepage component. The Homepage then calls Auth.currentSession() successfully and uses the jwt tokens. On the Homepage, I have a <Link> to another page, which attempts to do the same call to Auth.currentSession() - however, this time it fails with a “not authenticated” error. Interestingly, I can call Auth.currentCredentials() and successfully get the client’s credentials, which also shows the “authorised” status as “true”.
I’ve tried both “await Auth.currentSession()” and “Auth.currentSession().then()” fixes, but neither work
My Router looks like this:
<BrowserRouter>
<Switch>
<Route exact path="/" component={SignIn}/>
<Route path="/register" component={SignUp} />
<Route path="/signin" component={SignIn} />
<PrivateRoute path="/home" component={Home} />
<PrivateRoute path="/customer" component={Customer} />
</Switch>
</BrowserRouter>
where PrivateRoute is defined by:
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props => (
isAuthenticated() === true
? <Component {...props} />
: <Redirect to='/signin' />
)} />
)
Furthermore, even with this definition of PrivateRoute I can still access the Customer page when isAuthenticated is true.
I’m wondering if there’s any extra special configurations when working with Amplify and React/react-router?
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
@richardh0455 Did you ever find a solution for this? I am running into the same issue when I refresh my application 2+ times. I have gone in circles and come across all CookieStorage suggestions (#812), but nothing seems applicable!
This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server
*-help
channels or Discussions for those types of questions.