[Vuejs]-How to properly log out in Nuxt 3 / Vue JS?

0👍

Steps:-

  1. Import the auth module from @nuxtjs/auth

  2. Create a function called logout that uses the auth.logout() method.

  3. Bind the logout function to an event, such as a button click.

  4. When the event is triggered, the logout function will be called and the user will be logged out.

Nuxt3/Vue JS Simple Example:-

JS part

import { auth } from '@nuxtjs/auth';

export default {
  methods: {
    logout() {
      await auth.logout(); // clear the user's token from local storage and redirect the user to the login page
    },
  },
};

HTML part

<button @click="logout">Logout</button>

Leave a comment