gpt4 book ai didi

javascript - 登录可以在 Chrome 和 Firefox 上使用,但不能在 Edge 上使用

转载 作者:行者123 更新时间:2023-12-02 23:22:10 25 4
gpt4 key购买 nike

我的 Node.js 本地服务器上有一个 Firebase 身份验证系统。我已经设置了登录系统,以便用户使用前端登录。发送一个获取请求并返回一个 session cookie。这在 Chrome 和 Firefox 上工作正常,但在 Edge 上我在浏览器控制台和服务器控制台上都收到错误。

我检查了 token 是否确实发送到服务器,确实如此。

这是带有 firebase 和 cookie 解析器的登录处理程序


/** Session login endpoint. */
app.post("/sessionLogin", function(req, res) {
// Get ID token and CSRF token.
var idToken = req.body.idToken.toString();
var csrfToken = req.body.csrfToken.toString();

// Guard against CSRF attacks.
if (!req.cookies || csrfToken !== req.cookies.csrfToken) {
res.status(401).send("UNAUTHORIZED REQUEST!");
return;
}
// Set session expiration to 5 days.
var expiresIn = 60 * 60 * 24 * 5 * 1000;
// Create the session cookie. This will also verify the ID token in the process.
// The session cookie will have the same claims as the ID token.
// We could also choose to enforce that the ID token auth_time is recent.
admin
.auth()
.verifyIdToken(idToken)
.then(function(decodedClaims) {
// In this case, we are enforcing that the user signed in in the last 5 minutes.
if (new Date().getTime() / 1000 - decodedClaims.auth_time < 5 * 60) {
return admin
.auth()
.createSessionCookie(idToken, { expiresIn: expiresIn });
}
throw new Error("UNAUTHORIZED REQUEST!");
})
.then(function(sessionCookie) {
// Note httpOnly cookie will not be accessible from javascript.
// secure flag should be set to true in production.
console.log(sessionCookie);

var options = {
maxAge: expiresIn,
httpOnly: true,
secure: false /** to test in localhost */
};
res.cookie("session", sessionCookie, options);
res.end(JSON.stringify({ status: "success" }));
})
.catch(function(error) {
res.status(401).send("UNAUTHORIZED REQUEST!");
});
});

这是配置文件获取请求处理程序:

//Get profile endpoint. */
app.get("/profile", function(req, res) {
// Get session cookie.
var sessionCookie = req.cookies.session || "none";
// Get the session cookie and verify it. In this case, we are verifying if the
// Firebase session was revoked, user deleted/disabled, etc.
admin
.auth()
.verifySessionCookie(sessionCookie, true /** check if revoked. */)
.then(function(decodedClaims) {
// Serve content for signed in user.
return serveContentForUser("/profile", req, res, decodedClaims);
})
.catch(function(error) {
console.log("error: ", error);

// Force user to login.
res.redirect("/login");
});
});

前端错误:

HTTP401: DENIED - The requested resource requires user authentication.
(Fetch)POST - http://192.168.1.9/sessionLogin

后端错误

{ Error: Decoding Firebase session cookie failed. Make sure you passed the entire string JWT which represents a session cookie. See https://firebase.google.com/docs/auth/admin/manage-cookies for details on how to retrieve a session cookie.
at FirebaseAuthError.FirebaseError [as constructor] (C:\Users\mendi\Desktop\firebase-auth\node_modules\firebase-admin\lib\utils\error.js:42:28)
at FirebaseAuthError.PrefixedFirebaseError [as constructor] (C:\Users\mendi\Desktop\firebase-auth\node_modules\firebase-admin\lib\utils\error.js:88:28)
at new FirebaseAuthError (C:\Users\mendi\Desktop\firebase-auth\node_modules\firebase-admin\lib\utils\error.js:146:16)
at FirebaseTokenVerifier.verifyJWT (C:\Users\mendi\Desktop\firebase-auth\node_modules\firebase-admin\lib\auth\token-verifier.js:158:35)
at Auth.BaseAuth.verifySessionCookie (C:\Users\mendi\Desktop\firebase-auth\node_modules\firebase-admin\lib\auth\auth.js:318:43)
at C:\Users\mendi\Desktop\firebase-auth\app.js:179:6
at Layer.handle [as handle_request] (C:\Users\mendi\Desktop\firebase-auth\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\mendi\Desktop\firebase-auth\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\mendi\Desktop\firebase-auth\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\mendi\Desktop\firebase-auth\node_modules\express\lib\router\layer.js:95:5)
errorInfo:
{ code: 'auth/argument-error',
message:
'Decoding Firebase session cookie failed. Make sure you passed the entire string JWT which represents a session cookie. See https://firebase.google.com/docs/auth/admin/manage-cookies for details on how to retrieve a session cookie.' },
codePrefix: 'auth' }

也许这是我的错误或者是浏览器的问题。但我认为它不必对我的代码做任何事情。

最佳答案

我发现 Edge 41.16299 也有类似的问题,该问题已在最新更新中修复。因此,我建议您安装最新的Windows更新,并检查它是否有助于解决问题。引用:developer.microsoft.com/en-us/microsoft-edge/platform/issues

关于javascript - 登录可以在 Chrome 和 Firefox 上使用,但不能在 Edge 上使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56903284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com