Use and handle default Laravel validation errors response

This commit is contained in:
Bubka 2020-01-19 23:02:20 +01:00
parent 8940efc225
commit 2be4e3e4e1
10 changed files with 29 additions and 57 deletions

View File

@ -2,7 +2,6 @@
namespace App\Http\Controllers;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
@ -19,14 +18,11 @@ class IconController extends Controller
*/
public function upload(Request $request)
{
$validator = Validator::make($request->all(), [
$this->validate($request, [
'icon' => 'required|image',
]);
if ($validator->fails()) {
return response()->json(['validation' => $validator->errors()], 400);
}
$path = $request->file('icon')->storePublicly('public/icons');
return response()->json(pathinfo($path)['basename'], 201);

View File

@ -2,7 +2,6 @@
namespace App\Http\Controllers;
use Validator;
use Zxing\QrReader;
use OTPHP\TOTP;
use OTPHP\Factory;
@ -23,14 +22,10 @@ public function decode(Request $request)
{
// input validation
$validator = Validator::make($request->all(), [
$this->validate($request, [
'qrcode' => 'required|image',
]);
if ($validator->fails()) {
return response()->json(['validation' => $validator->errors()], 400);
}
// qrcode analysis
$path = $request->file('qrcode')->store('qrcodes');
$qrcode = new QrReader(storage_path('app/' . $path));
@ -64,11 +59,11 @@ public function decode(Request $request)
}
catch (AssertionFailedException $exception) {
return response()->json([
'validation' => [
'qrcode' => __('errors.response.no_valid_totp')
]
], 400);
$error = \Illuminate\Validation\ValidationException::withMessages([
'qrcode' => __('errors.response.no_valid_totp')
]);
throw $error;
}
}

View File

@ -2,7 +2,6 @@
namespace App\Http\Controllers;
use Validator;
use App\TwoFAccount;
use App\Classes\TimedTOTP;
use Illuminate\Http\Request;
@ -33,16 +32,11 @@ public function store(Request $request)
// see https://github.com/google/google-authenticator/wiki/Key-Uri-Format
// for otpauth uri format validation
$validator = Validator::make($request->all(), [
$this->validate($request, [
'service' => 'required',
'uri' => 'required|starts_with:otpauth://totp/',
]);
if ($validator->fails()) {
return response()->json(['validation' => $validator->errors()], 400);
}
$twofaccount = TwoFAccount::create([
'service' => $request->service,
'account' => $request->account,
@ -95,15 +89,10 @@ public function generateTOTP(TwoFAccount $twofaccount)
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), [
$this->validate($request, [
'service' => 'required',
]);
if ($validator->fails()) {
return response()->json( ['validation' => $validator->errors() ], 400);
}
try {
$twofaccount = TwoFAccount::FindOrFail($id);

View File

@ -3,7 +3,6 @@
namespace App\Http\Controllers;
use App\User;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
@ -18,15 +17,12 @@ class UserController extends Controller
*/
public function login(Request $request)
{
$validator = Validator::make($request->all(), [
$this->validate($request, [
'email' => 'required|exists:users,email',
'password' => 'required',
]);
if ($validator->fails()) {
return response()->json(['validation' => $validator->errors()], 400);
}
$credentials = [
'email' => request('email'),
'password' => request('password')
@ -86,16 +82,12 @@ public function register(Request $request)
return response()->json(['message' => __('errors.already_one_user_registered')], 400);
}
$validator = Validator::make($request->all(), [
$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed',
]);
if ($validator->fails()) {
return response()->json(['validation' => $validator->errors()], 400);
}
$input = $request->all();
$input['password'] = bcrypt($input['password']);

View File

@ -83,8 +83,8 @@
if( error.response.status === 401 ) {
this.errorMessage = this.$t('auth.forms.password_do_not_match')
}
else if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
else if( error.response.status == 422 ) {
this.validationErrors = error.response.data.errors
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response } });

View File

@ -104,8 +104,8 @@
this.validationErrors = {}
if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
if( error.response.status == 422 ) {
this.validationErrors = error.response.data.errors
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response } });

View File

@ -61,7 +61,7 @@
})
.catch(error => {
if( error.response.data.errors ) {
if( error.response.status == 422 ) {
this.validationErrors = error.response.data.errors
}
else if( error.response.data.requestFailed ) {

View File

@ -80,7 +80,7 @@
})
.catch(error => {
if( error.response.data.errors ) {
if( error.response.status == 422 ) {
this.validationErrors = error.response.data.errors
}
else if( error.response.data.resetFailed ) {

View File

@ -115,8 +115,8 @@
this.$router.push({name: 'accounts', params: { InitialEditMode: false }});
})
.catch(error => {
if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
if( error.response.status == 422 ) {
this.validationErrors = error.response.data.errors
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
@ -152,8 +152,8 @@
this.validationErrors['qrcode'] = '';
})
.catch(error => {
if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
if( error.response.status == 422 ) {
this.validationErrors = error.response.data.errors
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
@ -185,8 +185,8 @@
this.validationErrors['icon'] = '';
})
.catch(error => {
if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
if( error.response.status == 422 ) {
this.validationErrors = error.response.data.errors
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });

View File

@ -106,8 +106,8 @@
this.$router.push({name: 'accounts', params: { InitialEditMode: true }});
})
.catch(error => {
if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
if( error.response.status == 422 ) {
this.validationErrors = error.response.data.errors
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response } });
@ -144,8 +144,8 @@
this.validationErrors['icon'] = '';
})
.catch(error => {
if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
if( error.response.status == 422 ) {
this.validationErrors = error.response.data.errors
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response } });