Skip to content

Ability to show errors on Login and Register pages #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public function login(Request $request)

if (!Auth::attempt($credentials, $remember)) {
return response([
'error' => 'The Provided credentials are not correct'
'errors' => [
'auth' => ['The Provided credentials are not correct']
]
], 422);
}
$user = Auth::user();
Expand Down
14 changes: 6 additions & 8 deletions vue/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 21 additions & 9 deletions vue/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,30 @@ const store = createStore({

register({commit}, user) {
return axiosClient.post('/register', user)
.then(({data}) => {
commit('setUser', data.user);
commit('setToken', data.token)
return data;
})
.then(res => {
return new Promise((resolve, reject) => {
if (res.isAxiosError)
reject(res.response.data.errors)

const data = res.data
commit('setUser', data.user);
commit('setToken', data.token)
resolve()
})
})
},
login({commit}, user) {
return axiosClient.post('/login', user)
.then(({data}) => {
commit('setUser', data.user);
commit('setToken', data.token)
return data;
.then(res => {
return new Promise((resolve, reject) => {
if (res.isAxiosError)
reject(res.response.data.errors)

const data = res.data
commit('setUser', data.user);
commit('setToken', data.token)
resolve()
})
})
},
logout({commit}) {
Expand Down
19 changes: 13 additions & 6 deletions vue/src/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@
</p>
</div>
<form class="mt-8 space-y-6" @submit="login">
<Alert v-if="errorMsg">
{{ errorMsg }}
<Alert v-if="errors">
<div class="text-sm">
<div v-for="(field, i) of Object.keys(errors)" :key="i">
<div v-for="(error, ind) of errors[field] || []" :key="ind">
* {{ error }}
</div>
</div>
</div>

<span
@click="errorMsg = ''"
@click="errors = ''"
class="w-8 h-8 flex items-center justify-center rounded-full transition-colors cursor-pointer hover:bg-[rgba(0,0,0,0.2)]"
>
<svg
Expand Down Expand Up @@ -144,7 +151,7 @@ const user = {
password: "",
};
let loading = ref(false);
let errorMsg = ref("");
let errors = ref("");

function login(ev) {
ev.preventDefault();
Expand All @@ -158,9 +165,9 @@ function login(ev) {
name: "Dashboard",
});
})
.catch((err) => {
.catch((error) => {
loading.value = false;
errorMsg.value = err.response.data.error;
errors.value = error;
});
}
</script>
37 changes: 28 additions & 9 deletions vue/src/views/Register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,33 @@
</p>
</div>
<form class="mt-8 space-y-6" @submit="register">
<Alert v-if="Object.keys(errors).length" class="flex-col items-stretch text-sm">
<div v-for="(field, i) of Object.keys(errors)" :key="i">
<div v-for="(error, ind) of errors[field] || []" :key="ind">
* {{ error }}
<Alert v-if="Object.keys(errors).length">
<div class="flex-col items-stretch text-sm">
<div v-for="(field, i) of Object.keys(errors)" :key="i">
<div v-for="(error, ind) of errors[field] || []" :key="ind">
* {{ error }}
</div>
</div>
</div>
<span
@click="errors = ''"
class="w-8 h-8 flex items-center justify-center rounded-full transition-colors cursor-pointer hover:bg-[rgba(0,0,0,0.2)]"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-6 w-6"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</span>
</Alert>

<input type="hidden" name="remember" value="true" />
Expand Down Expand Up @@ -53,7 +74,7 @@
required=""
v-model="user.email"
class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
:class="{ 'border-red-500': errors.email, 'z-10': errors.email }"
:class="{ 'border-red-500/50': errors.email, 'z-10': errors.email }"
placeholder="Email address"
/>
</div>
Expand All @@ -68,7 +89,7 @@
v-model="user.password"
class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
placeholder="Password"
:class="{ 'border-red-500': errors.password, 'z-10': errors.password }"
:class="{ 'border-red-500/50': errors.password, 'z-10': errors.password }"
/>
</div>
<div>
Expand Down Expand Up @@ -158,9 +179,7 @@ function register(ev) {
})
.catch((error) => {
loading.value = false;
if (error.response.status === 422) {
errors.value = error.response.data.errors;
}
errors.value = error;
});
}
</script>