4
votes

Hi I have spent a couple of days on trying to figure out why my user looses there "session" on my app when I close the app. How do I keep them signed in or what might cause this?

I am running the app on my google pixel 5 using the simulator option on android studio.

yaml file:

firebase_core: ^0.4.0+9
firebase_auth: ^0.14.0+9
cloud_firestore: ^0.13.6
firebase_storage: 3.1.6
firebase_messaging: 6.0.16
firebase_crashlytics: 0.1.4+1

This is what I can see in my logs (Not helpful much):

D/FlutterLocationService(32338): Unbinding from location service. D/FlutterLocationService(32338): Destroying service. D/FlutterLocationService(32338): Creating service. D/FlutterLocationService(32338): Binding to location service.

flutter doctor -v:

(base) darrendupreez@Darrens-MacBook-Pro unavine_app % flutter doctor -v [✓] Flutter (Channel dev, 1.26.0-12.0.pre, on macOS 11.1 20C69 darwin-x64, locale en-CA) • Flutter version 1.26.0-12.0.pre at /Applications/flutter • Framework revision a706cd2112 (2 weeks ago), 2021-01-14 18:20:26 -0500 • Engine revision effb529ece • Dart version 2.12.0 (build 2.12.0-224.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /Users/darrendupreez/Library/Android/sdk • Platform android-30, build-tools 30.0.3 • Java binary at: /Users/darrendupreez/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/201.7042882/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.4, Build version 12D4e • CocoaPods version 1.10.1

[✓] Android Studio (version 4.1) • Android Studio at /Users/darrendupreez/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/201.7042882/Android Studio.app/Contents • Flutter plugin can be installed from: ???? https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: ???? https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)

[✓] Connected device (1 available) • Pixel 5 (mobile) • 0C161FDD4000DT • android-arm64 • Android 11 (API 30)

• No issues found!

3
Are you using a Providers and AuthCreditental for this ?Furkan
Hi Yes using Facebook Login and using Firebase as my backend.Darren-Jaen

3 Answers

3
votes

You can use shared_preferences: ^0.5.12+4 to save the session.Just check whether it is null or not. And when you need you can update the data with new login.

Sample:

SharedPreferences prefs = await SharedPreferences.getInstance(); int counter = (prefs.getInt('counter') ?? 0) + 1; print('Pressed $counter times.');

2
votes

Providers and AuthCreditental should work here is an example for google login with firebase

main.dart

   class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return Provider(
          create: (context) => AuthBloc(),
          child: MaterialApp(
            title: 'Your App',
            debugShowCheckedModeBanner: false,
            theme: ThemeData(
              primarySwatch: MaterialColor(0xff360A36, color),
              accentColor: Colors.limeAccent,
            ),
            //darkTheme: ThemeData.dark(),
            home: LogInPage(),
          ),
        );
      }

}

auth_bloc.dart

class AuthBloc {
  final authService = AuthService();
  final googleSignin = GoogleSignIn(scopes: ['email']);

  Stream<User> get currentUser => authService.currentUser;

  loginGoogle() async {
    try {
      final GoogleSignInAccount googleUser = await googleSignin.signIn();
      final GoogleSignInAuthentication googleAuth =
          await googleUser.authentication;
      final AuthCredential credential = GoogleAuthProvider.credential(
          idToken: googleAuth.idToken, accessToken: googleAuth.accessToken);

      //Firebase Sign in
      final result = await authService.signInWithCredential(credential);
      print('${result.user.displayName}');
    } catch (error) {
      print(error);
    }
  }


  logout() {
    authService.logout();
    googleSignin.signOut();
  }

sign in button

  SignInButton(
            Buttons.Google,
            text: "Sign in with Google",
            onPressed: () => authBloc.loginGoogle(),
          ),

Also you can watch this tutorial for using credentials and providers

https://www.youtube.com/watch?v=_uYO2ht5Nl4&list=PL19w2HZFNl2BsftambzhFE7tVrqGEVwzy&index=1&t=1924s

0
votes

Firebase auth automatically manage the auth state for your project but you have to access that auth state in your project to keep user data in the application. You can use the authStateChange function to make it work.

Stream<User> get currentUser =>  _auth.authStateChanges();

And the user state in your main function using the Provider Package. Also wrap your `MaterialApp()' function by the following code:

MultiProvider(
    providers: [
      StreamProvider<User>.value(
        value: Auth().currentUser, 
      )
    ],
      child: GetMaterialApp()
)