Flatter firebase电话身份验证-如何获取身份验证令牌
在我的应用程序中,我有电子邮件和电话身份验证,当我用电子邮件登录时,它会给我一个长的身份验证令牌,我可以用它来读写数据库,但是当我用电话登录时,我只得到一个短的令牌,它似乎不符合身份验证令牌的位置,因为它既不读也不写数据库。你知道我怎样才能得到正确的身份验证令牌吗Flatter firebase电话身份验证-如何获取身份验证令牌,firebase,firebase-authentication,flutter,Firebase,Firebase Authentication,Flutter,在我的应用程序中,我有电子邮件和电话身份验证,当我用电子邮件登录时,它会给我一个长的身份验证令牌,我可以用它来读写数据库,但是当我用电话登录时,我只得到一个短的令牌,它似乎不符合身份验证令牌的位置,因为它既不读也不写数据库。你知道我怎样才能得到正确的身份验证令牌吗 String phoneNo; String smsCode; String verificationId; String uid; Future<void> verifyPhone() async
String phoneNo;
String smsCode;
String verificationId;
String uid;
Future<void> verifyPhone() async {
final PhoneCodeAutoRetrievalTimeout autoRetrieve = (String verId) {
this.verificationId = verId;
};
final PhoneCodeSent smsCodeSent = (String verId, [int forceCodeResend]) {
this.verificationId = verId;
smsCodeDialog(context).then((value) {
print('Signed in');
});
};
final PhoneVerificationCompleted verifiedSuccess = (FirebaseUser user) {
print('verified');
};
final PhoneVerificationFailed veriFailed = (AuthException exception) {
print('${exception.message}');
};
await FirebaseAuth.instance.verifyPhoneNumber(
phoneNumber: this.phoneNo,
codeAutoRetrievalTimeout: autoRetrieve,
codeSent: smsCodeSent,
timeout: const Duration(seconds: 5),
verificationCompleted: verifiedSuccess,
verificationFailed: veriFailed);
}
Future<bool> smsCodeDialog(BuildContext context) {
return showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return new AlertDialog(
title: Text('Enter sms Code'),
content: TextField(
onChanged: (value) {
this.smsCode = value;
},
),
contentPadding: EdgeInsets.all(10.0),
actions: <Widget>[
new FlatButton(
child: Text('Done'),
onPressed: () {
FirebaseAuth.instance.currentUser().then((user) {
if (user != null) {
setState(() {
this.uid = user.uid;
});
_submitForm(widget.model.authenticatePhone, 2);
Navigator.of(context).pop();
Navigator.of(context).pushReplacementNamed('/');
} else {
Navigator.of(context).pop();
signIn();
}
});
},
)
],
);
});
}
signIn() {
FirebaseAuth.instance
.signInWithPhoneNumber(verificationId: verificationId, smsCode: smsCode)
.then((user) {
Navigator.of(context).pushReplacementNamed('/');
}).catchError((e) {
print(e);
});
}
从数据库中读取:
http.Response responseToken;
responseToken = await http.put(
'https://app.firebaseio.com/tokens/${_authenticatedUser.id}/token.json?auth=${_authenticatedUser.token}',
body: json.encode(token));
return http
.get(
'https://app.firebaseio.com/products.json?auth=${_authenticatedUser.token}')
使用电子邮件和密码登录firebase auth不必如此复杂。只要这样做:
FirebaseUser user = await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: password
);
uid = user.uid;
而且,对数据库的读写应该通过firebase方式完成,而不是旧式的http方式
写作:
FirebaseDatabase.instance.reference("users/$uid").set("test");
阅读:
final data = await FirebaseDatabase.instance.reference("users/$uid").once();
您通常使用当前用户的uid来读取和写入数据库,而不是身份验证令牌。@dshukertjr这对我来说似乎不起作用,uid大约只有28个字符,而电子邮件的身份验证大约有3-4行字符,这似乎不确定why@dshukertjr当我试着写的时候,我得到了这个错误{错误:无法分析身份验证令牌。}没错。28个字符的uid是你应该使用的。你能把你用于电子邮件和密码登录的代码以及读/写代码发布到数据库吗?@dshukertjr我刚刚添加了代码,在电子邮件中我正在使用idToken进行身份验证。虽然这是用于电子邮件的,但电话uid对m的身份验证仍然不起作用它是同一个uid。uid的长度应该是28个字符。我知道,只是我直接写了uid而不是${u authenticatedUser.token},它仍然不会为我写,继续得到{错误:无法解析身份验证令牌。}但如果我硬编码的电子邮件idToken它的工作完美fine@DarwishAl-Neyadi我已经编辑了我的答案。Firebase与普通服务器完全不同,你可以使用Firebase插件进行任何操作,因此请使用这些插件,而不是使用http。是的,你可以。这是关于web的,但Flatter是一样的。
final data = await FirebaseDatabase.instance.reference("users/$uid").once();