javascript - 使用 express nodejs 获取此错误 auth/operation-not-supported-in-this-environment
问题描述
我在我的项目中使用 Firebase,但在auth/operation-not-supported-in-this-environment
使用谷歌凭据登录时出现此错误。
.hbs 文件代码
<span class="google-bg session-icon">
<a href="#!" id="google" onclick=" return loginWithGoogle(this)">
<i class="ti-google"></i>
</a>
</span>
脚本代码
function loginWithGoogle(event){
$.ajax({
url: "/session/google/login",
type: "POST"
})
.done(function (data) {
error= JSON.stringify(data);
console.log(error);
M.toast({html: error})
});
}
快递代码
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
})
});
})
当我点击谷歌图标然后调用函数并在执行快速代码并生成错误之后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');
}
});
使用 gmail 帐户成功登录后,我在渲染仪表板页面之前调用了仪表板路由和使用条件,但currentUser
返回 null。我检查了 Firebase 控制台,该控制台显示最近使用 gmail 登录的新用户,如果我使用简单的用户名和密码登录,则currentUser
返回数据。我哪里错了??
2018 年 17 月 10 日更新
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
})
}
成功登录后,我已重定向到'/dashboard'
仪表板并表示呼叫定义的路由。我昨天提到过。现在请告诉我我在哪里调用仪表板路线?
解决方案
在您开始之前检查您是否已将 Firebase 添加到您的前端(如果您还没有这样做的话)这里是如何做到这一点的说明:https ://firebase.google.com/docs/web/setup
用谷歌登录是在前端完成的,非常简单,你不需要改变你的html代码,只需要像下面这样的javascript,不需要调用云函数
function loginWithGoogle(event){
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithRedirect(provider);
}
这只是您通过重定向登录时的一种用例,有弹出式登录和更复杂的登录处理,您可以在使其工作后尝试: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
});
}
您可以在云功能中处理经过身份验证的请求,例如
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
})
});
})
推荐阅读
- cakephp - 元素视图在 CakePHP 4.x 的控制器中不起作用
- java - OneM2M:在 FTP 服务器上加载文件后获取通知
- three.js - 在十二面体表面上创造恒星
- flutter - 禁用从剪贴板粘贴到颤振应用程序
- error-handling - 无法将笔记本上传到 jovian
- android - 即使应用程序被杀死或在后台,如何连续执行方法
- php - 重新安装 wamp 3.2.3 和 PHP 7.3 后的 PHP 错误
- node.js - 如何在 expressjs 中使用多个中间件?
- java - 在 Java 字符串中取 NOT 条件
- ios - 如何在后台 ios 13 swift 中从 CMMotionManager 获取运动更新