[英]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.