how to auto login existing user in flutter
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
Sandeep Sharma
Updated on December 17, 2022Comments
-
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:
Is this the correct way to do so?
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 over 4 yearsUse sharedPreference to save data localy try this pub.dev/packages/shared_preferences
-
Aamil Silawat over 4 yearsUsing the above plugin you can auto-login if once the user is registered
-
Sandeep Sharma over 4 yearsthanks, I know how to get current user but how to use in main.dart?
-
Peter Haddad over 4 yearscall it in initstate and navigate to specific pages according to the condition