question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
GeorgeBarnardcommented, Mar 16, 2019

@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!

0reactions
github-actions[bot]commented, Jun 12, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Auth.currentSession says no current user - Stack Overflow
The user is able to log in, but when I try to get the current session, I get a message saying no current...
Read more >
Authentication - Password & user management - JavaScript
Auth.currentSession() returns a CognitoUserSession object which contains JWT accessToken , idToken , and refreshToken . This method will automatically ...
Read more >
Authentication State Persistence | Firebase - Google
Indicates that the state will be persisted even when the browser window is closed or the activity is destroyed in React Native. An...
Read more >
OpenID Connect & OAuth 2.0 API - Okta Developer
This request authenticates the user and returns tokens along with an authorization grant to the client application as a part of the callback...
Read more >
JavaScript SDK - Stytch
This should be a url that your app receives and parses and subsequently send an API request to authenticate the magic link and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found