firebase onauthstatataTataTaTaTaTaTeChanged无限循环节点JS
我正在使用firebase在节点JS应用程序上签名我的用户。我想看看用户是否经过身份验证,然后将其重定向到我想要的页面(如果未记录或仪表板登录)。
但是,当我重定向用户(如果先前未记录或会话到期)时,它在同一页面上循环(我在登录页面上时每次发送登录页面的重定向)。
我实际使用的功能:
function authenficated (req, res, next) {
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
console.log("connected" + " " + user.uid);
next()
} else {
console.log("disconnected")
res.redirect('/') //loop on / page
next()
}
});
}
我想要一个函数,如果我的用户登录是否记录,如果它记录了我的节点后端返回到 /仪表板或我想要的其他页面,如果没有,则无法访问仪表板,并且它会自动返回到 /或 /登录
我指定我不使用react或vue,我简单地使用ejs显示我的页面,
谢谢
I'm using firebase to sign in my users on my node js app. I would like to see if the user is authentificated or not and after it redirect to the page I want (login if it not logged or dashboard).
But when I redirect user (if it not logged previously or session expires) it's looping on the same page (send redirect of the login page everytime when I'm on login page).
My function that I use actually :
function authenficated (req, res, next) {
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
console.log("connected" + " " + user.uid);
next()
} else {
console.log("disconnected")
res.redirect('/') //loop on / page
next()
}
});
}
I would like a function that provides if my user is logged or not, if it logged my node backend return to /dashboard or other pages that I want and if not it cannot access to dashboard and it return automatically to / or /login
I specify I don't use React or Vue, I use simply EJS to display my pages
Thanks for all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此功能/SDK用于前端应用程序,而不是后端应用程序。您需要为此进行管理SDK。您可以使用cookie,并且管理员SDK提供了创建cookie的功能。登录后,您将cookie连接到标头,并将通过浏览器按每个请求发送。如果cookie标头为空的,则比您所不知道的未登录。要注销用户,您可以添加一个头方法清除cookie。
This function/sdk is meant for frontend applications and not backend apps. You need to the admin sdk for that. You can use cookies and the admin sdk provides a function to create cookies. After a signin you attach the cookie to the headers and it will be send by the browser on every request. If the cookie header is empty than you know the user isn't signed in. To logout a user you can add a head method to clear the cookie.
要使用后端功能,您需要使用 admin sdk。此功能是前端功能( web sdk)。
您可以在前端使用Onauthstatatechang,并从前端重定向。请记住,Onauthstatatechanged将在每个页面上发射。
或实现 cookies> cookies 像以前的注释一样。
或
通过HTTP请求(Fetch或Axios)从客户端发送ID令牌,并使用Admin SDK验证服务器端。这是特定链接。该解决方案将要求您在前端加载一些东西,然后将HTTP请求发送到后端,验证,然后在后面发送受保护的资源。
另一方面,cookie在每个请求中都会发送到后端,因此,如果页面加载请求中没有cookie,则显然没有用户。或者,如果以下功能失败,则服务器将不会发送受保护的资源。 (这在上面的链接中为cookie说明)
To use backend function you need to use the admin sdk. This function is a front end function (web sdk ).
You can use onAuthStateChanged on the front end and redirect them from the front end. Remember onAuthStateChanged will fire on every page load.
OR implement cookies like the previous comments.
OR
Send the id token from the client via http request (fetch or axios) and verify server side using the admin sdk. Here is the specific link. This solution would require you to load something on the front end though and then send a http request to the backend, verify, then send protected resources after.
Cookies on the other hand are sent to the backend with every request, so if no cookie is present on the page load request then obviously there is no user. Or if the below function fails then server wont send protected resources. (this is explained in the link above for cookies)