Swi*_*ter 10 dart firebase firebase-authentication flutter
我的 flutter 应用程序中有一个使用 firebase 身份验证的用户管理功能。我可以使用firebase_auth的createUserWithEmailAndPassword()功能注册新用户帐户。
return await FirebaseAuth.instance.
createUserWithEmailAndPassword(email: email, password: password);
Run Code Online (Sandbox Code Playgroud)
问题是注册成功后,FirebaseAuth即使我已经登录,它也会自动将我的实例验证为新用户。
我遇到了这个答案:Firebase 踢出当前用户,但它是在 javascript 中并且有一个稍微不同的 api。
我怎样才能在飞镖中做等价的?
Swi*_*ter 24
更新:firebase_core ^0.5.0并firebase_auth ^0.18.0+1弃用了一些旧类。
下面是为firebase_core ^0.5.1和更新的代码firebase_auth ^0.18.2。
static Future<UserCredential> register(String email, String password) async {
FirebaseApp app = await Firebase.initializeApp(
name: 'Secondary', options: Firebase.app().options);
try {
UserCredential userCredential = await FirebaseAuth.instanceFor(app: app)
.createUserWithEmailAndPassword(email: email, password: password);
}
on FirebaseAuthException catch (e) {
// Do something with exception. This try/catch is here to make sure
// that even if the user creation fails, app.delete() runs, if is not,
// next time Firebase.initializeApp() will fail as the previous one was
// not deleted.
}
await app.delete();
return Future.sync(() => userCredential);
}
Run Code Online (Sandbox Code Playgroud)
原答案
我尝试了 firebase 身份验证 api,我目前的工作解决方案是:
// Deprecated as of `firebase_core ^0.5.0` and `firebase_auth ^0.18.0`.
// Use code above instead.
static Future<FirebaseUser> register(String email, String password) async {
FirebaseApp app = await FirebaseApp.configure(
name: 'Secondary', options: await FirebaseApp.instance.options);
return FirebaseAuth.fromApp(app)
.createUserWithEmailAndPassword(email: email, password: password);
}
Run Code Online (Sandbox Code Playgroud)
本质上它归结为创建一个新实例,FirebaseAuth因此自动登录createUserWithEmailAndPassword()不会影响默认实例。
根据Swift 的回答,我发布了此解决方案的更新代码。
Future signUpWithEmailPasswordAdminPastor(String email, String password, String role, String nama, String keuskupan, String kevikepan, String paroki) async {
try {
FirebaseApp tempApp = await Firebase.initializeApp(name: 'temporaryregister', options: Firebase.app().options);
UserCredential result = await FirebaseAuth.instanceFor(app: tempApp).createUserWithEmailAndPassword(
email: email, password: password);
// create a new document user for the user with uid
await DatabaseService(uid: result.user.uid).updateUserDataSelf(
true,
role,
nama,
keuskupan,
kevikepan,
paroki,
'',
DateTime.now(),
DateTime.now());
tempApp.delete();
return 'OK';
} catch (e) {
print(e.toString());
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码适用于firebase_core: ^0.5.0和firebase_auth: ^0.18.0+1。
| 归档时间: |
|
| 查看次数: |
4419 次 |
| 最近记录: |