wolfeidau/cognito-vue-bootstrap

Cannot access homepage while authenticated

alexaandru opened this issue · 2 comments

I was unable to access the homepage while being authenticated, and it all boiled down to this: https://github.com/wolfeidau/cognito-vue-bootstrap/blob/master/src/router/index.js#L72-L76, I quote:

    if (to.meta && to.meta.auth !== undefined) {
        if (to.meta.auth) {
          // ...
        }
        // otherwise are we already authenticated?
        if (store.getters['auth/isAuthenticated']) {
            // yes we are, so off to dashboard
            router.push({ name: 'dashboard' })
            return
        }
        // ...
    }

in other words, if meta.auth was not undefined and it was also not true, then we would be redirected to Dashboard. Can be quite confusing, especially for a newbie trying to get started.

#22 should fix this.

While we're at it, on my own code, I simplified that even further, a bit into absurd :D :

router.beforeEach((to, from, next) => {
  if (!to.meta)
    return next()
  if (to.meta.title)
    document.title = to.meta.title
  if (!to.meta.auth || store.getters["auth/isAuthenticated"])
    return next()
  router.push({ name: "signIn" })
})

It is identical to the (fixed) function from the router behavior wise, but it would likely be quite confusing for beginners (and not only) which is why I did not proposed it. Just mentioned it for fun :)

I'm a big fan of early returns, I think that all those levels of indentation (and mental overhead) is what lead to this bug in the 1st place. But the return next() is definitely in the gray area (at best) :)