Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 428
Mohandes Avatar asked Feb 08 '19 13:02

Mohandes


2 Answers

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('/')
    }
  }
}
like image 138
Borjante Avatar answered Oct 24 '22 13:10

Borjante


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: '/'
    },
}
like image 31
zarpio Avatar answered Oct 24 '22 14:10

zarpio