[Vuejs]-Getting requested page route in Nuxt middleware

0👍

To help @Bodger I’m posting how I resolved this, it may not be perfect and it’s working on a slightly older version of Nuxt (I know 😵!) but this is how I resolved the issue.

.../middleware/authenticated.js

export default function (context) {
  const path =
    context.route.path.length && context.route.path[0] === '/'
      ? context.route.path.slice(1)
      : context.route.path
  const pathArray = path.split('/')
  if (process.server && !context.store.state.authenticated) {
    return context.redirect('/access', pathArray)
  } else if (!context.store.state.authenticated) {
    return context.redirect('/access', pathArray)
  }
}

The pathArray is then accessible in my /access page.

.../pages/access.js

data() {
    return {
      attemptedRoutePathArray: Object.values(this.$route.query)
      ...
    }
  },
...
computed: {
    attemptedRouteURL() {
      return new URL(
        this.attemptedRoutePathArray.join('/'),
        process.env.baseUrl
      )
    },
    ...
}

Leave a comment