how to auto login existing user in flutter

7,006

Since you are using firebase-authentication, then to know if the user is still logged in and navigate to a specific page, you can do the following:

FirebaseUser user = await FirebaseAuth.instance.currentUser();
if(user != null){
  // navigate to home page
}
else
{
// log in 
}

https://pub.dev/packages/firebase_auth

Share:
7,006
Sandeep Sharma
Author by

Sandeep Sharma

Updated on December 17, 2022

Comments

  • Sandeep Sharma
    Sandeep Sharma over 1 year

    I have 4 Screens :

    1.having two buttons with Login and SignUp(main.dart)

    2.login Screen. (checks if the user is authenticated and then push to the home page)

    3.SignUp Screen. (create the user and push the user to login)

    4.HomePage. (with Logout button)

    now, my question Is:

    1. Is this the correct way to do so?

    2. I want the existing user to auto-login. (Main problem) I know the concept of doing auto-login with login existing user. can you help? thanks.

    and I want to check here in main.dart if the user is authenticated or not if yes then push to home otherwise show login page which is in main.dart itself.

    main.dart
    
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primaryColor: Color.fromRGBO(255,188,114, 1),
          ),
          home: Loginpage (),
        );
      }
    }
    
    class Loginpage extends StatefulWidget{...}
    class _LoginpageState extends State<Loginpage>{...}
    
    • Aamil Silawat
      Aamil Silawat over 4 years
      Use sharedPreference to save data localy try this pub.dev/packages/shared_preferences
    • Aamil Silawat
      Aamil Silawat over 4 years
      Using the above plugin you can auto-login if once the user is registered
  • Sandeep Sharma
    Sandeep Sharma over 4 years
    thanks, I know how to get current user but how to use in main.dart?
  • Peter Haddad
    Peter Haddad over 4 years
    call it in initstate and navigate to specific pages according to the condition