3

I'm using Amplify Flutter to implement user authentication in my Flutter app and can't figure out the following two things:

  1. Auto-login: How do I implement auto-login so that users remain logged in when closing and reopening the app over time (i.e. the app remains in logged-in state indefinitely unless users specifically log out of the app)?

  2. authToken: When the user is logged in, how do I get the authToken needed to add to my outgoing api requests (from the app to my aws backend)?

Been scouring the internet (and reading through the official docs) for solutions but can't seem to find much info on this or figure it out.

Camron
  • 503
  • 5
  • 19
  • I'm having the same issue and can't find anything to solve it. How are you doing with it? – Neo Jul 25 '21 at 13:49
  • @Neo to be frank, I got it all to work but other issues arose with Amplify Flutter so I completely switched over to Firebase and it has been a breeze comparatively. But looking at my old notes, to get the token with Amplify Flutter, follow [these instructions](https://docs.amplify.aws/lib/auth/access_credentials/q/platform/flutter). You can then call the following on the result to get the access token: `res.userPoolTokens.accessToken`. Then of course whatever backend your app is communicating with has to authenticate that token (using Amplify SDK). – Camron Jul 26 '21 at 18:59
  • @Neo as for auto-login, the above link also mentions that there is an `isSignedIn` flag that you can use. So you should be able to do the following: `if (res.isSignedIn) { // the code for whatever you want to do, e.g. skip login page and take user straight to home page. } ` Good luck! – Camron Jul 26 '21 at 19:07
  • thank you for answering. I also got it working and I had read that documentation however, it's not working properly I thing because it is rebuilding constantly my Builder widget. It's a shame because I was very much into AWS stack but they seem to be too green yet (am I'm also green). I also was considering moving to Firebase. – Neo Jul 27 '21 at 06:39

1 Answers1

-1

I was experiencing this issue as well and came across Amplify's Authenticator widget.

It's pretty easy to setup and works very well with little to no effort from the developer. It infers configuration from your Amplify config dart file, even allows for extensive customization over the UI's behavior.

@override
Widget build(BuildContext context) {
    return Authenticator(
        child: MaterialApp(
            builder: Authenticator.builder(),
            home: const AppContainer(),
        ),
    );
}

Full docs can be found here

Garrett
  • 1,576
  • 4
  • 27
  • 51