Reputation: 393
I am currently workin on Authentication functionality with the help of the Nuxt Auth Module.
On the frontend i am running Nuxt Js, and on the backend i am running Laravel 5.7
in nuxt.config.js i have set the auth settings:
auth: {
strategies: {
local: {
endpoints: {
login: { url: 'login', method: 'post', propertyName: 'access_token' },
logout: { url: 'logout', method: 'post' },
user: { url: 'user', method: 'get', propertyName: 'user' },
}
},
tokenRequired: true,
tokenType: 'bearer',
}
},
in my index.vue i have a form with the login method:
<template>
<div>
<div>
<b-container>
<b-row no-gutters>
<b-col col lg="12">
</b-col>
</b-row>
<b-row no-gutters>
<b-col col lg="12">
<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm" label-position="top">
<el-form-item label="Email" prop="email">
<el-input v-model="ruleForm.email" ></el-input>
</el-form-item>
<el-form-item label="Password" prop="password">
<el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="login">Inloggen</el-button>
<!--<el-button @click="resetForm('ruleForm2')">Reset</el-button>-->
</el-form-item>
</el-form>
</b-col>
</b-row>
</b-container>
</div>
</div>
</template>
<script>
export default {
layout: 'login',
data() {
var validatePass = (rule, value, callback) => {
if (value === '') {
callback(new Error('Vul een wachtwoord in'));
} else {
callback();
}
};
return {
ruleForm: {
email: '',
password: '',
},
rules: {
password: [
{ validator: validatePass, trigger: 'blur' }
],
email: [
{ required: true, message: 'Vul een email in', trigger: 'blur' },
{ type: 'email', message: 'Vul een correct email adres in', trigger: ['blur'] }
]
}
};
},
methods: {
async login() {
try {
await this.$auth.loginWith('local', {
data: {
username: this.ruleForm.email,
password: this.ruleForm.password
}
}).then(() => {
this.$router.push({ name: 'dashboard'})
})
} catch (e) {
console.log(e)
}
},
}
}
</script>
When i try to login, the async function 'login' is being called. The user that corresponds with the username and password gets returned. The only problem i have, is that the when i look in the vuex state, auth.loggedIn stays false and the auth.user stays undefined.
I thought Nuxt Auth updates the state automatically, or am i missing something?
Any help would be greatly appreciated :D
Upvotes: 2
Views: 4766
Reputation: 393
I finally found the solution. I had to set the propertyname of the user endpoint to false
Old version
endpoints: {
login: { url: 'login', method: 'post', propertyName: 'access_token' },
logout: { url: 'logout', method: 'post' },
user: { url: 'user', method: 'get', propertyName: 'user' },
}
New version
endpoints: {
login: { url: 'login', method: 'post', propertyName: 'access_token' },
logout: { url: 'logout', method: 'post' },
user: { url: 'user', method: 'get', propertyName: false },
}
Now the user gets loaded in the state
Upvotes: 2