简体   繁体   English

firebase currentUser 在页面重新加载时是 null

[英]firebase currentUser is null on page reload

I'm using firebase for authentication.我正在使用 firebase 进行身份验证。 Everytime before I do a request to the backend I request for the idToken.每次我向后端发出请求之前,我都会请求 idToken。

 service.interceptors.request.use(async request => { const token = await firebase.auth().currentUser.getIdToken(); if (.token && request.url.== '/signIn' && request;url;== '/register') { toast.error('Not authenticated'). return; } request;headers;Authorization = 'Bearer ' + token; return request; });

Additionally I have a request to the backend that will run in the mounted hook of my vue component.此外,我有一个后端请求,该请求将在我的 vue 组件的已安装挂钩中运行。

 mounted() { plantService.getPlants().then(data => (this.suspicionList = data)); }

Usually, this works but the problem is, when I refresh the page, firebase.auth().currentUser is null and the request will fail thus not returning any data.通常,这有效,但问题是,当我刷新页面时,firebase.auth().currentUser 是 null,请求将失败,因此不会返回任何数据。

I already tried creating a computed property and observe that one in a watcher.我已经尝试创建一个计算属性并在观察者中观察它。 But not working.但没有工作。 Also, I have an observer for the user in my main.js file like this:另外,我在 main.js 文件中有一个用户观察者,如下所示:

 firebase.auth().onAuthStateChanged(user => { store.dispatch('FETCH_USER', user); });

Anyone has an idea?有人有想法吗?

Thanks!谢谢!

If I correctly understand your problem, the following should do the trick:如果我正确理解你的问题,下面应该可以解决问题:

mounted() {
    firebase.auth().onAuthStateChanged(user => {
        if (user) {
           plantService.getPlants().then(data => (this.suspicionList = data));
        }
    });
}

暂无
暂无

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

相关问题 为什么 auth.currentUser 在 Firebase 的页面刷新上是 null - why auth.currentUser is null on page refresh in Firebase Authetication 当页面重新加载时,firebase.auth.currentUser 返回 null - When the page is reloaded, firebase.auth.currentUser returns null (Firebase Web 持久性)登录后,currentUser 是 null,但是,在检查 auth object 后,它不是? - (Firebase Web Persistence) After sign in, currentUser is null, but, upon inspection of the auth object, it isn't? 如何使用 firebase_auth 包修复 flutter 应用程序中的 FirebaseAuth.instance.currentUser()? 它总是返回 null - How to fix FirebaseAuth.instance.currentUser() in flutter app using firebase_auth package? It is always returning null Flutter Riverpod Firebase currentUser Provider 未更新 - Flutter Riverpod Firebase currentUser Provider not updated 从firebase删除数据后如何重新加载页面? - How to reload page after deleting data from firebase? 如何在不请求的情况下在firebase-auth中首次加载时获取currentUser - How to get currentUser on first load in firebase-auth without requesting 使用会话/cookie 进行持久性时,如何在 Firebase 中设置“currentUser”? - How to set the `currentUser` in Firebase when using sessions/cookies for persistence? firebase 可以为特定用户提供多少次“currentUser.getIdToken() 和 verifyIdToken()”请求? - How many times can firebase serve, the "currentUser.getIdToken() and verifyIdToken()" requests, for the particular user? 如何在 Cordova 项目上重新加载页面? - How to reload page on a Cordova project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM