Mohandes
Mohandes

Reputation: 291

How to redirect to home page after logout in nuxt?

after login in site i want when i click to specific button , user log out from site to home page.

here is my template code:

<template>
  <nav id="header" class="navbar navbar-expand header-setting">
    <div class="main-content">
          <div class="notification" @click="LogOut()"></div>
      </div>
    </div>
  </nav>
</template>

and here is my script code:

export default {
  name: 'HeadeAfterLogin',
  methods: {
    LogOut() {
      localStorage.removeItem('token')
    }
  }
}

any one can help me to complete LogOut function ?

Upvotes: 8

Views: 36383

Answers (2)

zarpio
zarpio

Reputation: 7348

What I am doing is:

Component:

### html 
<a href="#" class="dropdown-item" @click.prevent="signOut">LogOut</a>

### script
export default {
    methods: {
      signOut() {
        this.$auth.logout();
      }
    }
}

nuxt.config.js

auth: {
    strategies: {
        ...
    },
    redirect: {
        login: '/login',
        logout: '/login', # after logout, user will be redirected here.
        home: '/'
    },
}

Upvotes: 1

Borjante
Borjante

Reputation: 10517

Inside your component you have access to this.$router

So you can easily do:

export default {
  name: 'HeadeAfterLogin',
  methods: {
    LogOut() {
      localStorage.removeItem('token')
      this.$router.push('/')
    }
  }
}

Upvotes: 27

Related Questions