wolfeidau / cognito-vue-bootstrap

This application illustrates how to use the Amazon Amplify with vue.js.
https://github.com/wolfeidau/cognito-vue-bootstrap
Other
181 stars 36 forks source link

Cannot access homepage while authenticated #21

Open alexaandru opened 5 years ago

alexaandru commented 5 years ago

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.

alexaandru commented 5 years ago

22 should fix this.

alexaandru commented 5 years ago

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) :)