简体   繁体   中英

Amplify flutter authentication

I am trying to implement Amplify in my flutter project. I wanna to get userSub just after signing up a new user without login and account verify. I am using email as a username. Some others suggested to use Amplify.Auth.fetchAuthSession() but I think I can't use this method without login. Any help?

You have to implement authentication after you can get user data. Check the Documentation from Amplify

SignUp

try {
  Map<String, String> userAttributes = {
    'email': 'email@domain.com',
    'phone_number': '+15559101234',
    // additional attributes as needed
  };
  SignUpResult res = await Amplify.Auth.signUp(
    username: 'myusername',
    password: 'mysupersecurepassword',
    options: CognitoSignUpOptions(
      userAttributes: userAttributes
    )
  );
  setState(() {
    isSignUpComplete = res.isSignUpComplete;
  });
} on AuthException catch (e) {
  print(e.message);
}

ConfirmSignUP

try {
  SignUpResult res = await Amplify.Auth.confirmSignUp(
    username: 'myusername',
    confirmationCode: '123456'
  );
  setState(() {
    isSignUpComplete = res.isSignUpComplete;
  });
} on AuthException catch (e) {
  print(e.message);
}

SignIn

try {
  SignInResult res = await Amplify.Auth.signIn(
    username: usernameController.text.trim(),
    password: passwordController.text.trim(),
  );
  setState(() {
    isSignedIn = res.isSignedIn;
  });
} on AuthException catch (e) {
  print(e.message);
}

Follow this video tutoril you can implement Amplify Auth using BLOC architecture

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM