Var*_*hal 8 javascript node.js express firebase firebase-authentication
我在我的项目中使用Firebase,但auth/operation-not-supported-in-this-environment在使用Google凭据登录时出现此错误.
.hbs文件代码
<span class="google-bg session-icon">
<a href="#!" id="google" onclick=" return loginWithGoogle(this)">
<i class="ti-google"></i>
</a>
</span>
Run Code Online (Sandbox Code Playgroud)
脚本代码
function loginWithGoogle(event){
$.ajax({
url: "/session/google/login",
type: "POST"
})
.done(function (data) {
error= JSON.stringify(data);
console.log(error);
M.toast({html: error})
});
}
Run Code Online (Sandbox Code Playgroud)
快递代码
router.post('/session/google/login',function (req, res, next){
//console.log('get resqust');
firebase.auth().signInWithRedirect(googleAuthProvider).then(function(result){
console.log(result);
}, function(error){
console.log(error.code);
res.json({
data: error.code
})
});
})
Run Code Online (Sandbox Code Playgroud)
当我点击Google图标然后调用loginWithGoogle函数并获得路由器路径/session/google/login后执行快速代码并生成错误.我想知道解决这个问题,可能出现什么问题?
谢谢!!!
更新(16-10-18)
使用Gmail帐户成功登录后,请拨打信息中心路线.
router.get('/dashboard', function(req, res, next) {
console.log(firebase.auth().currentUser);
if(firebase.auth().currentUser != null){
res.render('dashboard', {
title: 'App'
});
}else {
req.session.error = 'Access denied!';
console.log(req.session.error);
res.redirect('/login');
}
});
Run Code Online (Sandbox Code Playgroud)
使用gmail帐户成功登录后,我在调用仪表板页面之前调用了仪表板路径和使用条件,但currentUser返回null.我已检查Firebase控制台,该控制台显示最近使用gmail的新用户登录,如果我使用简单的用户ID和密码登录,则currentUser返回数据.哪里错了?
更新17-10-18
function loginWithGoogle(event) {
firebase.initializeApp(config);
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(googleAuthProvider)
.then(function (user) {
// localStorage.setItem('user',JSON.stringify(user));
window.location.href = '/dashboard';
})
.catch(function (error) {
var errorCode = error.code;
var errorMessage = error.message;
var email = error.email;
var credential = error.credential
})
}
Run Code Online (Sandbox Code Playgroud)
成功登录后,我重定向到'/dashboard'表达呼叫定义的仪表板路由.我昨天提到过.现在请告诉我在哪里我称之为仪表板路线?
在开始之前,请检查您是否已将 Firebase 添加到前端(如果尚未这样做),这里是如何执行此操作的说明: https: //firebase.google.com/docs/web/setup
使用Google登录是在前端完成的,非常简单,您不需要更改html代码,只需像下面这样的javascript,不需要调用云函数
function loginWithGoogle(event){
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithRedirect(provider);
}
Run Code Online (Sandbox Code Playgroud)
这只是通过重定向登录时的用例之一,有弹出登录和更复杂的登录处理,您可以在使这一用例正常工作后尝试:https ://firebase.google.com /docs/auth/web/google-signin
对于对云功能进行身份验证的调用,您需要先获取令牌,然后进行调用。但您只有在登录后才能执行此操作。
function authenticatedCallToFunctions(event){
firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
$.ajax({
url: "someurl",
type: "POST",
headers: {"firebase-token": idToken}
})
.done(function (data) {
error= JSON.stringify(data);
console.log(error);
M.toast({html: error})
});
}).catch(function(error) {
// Handle error
});
}
Run Code Online (Sandbox Code Playgroud)
您可以在云功能中处理经过身份验证的请求,例如
import * as admin from 'firebase-admin';
router.post('someurl',function (req, res, next){
var token = req.headers['firebase-token'];
admin.auth().verifyIdToken(token).then(function(result){
console.log(result);
}, function(error){
console.log(error.code);
res.json({
data: error.code
})
});
})
Run Code Online (Sandbox Code Playgroud)
适用于 JavaScript 的 Firebase 身份验证 SDK 仅适用于浏览器,不适用于 Node.js 环境。如果您需要在后端使用 Firebase Auth,则需要使用Firebase Admin SDK,您可以用它来管理用户会话。
| 归档时间: |
|
| 查看次数: |
922 次 |
| 最近记录: |