在不丢失当前状态(登录用户)的情况下创建多个用户凭据Firebase颤动 [英] Create Multiple User Credentials without loosing current state (logged in user) Firebase Flutter
本文介绍了在不丢失当前状态(登录用户)的情况下创建多个用户凭据Firebase颤动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
enter image description here我尝试了其他方法,但在我的情况下无效。请帮我解决这个问题。 示例代码::
class _TestScreenState extends State<TestScreen> {
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
final FirebaseFirestore _fs = FirebaseFirestore.instance;
final FirebaseAuth _auth = FirebaseAuth.instance;
bool _isLoading = false;
final User _currentUser = FirebaseAuth.instance.currentUser;
Future<void> _createUser() async {
try {
setState(() {
_isLoading = true;
});
await _auth
.createUserWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
)
.then((UserCredential _user) async {
try {
await _fs.collection('users').doc(_user.user.uid).set({
'email': _emailController.text,
'password': _passwordController.text,
}).then((value) {
setState(() {
_isLoading = false;
});
});
} catch (err) {
_user.user.delete();
print("User Deleted");
setState(() {
_isLoading = false;
});
}
});
} catch (err) {
print("Cannot Sign Up: $err");
setState(() {
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
print("USER--" + _currentUser.uid);
return Scaffold(
appBar: AppBar(title: Text("Test Screen")),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
TextField(
controller: _emailController,
),
SizedBox(
height: 20,
),
TextField(
controller: _passwordController,
),
SizedBox(height: 30),
ElevatedButton(
onPressed: _createUser,
child: Text(_isLoading ? "Creating..." : "Create User"),
),
],
),
),
);
}
}
推荐答案
在Firebase身份验证中,每个FirebaseAuth
对象都有一个登录用户。如果要登录多个用户,则需要基于显式FirebaseApp
对象(而不是默认的FirebaseAuth.instance
)的FirebaseAuth
class的多个实例。
请注意,这是一个常见的反模式。例如,希望在应用程序中拥有一个可以创建其他用户帐户的管理用户,通常通过创建自定义后端API(例如在云函数上)来更好地处理这一问题,然后您可以在其中创建专门针对此类管理功能的Admin SDK。
这篇关于在不丢失当前状态(登录用户)的情况下创建多个用户凭据Firebase颤动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文