Reformat some responses

This commit is contained in:
Bubka 2021-09-07 23:13:20 +02:00
parent f3adb0b5dd
commit 297b55f66f
4 changed files with 18 additions and 9 deletions

View File

@ -4,6 +4,7 @@
use Throwable;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class Handler extends ExceptionHandler
{
@ -48,10 +49,15 @@ public function report(Throwable $exception)
*/
public function render($request, Throwable $exception)
{
if ($exception instanceof ModelNotFoundException) {
return response()->json([
'message' => str_replace('App\\', '', $exception->getModel()).' not found'], 404);
}
if ($exception instanceof InvalidQrCodeException) {
return response()->json([
'message' => 'not a valid QR code'], 400);
}
return parent::render($request, $exception);
}
}

View File

@ -76,7 +76,11 @@ protected function sendLoginResponse(Request $request)
$this->authenticated($request, $this->guard()->user());
return response()->json(['message' => $success], Response::HTTP_OK);
return response()->json([
'message' => 'authenticated',
'token' => $success['token'],
'name' => $success['name']
], Response::HTTP_OK);
}
/**

View File

@ -58,13 +58,11 @@ public function register(Request $request)
//$this->guard()->login($user);
$success['token'] = $user->createToken('MyApp')->accessToken;
$success['name'] = $user->name;
return response()->json(['message' => $success]);
// return $this->registered($request, $user)
// ?: redirect($this->redirectPath());
return response()->json([
'message' => 'account created',
'token' => $user->createToken('MyApp')->accessToken,
'name' => $user->name,
]);
}
/**

View File

@ -24,8 +24,9 @@ public function upload(Request $request)
]);
$path = $request->file('icon')->store('public/icons');
$response['filename'] = pathinfo($path)['basename'];
return response()->json(pathinfo($path)['basename'], 201);
return response()->json($response, 201);
}