Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
How to use middlewares one more time in Nuxt.js universal mode?

I have middleware that I need to check if user authenticated every time he visits page. For now it seems like this, just to make sure if it's able to work with localStorage:

export default function({ store, redirect, route }) {
  console.log('Here is auth middleware')
  console.log(localStorage.getItem('token'))
}
// nuxt.config.js
  plugins: [
    '~/plugins/test.client.js'
  ],

Generally, this middleware has to work with localStorage. Here I found solution of my problem, but it works only one time (from documentation):

In universal mode, middlewares will be called server-side once (on the first request to the Nuxt app or when page refreshes) and client-side when navigating to further routes

So, I need to create such middleware, that is going to work only on specific pages and be able to work with localStorage with Nuxt.js app in universal mode. Is it possible to implement?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This problem can be solved using mixins. In mixins/auth.js I have this code:

import { verifyToken } from "@/api";
export default {
  async mounted() {
    if (!localStorage.getItem('token')) {
      await this.$router.push({path: '/login'})
    } else {
      const checkToken = await verifyToken({ token: localStorage.getItem('token') })
      if (checkToken.error) {
        localStorage.removeItem('token')
        await this.$router.push({path: '/login'})
      }
    }
  }
}

And then, on page where I need to check is there is valid token in localStorage I just do this:

<script>
import auth from '~/mixins/auth'
export default {
  name: "Component",
  mixins: [auth],
</script>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda