【发布时间】:2021-02-21 04:37:07
【问题描述】:
通过使用“firebase serve --only hosting,functions”解决了这个问题 具有生产身份验证的 verifyIdToken 正在按预期工作
我正在使用 firebase 构建一个 SPA,并在我的后端使用 Express.js 进行用户验证和路由。
在我的登录脚本中,我使用 firebase-u-auth 进行 Google 登录,然后如果用户登录
var app = firebase.initializeApp(config);
firebase.auth(app).setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.then(() => {
firebase.auth(app).onAuthStateChanged(function (user) {
if (!user)
return;
redirectSuccesful(user);
});
});
function redirectSuccesful(user) {
// for development purposes, enable insecure cookies for http on local server
let secure = window.location.protocol.toLowerCase() === 'https:' || (window.location.hostname !== 'localhost' && window.location.hostname !== '127.0.0.1' && window.location.hostname !== '0.0.0.0');
user.getIdToken().then((token) => {
let expDate = expiryDateFromJwt(token);
let cookies = `__session=${token}; samesite=strict; path=/${expDate ? '; expires=' + expDate.toUTCString() : ''}${secure ? '; secure' : ''}`;
document.cookie = cookies;
window.location.assign('/');
});
}
在我的函数文件夹中,我使用 express with
const serviceAccount = require('...json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "<my-url>"
});
expressApp.get('/', wrap (async function(req, res){
const token = getSessionToken(req); //custom function that gets clean saved token from session
if(token){
const decodedToken = await admin.auth().verifyIdToken(token);
...
}
}))
如果我在https://jwt.io/ 中传递令牌,它似乎是正确的,但是函数 verifyIdToken(token) 显示“Firebase ID 令牌的算法不正确。预期为“无”但得到“RS256。有关如何检索 ID 令牌的详细信息,请参阅 https://firebase.google.com/docs/auth/admin/verify-id-tokens。”
错误来自哪里?管理员配置?
谢谢!
【问题讨论】:
-
您是否在本地(模拟器)收到错误?还是在生产中(已部署)?
-
当您使用模拟器运行 Admin SDK 时会发生这种情况,但给定了一个实际的 ID 令牌。使用模拟器运行时,Admin SDK 只会验证模拟器发出的 ID 令牌。
-
@HiranyaJayathilaka Jayathilaka:是的,我在模拟器中运行它。感谢您提供此信息!现在很清楚为什么会出现这个错误。但是我不能以某种方式强迫模拟器对生产进行身份验证吗?因为我针对生产环境初始化我的应用程序?谢谢大家!
-
据我所知,没有办法做到这一点。当您使用模拟器运行 Admin SDK 时,假定任何相关的客户端组件也与模拟器一起运行。您可以在github.com/firebase/firebase-tools 提交您对此行为的任何具体反馈
-
@HiranyaJayathilaka "firebase serve --only hosting,functions" 解决了我的问题,它使用生产身份验证服务器,但使用本地托管和功能
标签: node.js firebase express firebase-authentication firebase-admin