How to Signout a user in Flutter with Firebase authentication

穿精又带淫゛_ 提交于 2020-08-22 07:43:22

问题


I have a problem signing out the current user from my app

the method I am using is as follows:

....
onPressed:_signOut
//jump to function


  void _signOut() {
  FirebaseAuth.instance.signOut();
  FirebaseUser user = FirebaseAuth.instance.currentUser;
  //print('$user');
  runApp(
      new MaterialApp(
        home: new LoginPage(),
      )

  );
}

So now when I press the button it should sign the user out and redirect them to the home page which they will have to login again, however, the redirecting happens but the user data would still be saved so when I press the button again it automatically sign in again with the last account. How can I remove user data so the app asks about their credentials each time they try to login after a logout ?

I feel I am missing something in the linkage between pages and how their behavior change accordingly, but what is it ?

Update: I use google sign in function with firebase authentication

   Future<String> _testSignInWithGoogle() async {
  final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
  final GoogleSignInAuthentication googleAuth =
  await googleUser.authentication;
  final FirebaseUser user = await _auth.signInWithGoogle(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );
  assert(user.email != null);
  assert(user.displayName != null);
  assert(!user.isAnonymous);
  assert(await user.getToken() != null);
return 'signInWithGoogle succeeded: $user';
}

My login page looks like this:

    class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return new Scaffold(
        appBar: new AppBar(
          title: new Text("Login"), backgroundColor: Colors.blue,),
        body: new Container(
            child: new Center(
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new IconButton(
                      icon: new Icon(Icons.account_box, color: Colors.red),
                      onPressed:  _signIn,
                      iconSize: 80.0,),
                    new Text("Google Signin")
                  ],
                )
            )
        )
    );
  }
}

Update: Changed _signOut() method to be async as follows:

    Future <LoginPage> _signOut()  async{
    await FirebaseAuth.instance.signOut();

    return new LoginPage();
}

Now when I press on signout, it does not redirect me to the LoginPagae nor does it sign the user out.


回答1:


Firebase auth's signOut method is asynchronous. You should make your _signOut method async and call await FirebaseAuth.instance.signOut(); so that the call to runApp occurs after the user is signed out.

You should also call _googleSignIn.signOut() when logging out if you want signIn to present the user with an authentication dialog instead of silently and automatically re-using the current Google user.




回答2:


You need to have an Instances of FirebaseAuth

  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

And Then

  _signOut() async {
    await _firebaseAuth.signOut();
   }



回答3:


Most demos I've looked at only logout of Firebase using a command like _auth.signOut();

This doesn't appear to exist anymore (see Collin's reply above):

_googleSignIn.signOut()

So, I had to use this one method to signout/logout of Google.

_googleSignIn.disconnect();



回答4:


First create an instance of FirebaseAuth like so

 FirebaseAuth auth = FirebaseAuth.instance;

Then add this to either your logout button or any means you wish to use for the logout.

signOut() async {
    await _firebaseAuth.signOut();
  }

You can also create a function and then call the signOut within your button like so

import 'package:flutter/material.dart';

class SignOut extends StatefulWidget {
  @override
  _ SignOut State createState() => _ SignOut State();
}

class _ SignOut State extends State< SignOut > {

     FirebaseAuth auth = FirebaseAuth.instance;
    
      signOut() async {
        await _firebaseAuth.signOut();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.white,
          body: Center(
            child: Container(
              child: RaisedButton(
                onPressed: () {
                  signOut();
                },
    
              )
            ),
          ),
        );
      }
    }

You decide.



来源:https://stackoverflow.com/questions/45079459/how-to-signout-a-user-in-flutter-with-firebase-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!