Currently, I am implementing Authentication functionality using the Nuxt Auth Module.
My frontend is built on Nuxt Js, while my backend is running Laravel 5.7
In nuxt.config.js, I have configured the auth settings as follows:
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',
}
},
Within my index.vue file, I have a form with a 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">Login</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('Please enter a password'));
} else {
callback();
}
};
return {
ruleForm: {
email: '',
password: '',
},
rules: {
password: [
{ validator: validatePass, trigger: 'blur' }
],
email: [
{ required: true, message: 'Please enter an email', trigger: 'blur' },
{ type: 'email', message: 'Please enter a valid email address', 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>
Upon attempting to log in, the asynchronous function 'login' is triggered. The corresponding user based on the given username and password is returned. However, upon checking the Vuex state, `auth.loggedIn` remains false and `auth.user` is undefined.
I initially believed that Nuxt Auth would automatically update the state. Am I overlooking something?
Any assistance would be greatly appreciated! 😄