I'm using firebase for authentication. Everytime before I do a request to the backend I request for the 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.
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.
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:
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));
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.