Amplify-js: Where to save token when Log In with Next.js

Created on 15 Mar 2020  路  3Comments  路  Source: aws-amplify/amplify-js

* Which Category is your question related to? *
Auth
* What AWS Services are you utilizing? *
Cognito, AWS AppSync
* Provide additional details e.g. code snippets *

I'd like to know how to save the token generated when a user logs in, this is the code I'm using for this functionality:

async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault();

    try {
      await Auth.signIn("[email protected]", "MyAwesomePassword");
      Router.push("/account");
    } catch (error) {
      setErrorMessage(error.message);
    }
  }

So, when the user is redirected to the account route, I fetch the token of this user with the method Auth.currentAuthenticatedUser(); but this one does not exist. This is because the token is not saved... I've been looking for information and I could find this what I want to know is if this example is a good practice to implement it or someone has a better code for this? Thank you!

React SSR question

All 3 comments

Two things could be at fault here:

  1. There was a race-condition with Auth.currentAuthenticatedUser() where the token wasn't immediately available after logging in, but has since been fixed.

  2. 6146 automatically handles tokens for Next.js sot hey can be passed to the server.

    Please see my synopsis on changes here: https://github.com/aws-amplify/amplify-js/issues/5435#issuecomment-692990722

Please update to @latest and if you're still running into issues, please open an issue using our new issue template, so that I can quickly reproduce it & respond. 馃檹

Yeah, I've been following how SSR will work on Amplify, and it's wonderful!

Awesome! Thanks for your support @MontoyaAndres! 馃槏

Was this page helpful?
0 / 5 - 0 ratings