简体   繁体   English

如何使用 firebase 认证重新认证用户?

[英]how to reauthenticate user with firebase authentication?

I'm new to firebase.我是 firebase 的新手。 In my nuxt js project with multiple pages, firestore rule is set to read or write once request.auth.= null.在我的具有多个页面的 nuxt js 项目中,firestore 规则设置为读取或写入一次 request.auth.= null。

so if when refresh in a page, auth will be gone and it display error 'permission-denied' in terminal.因此,如果在页面中刷新时,身份验证将消失,并在终端中显示错误“权限被拒绝”。

i tried Authentication State Persistence LOCAL but it doesn't work.我尝试了身份验证 State Persistence LOCAL 但它不起作用。 What is the purpose of using these auth persistence mode?使用这些 auth 持久化模式的目的是什么?

firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.then(function() {
return firebase.auth().signInWithEmailAndPassword(email, password);
})

Auth session is automatically persistent on the frontend of your app, however Nuxt.js contains frontend and backend part. Auth session 在您的应用程序的前端自动持久化,但是 Nuxt.js 包含前端和后端部分。 If you are storing user data somewhere on the frontend, you probably need to wait until the user data become accessible.如果您将用户数据存储在前端的某个位置,您可能需要等到用户数据变得可访问。

Example of the listener :监听器示例:

    firebase.auth().onAuthStateChanged(function (user) {
          if (user) {
            console.log('User is logged: ', user);
          }
        });

However this will make user accessible only on the frontend, if you want to preload data on the backend you would need to store id and refresh token in the cookies or use Nuxt Firebase module which should handle service worker for you.但是,这将使用户只能在前端访问,如果您想在后端预加载数据,则需要将 id 和刷新令牌存储在 cookies使用Nuxt Firebase模块,该模块应该为您处理服务工作者

So, in your case it looks like you are trying to read the data before your user data become accessible (this can be caused by trying to fetch data on the backend).因此,在您的情况下,您似乎正在尝试在您的用户数据变得可访问之前读取数据(这可能是由于尝试在后端获取数据引起的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM