Better errors handling for user controller

This commit is contained in:
Bubka 2020-01-14 11:49:35 +01:00
parent d46e3d5ce6
commit 9632641702
3 changed files with 27 additions and 22 deletions

View File

@ -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]);
}

View File

@ -9,14 +9,14 @@
<div class="control">
<input id="email" type="email" class="input" v-model="email" required autofocus />
</div>
<p class="help is-danger" v-if="errors.email">{{ errors.email.toString() }}</p>
<p class="help is-danger" v-if="validationErrors.email">{{ validationErrors.email.toString() }}</p>
</div>
<div class="field">
<label class="label">{{ $t('auth.forms.password') }}</label>
<div class="control">
<input id="password" type="password" class="input" v-model="password" required />
</div>
<p class="help is-danger" v-if="errors.password">{{ errors.password.toString() }}</p>
<p class="help is-danger" v-if="validationErrors.password">{{ validationErrors.password.toString() }}</p>
</div>
<div class="field">
<div class="control">
@ -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 } });

View File

@ -9,28 +9,28 @@
<div class="control">
<input id="name" type="text" class="input" v-model="name" required autofocus />
</div>
<p class="help is-danger" v-if="errors.name">{{ errors.name.toString() }}</p>
<p class="help is-danger" v-if="validationErrors.name">{{ validationErrors.name.toString() }}</p>
</div>
<div class="field">
<label class="label">{{ $t('auth.forms.email') }}</label>
<div class="control">
<input id="email" type="email" class="input" v-model="email" required />
</div>
<p class="help is-danger" v-if="errors.email">{{ errors.email.toString() }}</p>
<p class="help is-danger" v-if="validationErrors.email">{{ validationErrors.email.toString() }}</p>
</div>
<div class="field">
<label class="label">{{ $t('auth.forms.password') }}</label>
<div class="control">
<input id="password" type="password" class="input" v-model="password" required />
</div>
<p class="help is-danger" v-if="errors.password">{{ errors.password.toString() }}</p>
<p class="help is-danger" v-if="validationErrors.password">{{ validationErrors.password.toString() }}</p>
</div>
<div class="field">
<label class="label">{{ $t('auth.forms.confirm_password') }}</label>
<div class="control">
<input id="password_confirmation" type="password" class="input" v-model="password_confirmation" required />
</div>
<p class="help is-danger" v-if="errors.passwordConfirmation">{{ errors.passwordConfirmation.toString() }}</p>
<p class="help is-danger" v-if="validationErrors.passwordConfirmation">{{ validationErrors.passwordConfirmation.toString() }}</p>
</div>
<div class="field">
<div class="control">
@ -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 } });
}
});
}
},