Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3a641df

Browse files
consistent response in all APIs
1 parent a97d307 commit 3a641df

File tree

7 files changed

+153
-7
lines changed

7 files changed

+153
-7
lines changed

‎app/ApiCode.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App;
4+
5+
class ApiCode {
6+
public const SOMETHING_WENT_WRONG = 250;
7+
public const INVALID_CREDENTIALS = 251;
8+
}

‎app/Http/Controllers/AuthController.php‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace App\Http\Controllers;
44

5-
use Illuminate\Http\Request;
5+
use App\ApiCode;
6+
67

78
class AuthController extends Controller
89
{
@@ -15,24 +16,24 @@ public function login() {
1516
$credentials = request(['email', 'password']);
1617

1718
if (! $token = auth()->attempt($credentials)) {
18-
return response()->json(['error' => 'Invalid email or password'], 401);
19+
return $this->respondUnAuthorizedRequest(ApiCode::INVALID_CREDENTIALS);
1920
}
2021

2122
return $this->respondWithToken($token);
2223
}
2324

2425
private function respondWithToken($token) {
25-
return response()->json([
26+
return $this->respond([
2627
'token' => $token,
2728
'access_type' => 'bearer',
2829
'expires_in' => auth()->factory()->getTTL() * 60
29-
]);
30+
], "Login Successful");
3031
}
3132

3233

3334
public function logout() {
3435
auth()->logout();
35-
return response()->json(['msg' => 'User successfully logged out']);
36+
return $this->respondWithMessage('User successfully logged out');
3637
}
3738

3839

@@ -41,6 +42,6 @@ public function refresh() {
4142
}
4243

4344
public function me() {
44-
return response()->json(auth()->user());
45+
return $this->respond(auth()->user());
4546
}
4647
}

‎app/Http/Controllers/Controller.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,32 @@
66
use Illuminate\Foundation\Bus\DispatchesJobs;
77
use Illuminate\Foundation\Validation\ValidatesRequests;
88
use Illuminate\Routing\Controller as BaseController;
9+
use MarcinOrlowski\ResponseBuilder\ResponseBuilder;
910

1011
class Controller extends BaseController
1112
{
1213
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
14+
15+
16+
public function respond($data, $msg = null) {
17+
return ResponseBuilder::asSuccess()->withData($data)->withMessage($msg)->build();
18+
}
19+
20+
public function respondWithMessage($msg) {
21+
return ResponseBuilder::asSuccess()->withMessage($msg)->build();
22+
}
23+
24+
public function respondWithError($api_code, $http_code) {
25+
return ResponseBuilder::asError($api_code)->withHttpCode($http_code)->build();
26+
}
27+
28+
public function respondBadRequest($api_code) {
29+
return $this->respondWithError($api_code, 400);
30+
}
31+
public function respondUnAuthorizedRequest($api_code) {
32+
return $this->respondWithError($api_code, 401);
33+
}
34+
public function respondNotFound($api_code) {
35+
return $this->respondWithError($api_code, 404);
36+
}
1337
}

‎composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"guzzlehttp/guzzle": "^6.3",
1515
"laravel/framework": "^7.0",
1616
"laravel/tinker": "^2.0",
17+
"marcin-orlowski/laravel-api-response-builder": "^7.1",
1718
"tymon/jwt-auth": "^1.0"
1819
},
1920
"require-dev": {

‎composer.lock‎

Lines changed: 69 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎config/response_builder.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
4+
use App\ApiCode;
5+
6+
return [
7+
/*
8+
|-----------------------------------------------------------------------------------------------------------
9+
| Code range settings
10+
|-----------------------------------------------------------------------------------------------------------
11+
*/
12+
'min_code' => 100,
13+
'max_code' => 1024,
14+
15+
/*
16+
|-----------------------------------------------------------------------------------------------------------
17+
| Error code to message mapping
18+
|-----------------------------------------------------------------------------------------------------------
19+
|
20+
*/
21+
'map' => [
22+
ApiCode::SOMETHING_WENT_WRONG => 'api.something_went_wrong',
23+
ApiCode::INVALID_CREDENTIALS => 'api.invalid_credentials',
24+
],
25+
];

‎resources/lang/en/api.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Authentication Language Lines
8+
|--------------------------------------------------------------------------
9+
|
10+
| The following language lines are used during authentication for various
11+
| messages that we need to display to the user. You are free to modify
12+
| these language lines according to your application's requirements.
13+
|
14+
*/
15+
16+
'something_went_wrong' => 'Ops! Something went wrong.',
17+
'invalid_credentials' => 'Invalid email or password.'
18+
19+
];

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /