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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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