From 9632641702b33774c225eeb2ba626d0fa427ae28 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Tue, 14 Jan 2020 11:49:35 +0100 Subject: [PATCH] Better errors handling for user controller --- app/Http/Controllers/UserController.php | 10 +++++----- resources/js/views/Login.vue | 16 ++++++++-------- resources/js/views/Register.vue | 23 ++++++++++++++--------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 2274c7b5..fe0f4fb0 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -36,10 +36,10 @@ public function login(Request $request) $success['token'] = Auth::user()->createToken('MyApp')->accessToken; $success['name'] = Auth::user()->name; - return response()->json(['success' => $success], 200); + return response()->json(['message' => $success], 200); } - return response()->json(['error' => 'Unauthorised'], 401); + return response()->json(['message' => 'unauthorised'], 401); } @@ -53,7 +53,7 @@ public function logout() $accessToken = Auth::user()->token(); $accessToken->revoke(); - return response()->json(['success' => 'signed out']); + return response()->json(['message' => 'signed out']); } @@ -83,7 +83,7 @@ public function register(Request $request) $count = DB::table('users')->count(); if( $count > 0 ) { - return response()->json(['error' => __('already_one_user_registered')], 400); + return response()->json(['message' => __('errors.already_one_user_registered')], 400); } $validator = Validator::make($request->all(), [ @@ -103,7 +103,7 @@ public function register(Request $request) $success['token'] = $user->createToken('MyApp')->accessToken; $success['name'] = $user->name; - return response()->json(['success' => $success]); + return response()->json(['message' => $success]); } diff --git a/resources/js/views/Login.vue b/resources/js/views/Login.vue index 9841bafc..3cf991ed 100644 --- a/resources/js/views/Login.vue +++ b/resources/js/views/Login.vue @@ -9,14 +9,14 @@
-

{{ errors.email.toString() }}

+

{{ validationErrors.email.toString() }}

-

{{ errors.password.toString() }}

+

{{ validationErrors.password.toString() }}

@@ -42,7 +42,7 @@ return { email : '', password : '', - errors: {} + validationErrors: {} } }, methods : { @@ -54,20 +54,20 @@ password: this.password }) .then(response => { - localStorage.setItem('user',response.data.success.name) - localStorage.setItem('jwt',response.data.success.token) + localStorage.setItem('user',response.data.message.name) + localStorage.setItem('jwt',response.data.message.token) if (localStorage.getItem('jwt') != null){ this.$router.go('/'); } }) .catch(error => { - console.log(error.response); if( error.response.status === 401 ) { - this.errors['password'] = [ this.$t('auth.forms.password_do_not_match') ] + this.validationErrors['email'] = '' + this.validationErrors['password'] = [ this.$t('auth.forms.password_do_not_match') ] } else if( error.response.data.validation ) { - this.errors = error.response.data.validation + this.validationErrors = error.response.data.validation } else { this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); diff --git a/resources/js/views/Register.vue b/resources/js/views/Register.vue index 6406b1dc..2bc1f573 100644 --- a/resources/js/views/Register.vue +++ b/resources/js/views/Register.vue @@ -9,28 +9,28 @@
-

{{ errors.name.toString() }}

+

{{ validationErrors.name.toString() }}

-

{{ errors.email.toString() }}

+

{{ validationErrors.email.toString() }}

-

{{ errors.password.toString() }}

+

{{ validationErrors.password.toString() }}

-

{{ errors.passwordConfirmation.toString() }}

+

{{ validationErrors.passwordConfirmation.toString() }}

@@ -62,7 +62,7 @@ email : '', password : '', password_confirmation : '', - errors: {}, + validationErrors: {}, errorMessage: '' } }, @@ -73,7 +73,7 @@ .then(response => { if( response.data.userCount > 0) { this.errorMessage = this.$t('errors.already_one_user_registered') + ' ' + this.$t('errors.cannot_register_more_user') - this.$router.push({ name: 'flooded' }); + //this.$router.push({ name: 'flooded' }); } }) .catch(error => { @@ -92,15 +92,20 @@ password_confirmation : this.password_confirmation }) .then(response => { - localStorage.setItem('user',response.data.success.name) - localStorage.setItem('jwt',response.data.success.token) + localStorage.setItem('user',response.data.message.name) + localStorage.setItem('jwt',response.data.message.token) if (localStorage.getItem('jwt') != null){ this.$router.go('/'); } }) .catch(error => { - this.errors = error.response.data.validation + if( error.response.data.validation ) { + this.validationErrors = error.response.data.validation + } + else { + this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); + } }); } },