首页 文章

如何使用Firebase身份验证在Flutter中注销用户

提问于
浏览
3

我从我的应用程序退出当前用户时遇到问题

我使用的方法如下:

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


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

  );
}

所以,现在当我按下按钮时,它应该签出用户并将它们重定向到主页,他们将不得不再次登录,但是,重定向发生但用户数据仍然会被保存,所以当我再次按下按钮时它会自动使用最后一个帐户重新登录 . 如何删除用户数据,以便应用程序在每次注销后尝试登录时询问他们的凭据?

我觉得我错过了页面之间的联系以及他们的行为如何相应地改变的东西,但是它是什么?

更新:我使用gobase登录功能和firebase身份验证

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';
}

我的登录页面如下所示:

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")
                  ],
                )
            )
        )
    );
  }
}

更新:将_signOut()方法更改为异步,如下所示:

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

    return new LoginPage();
}

现在,当我按下注销时,它不会将我重定向到LoginPagae,也不会签署用户 .

1 回答

  • 5

    Firebase auth的 signOut 方法是异步的 . 您应该使 _signOut 方法 async 并调用 await FirebaseAuth.instance.signOut(); ,以便在用户注销后调用 runApp .

    如果您希望 signIn 向用户显示身份验证对话框而不是静默并自动重新使用当前Google用户,则还应在注销时调用 _googleSignIn.signOut() .

相关问题