Route guard with stored location



  • Hi!

    I’m trying to implement route guard that will store desired location for user that not yet logged in and redirect to it after login.

    let storedURL = null
      router.beforeEach((to, from, next) => {
        const record = to.matched.find(record => record.meta.auth)
        if (record) {
          if (store.getters['auth/check']) {
            if (storedURL) {
              const redirectURL = storedURL
              storedURL = null
              next(redirectURL)
            } else {
              next()
            }
          } else {
            storedURL = to.path
            next('/')
          }
        } else {
          if (to.path === '/' && store.getters['auth/check']) {
            next('/dashboard')
          } else {
            next()
          }
        }
      })
    

    It works, but on store location I have warning in my console:

    Error: Script terminated by timeout at:
    notify@webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:730:36
    reactiveSetter@webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:1056:11
    proxySetter@webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:4625:5
    __onControlFocusin@webpack-internal:///./node_modules/quasar/src/components/field/QField.js:345:9
    invokeWithErrorHandling@webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:1853:26
    invoker@webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:2178:14
    add$1/original._wrapper@webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:6907:25
    

    And on redirect I have an error:

    uncaught exception: undefined
    

    Can’t understand what is wrong


Log in to reply