在我的应用程序中,我没有用户登录,但我想保护我的 Firebase 数据。我用于此 Firebase token 生成器 - PHP。访问数据库就像俄罗斯轮盘赌。一旦我获得权限,就不会在我刷新页面时使用。
这是我的 token 生成器文件:
<?php
require_once 'vendor/autoload.php';
use Firebase\Token\TokenException;
use Firebase\Token\TokenGenerator;
try {
$generator = new TokenGenerator('Firebase-Secret');
$token = $generator
->setData(array('uid' => '123'))
->setOption('admin', true)
->create();
} catch (TokenException $e) {
echo "Error: ".$e->getMessage();
}
echo $token;
?>
这里是 index.php:
<script>var token = "<?= include_once 'getToken.php' ?>";</script>
$(document).ready(function(){
firebaseRef.authWithCustomToken(token, function(error, authData) {
console.log(authData)
}, {remember: "sessionOnly" });
// do something
})
最佳答案
以后请附上一个minimal, complete repro为您的调试问题。您很可能遇到了 asynchronous issues .您的 //do something
代码可能需要移动到身份验证的回调中。
$(document).ready(function(){
firebaseRef.authWithCustomToken(token, function(error, authData) {
console.log(authData);
if( !error ) {
// run your authenticated code here
}
}, {remember: "sessionOnly" });
// trying to run something here that requires auth will
// be non-deterministic
});
关于javascript - Firebase JWT 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32119748/