The error code 422 indicates that there is an issue with the data submitted in the request. In this case, it seems like there might be validation errors with the username and password fields. To retrieve and display these errors, you can modify the catch block in the userLogin function as follows:
async function userLogin() {
await csrf()
try {
await useFetch(config.BASE_URL + '/login', {
method: 'POST',
credentials: 'include',
headers: {
Accept: 'application/json',
'X-XSRF-TOKEN': Cookie.get('XSRF-TOKEN')
},
body: {
username: username.value,
password: password.value
}
})
} catch (error) {
if (error.response.status === 422) {
errors.value = Object.values(error.response.data.errors).flat()
} else {
console.log(error)
}
}
}
Here, we check if the error response status is 422, and if so, we retrieve the validation errors from the response data and assign them to the errors ref. We also added an else block to log any other errors that might occur.
Note that we are accessing the response data using error.response.data instead of error.data, since the error object returned by useFetch includes a response property that contains the response data.
With this modification, the validation errors should be displayed in the template as a list under the password field.