Answer the question
In order to leave comments, you need to log in
How to force beforeRouter to wait until firebase confirms authorization?
I am making a simple application based on a series of lessons from https://youtu.be/DlXSA3_lSX4 .
I got to the point where in the routing we check whether the user is logged in and send to the appropriate pages:
import Vue from 'vue'
import VueRouter from 'vue-router'
// Routes
import Home from '@/views/Home'
import TasksPage from '@/views/TasksPage'
import Login from '@/views/Auth/Login'
import Registration from '@/views/Auth/Registration'
import store from '../store'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: Home,
beforeEnter(to, from, next) {
store.getters.checkUser ? next() : next('/login')
}
},
{
path: '/tasks',
name: 'tasks',
component: TasksPage,
beforeEnter(to, from, next) {
store.getters.checkUser ? next() : next('/login')
}
},
{
path: '/login',
name: 'login',
component: Login,
beforeEnter(to, from, next) {
!store.getters.checkUser ? next() : next('/')
}
},
{
path: '/registration',
name: 'registration',
component: Registration,
beforeEnter(to, from, next) {
!store.getters.checkUser ? next() : next('/')
}
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
import firebase from 'firebase/app'
import User from './user_help'
export default {
state: {
user: null
},
mutations: {
setUser(state, payload) {
state.user = payload
}
},
actions: {
async registerUser({commit}, {email, password}) {
commit('clearError')
commit('setLoading', true)
try {
const user = await firebase.auth().createUserWithEmailAndPassword(email, password)
commit('setUser', new User(user.user.uid))
commit('setLoading', false)
} catch (err) {
commit('setLoading', false)
commit('setError', err.message)
throw err
}
},
async loginUser({commit}, {email, password}) {
commit('clearError')
commit('setLoading', true)
try {
const user = await firebase.auth().signInWithEmailAndPassword(email, password)
commit('setUser', new User(user.user.uid))
commit('setLoading', false)
} catch (err) {
commit('setLoading', false)
commit('setError', err.message)
throw err
}
},
loggedUser({commit}, payload) {
commit('setUser', new User(payload.uid))
},
logoutUser({commit}) {
firebase.auth().signOut()
commit('setUser', null)
}
},
getters: {
user(state) {
return state.user
},
checkUser(state) {
return state.user !== null
}
}
}
export default class User {
constructor(id) {
this.id = id
}
}
new Vue({
router,
store,
render: h => h(App),
created() {
const firebaseConfig = {
apiKey: "...",
authDomain: "...",
databaseURL: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appId: "..."
}
firebase.initializeApp(firebaseConfig)
firebase.auth().onAuthStateChanged( user => {
if (user) {
this.$store.dispatch('loggedUser', user)
}
})
}
}).$mount('#app')
...
computed: {
checkUser() {
return this.$store.getters.checkUser
},
linkMenu() {
if (this.checkUser) {
return [
{ name: 'Home', url: '/' },
{ name: 'Tasks', url: '/tasks' }
]
}
return [
{ name: 'Login', url: '/login' },
{ name: 'Registration', url: '/registration' }
]
}
}
...
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question