L
L
Liyebre2021-06-27 20:31:38
Vue.js
Liyebre, 2021-06-27 20:31:38

Why is NuxtAuth not logging in?

Hello.

Question for those who understand auth.nuxtjs.org

In general:
I am writing a site that should be available only to authorized users.
As an api I use a fake one - https://github.com/techiediaries/fake-api-jwt-json...
I'm trying to understand why, after entering the correct data, the page simply refreshes and does not redirect to the main one. And if you go to the main page manually, it redirects back to the login form ..

After clicking on the "login" button in the fake api terminal, I see that everything is OK and the token is displayed:

login endpoint called; request body:
{ email: '[email protected]', password: 'demo' }
Access Token:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImRlbW9AZGVtby5ydSIsInBhc3N3b3JkIjoiZGVtbyIsImlhdCI6MTYyNDgxNDc3NiwiZXhwIjoxNjI0ODE4Mzc2fQ.uQEVQD1X6BVuGCH2JUtHOOjeYBHBxBtq2uo9OuHSu6Y
POST /auth/login 200 0.709 ms - 207

If I enter incorrect data, I get an error - everything is as it should be.

Please help me find the error and understand what I did wrong

Here are NuxtAuth settings from nuxt.config.js

axios: {
    baseURL: 'http://localhost:8000/',
    credentials: true
  },

  auth: {
    strategies: {
      local: {
        endpoints: {
          login: { 
            url: '/auth/login', 
            method: 'post', 
            propertyName: 'data.token' },
          user: { 
            url: '/', 
            method: 'get', 
            propertyName: 'data' },
          logout: false
        }
      }
    }
  },
  router: {
    middleware: ['auth']
  },



store/index.js

export const getters = {
    isAuthenticated(state) {
      return state.auth.loggedIn
    },
  
    loggedInUser(state) {
      return state.auth.user
    }
}



login page script

export default {
  layout: 'auth',
  components: {
    Notification,
  },
  data() {
    return {
      email: '',
      password: '',
      error: null
    }
  },
  methods: {
    async login() {
      try {
        await this.$auth.loginWith('local', {
          data: {
          email: this.email,
          password: this.password
          }
        })
        this.$router.push('/')
      } catch (e) {
        this.error = e.response.data.message
      }
    }
  },
  auth: 'guest'
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Liyebre, 2021-06-28
@Liyebre

I solved the problem by adding settings to nuxt.config.js:

token: {
          property: 'access_token',
          global: true,
        },
        user: {
          property: 'user',
        },

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question