From 9dd54eeba5e658c5b1c8a48092bec7c2c2453ef8 Mon Sep 17 00:00:00 2001 From: Satya Kresna Date: Sun, 19 Jan 2020 21:16:14 +0800 Subject: [PATCH 1/4] feat: add validation in login Passport API --- .../Controllers/Api/Auth/AuthController.php | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/Auth/AuthController.php b/app/Http/Controllers/Api/Auth/AuthController.php index fc45805..dc51771 100644 --- a/app/Http/Controllers/Api/Auth/AuthController.php +++ b/app/Http/Controllers/Api/Auth/AuthController.php @@ -5,11 +5,28 @@ use Route; use Illuminate\Http\Request; use App\Http\Controllers\Controller; +use Illuminate\Support\Facades\Validator; class AuthController extends Controller { + /** + * Get a validator for an incoming registration request. + * + * @param array $data + * @return \Illuminate\Contracts\Validation\Validator + */ + protected function validator(array $data) + { + return Validator::make($data, [ + 'email' => 'required|string|email', + 'password' => 'required|string|min:6', + ]); + } + public function login(Request $request) { + $this->validator($request->all())->validate(); + $form = [ 'grant_type' => 'password', 'client_id' => config('services.passport.client_id'), @@ -32,6 +49,8 @@ public function logout() $token->revoke(); }); - return response()->json('Logged out successfully', 200); + return response()->json(array( + "message" => 'Logged out successfully' + ), 200); } } From a92399b8eebf2bef90bbf3f32171801123982a9e Mon Sep 17 00:00:00 2001 From: Satya Kresna Date: Sun, 19 Jan 2020 21:17:22 +0800 Subject: [PATCH 2/4] style: make route logout consistent with route login --- routes/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/api.php b/routes/api.php index 7208ee4..7f4616b 100644 --- a/routes/api.php +++ b/routes/api.php @@ -20,7 +20,7 @@ // I think it's better without conflict the main project. Route::post('auth/login', 'Api\Auth\AuthController@login')->name('api.auth.login'); -Route::middleware('auth:api')->post('logout', 'Api\Auth\AuthController@logout')->name('api.auth.logout'); +Route::middleware('auth:api')->post('auth/logout', 'Api\Auth\AuthController@logout')->name('api.auth.logout'); Route::group(['middleware' => 'auth:api', 'as' => 'api.', 'namespace' => 'Api'], function () { /* From 527bf6366ee8fc446e73a6b993f36a55d2dc5d78 Mon Sep 17 00:00:00 2001 From: Satya Kresna Date: Sun, 19 Jan 2020 21:51:40 +0800 Subject: [PATCH 3/4] refactor: simplify login validation API --- .../Controllers/Api/Auth/AuthController.php | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/Api/Auth/AuthController.php b/app/Http/Controllers/Api/Auth/AuthController.php index dc51771..adb928e 100644 --- a/app/Http/Controllers/Api/Auth/AuthController.php +++ b/app/Http/Controllers/Api/Auth/AuthController.php @@ -5,27 +5,16 @@ use Route; use Illuminate\Http\Request; use App\Http\Controllers\Controller; -use Illuminate\Support\Facades\Validator; class AuthController extends Controller { - /** - * Get a validator for an incoming registration request. - * - * @param array $data - * @return \Illuminate\Contracts\Validation\Validator - */ - protected function validator(array $data) + + public function login(Request $request) { - return Validator::make($data, [ + $request->validate([ 'email' => 'required|string|email', 'password' => 'required|string|min:6', ]); - } - - public function login(Request $request) - { - $this->validator($request->all())->validate(); $form = [ 'grant_type' => 'password', From be4048826db18e6ebd0680bc6e68ef2ae692c517 Mon Sep 17 00:00:00 2001 From: Satya Kresna Date: Sun, 19 Jan 2020 23:09:45 +0800 Subject: [PATCH 4/4] style: update array format in logout response --- app/Http/Controllers/Api/Auth/AuthController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Api/Auth/AuthController.php b/app/Http/Controllers/Api/Auth/AuthController.php index adb928e..86a4155 100644 --- a/app/Http/Controllers/Api/Auth/AuthController.php +++ b/app/Http/Controllers/Api/Auth/AuthController.php @@ -38,8 +38,8 @@ public function logout() $token->revoke(); }); - return response()->json(array( - "message" => 'Logged out successfully' - ), 200); + return response()->json([ + 'message' => 'Logged out successfully', + ], 200); } }