Skip to content

Commit

Permalink
Refactor admin auth
Browse files Browse the repository at this point in the history
  • Loading branch information
arifszn committed Sep 19, 2022
1 parent 9548c69 commit cda4c85
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 245 deletions.
105 changes: 105 additions & 0 deletions app/Http/Controllers/Admin/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\AdminLoginRequest;
use App\Http\Resources\LoggedInAdminResource;
use App\Services\AuthService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response as HttpResponse;
use Illuminate\Support\Facades\Response;
use OpenApi\Attributes as OAT;

class AuthController extends Controller
{
/**
* Create a new controller instance.
*
* @param AuthService $authService
* @return void
*/
public function __construct(private AuthService $authService)
{
//
}

/**
* Login an admin.
*
* @param AdminLoginRequest $request
* @return JsonResponse
*
* @throws HttpException
* @throws NotFoundHttpException
*/
#[OAT\Post(
tags: ['adminAuth'],
path: '/api/admin/login',
summary: 'Login an admin',
operationId: 'Admin.AuthController.login',
requestBody: new OAT\RequestBody(
required: true,
content: new OAT\JsonContent(ref: '#/components/schemas/AdminLoginRequest')

),
responses: [
new OAT\Response(
response: HttpResponse::HTTP_OK,
description: 'Ok',
content: new OAT\JsonContent(ref: '#/components/schemas/LoggedInAdminResource')
),
new OAT\Response(
response: HttpResponse::HTTP_UNPROCESSABLE_ENTITY,
description: 'Unprocessable entity',
content: new OAT\JsonContent(ref: '#/components/schemas/ValidationError')
),
new OAT\Response(
response: HttpResponse::HTTP_UNAUTHORIZED,
description: 'Unauthorized',
content: new OAT\JsonContent(
properties: [
new OAT\Property(
property: 'message',
type: 'string',
example: 'Invalid credentials.'
),
]
)
),
]
)]
public function login(AdminLoginRequest $request): JsonResponse
{
$admin = $this->authService->loginAdmin($request);

return Response::json(new LoggedInAdminResource($admin));
}

/**
* Logout an admin.
*
* @param Request $request
* @return JsonResponse
*/
#[OAT\Post(
tags: ['adminAuth'],
path: '/api/admin/logout',
summary: 'Logout an admin',
operationId: 'Admin.AuthController.logout',
security: [['BearerToken' => []]],
responses: [
new OAT\Response(
response: HttpResponse::HTTP_NO_CONTENT,
description: 'No content'
),
]
)]
public function logout(Request $request): JsonResponse
{
$this->authService->logoutAdmin($request->user());

return Response::json(null, HttpResponse::HTTP_NO_CONTENT);
}
}
6 changes: 3 additions & 3 deletions app/Http/Controllers/Admin/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use App\Http\Controllers\Controller;
use App\Http\Resources\AdminResource;
use App\Services\AdminService;
use OpenApi\Attributes as OAT;
use Illuminate\Http\Response as HttpResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Response;
use Illuminate\Http\Request;
use Illuminate\Http\Response as HttpResponse;
use Illuminate\Support\Facades\Response;
use OpenApi\Attributes as OAT;

class ProfileController extends Controller
{
Expand Down
80 changes: 0 additions & 80 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
namespace App\Http\Controllers;

use App\Events\UserSignedUp;
use App\Http\Requests\Auth\AdminLoginRequest;
use App\Http\Requests\Auth\LoginRequest;
use App\Http\Requests\Auth\SignupRequest;
use App\Http\Resources\LoggedInAdminResource;
use App\Http\Resources\LoggedInUserResource;
use App\Services\AuthService;
use Illuminate\Http\JsonResponse;
Expand Down Expand Up @@ -145,82 +143,4 @@ public function logout(Request $request): JsonResponse

return Response::json(null, HttpResponse::HTTP_NO_CONTENT);
}

/**
* Login an admin.
*
* @param AdminLoginRequest $request
* @return JsonResponse
*
* @throws HttpException
* @throws NotFoundHttpException
*/
#[OAT\Post(
tags: ['adminAuth'],
path: '/api/admin/login',
summary: 'Login an admin',
operationId: 'AuthController.adminLogin',
requestBody: new OAT\RequestBody(
required: true,
content: new OAT\JsonContent(ref: '#/components/schemas/AdminLoginRequest')

),
responses: [
new OAT\Response(
response: HttpResponse::HTTP_OK,
description: 'Ok',
content: new OAT\JsonContent(ref: '#/components/schemas/LoggedInAdminResource')
),
new OAT\Response(
response: HttpResponse::HTTP_UNPROCESSABLE_ENTITY,
description: 'Unprocessable entity',
content: new OAT\JsonContent(ref: '#/components/schemas/ValidationError')
),
new OAT\Response(
response: HttpResponse::HTTP_UNAUTHORIZED,
description: 'Unauthorized',
content: new OAT\JsonContent(
properties: [
new OAT\Property(
property: 'message',
type: 'string',
example: 'Invalid credentials.'
),
]
)
),
]
)]
public function adminLogin(AdminLoginRequest $request): JsonResponse
{
$admin = $this->authService->loginAdmin($request);

return Response::json(new LoggedInAdminResource($admin));
}

/**
* Logout an admin.
*
* @param Request $request
* @return JsonResponse
*/
#[OAT\Post(
tags: ['adminAuth'],
path: '/api/admin/logout',
summary: 'Logout an admin',
operationId: 'AuthController.adminLogout',
security: [['BearerToken' => []]],
responses: [
new OAT\Response(
response: HttpResponse::HTTP_NO_CONTENT,
description: 'No content'
),
]
)]
public function adminLogout(Request $request): JsonResponse
{
$this->authService->logoutAdmin($request->user());

return Response::json(null, HttpResponse::HTTP_NO_CONTENT);
}
}
55 changes: 41 additions & 14 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
Expand Down Expand Up @@ -32,27 +33,53 @@ public function boot()
Route::middleware('api')
->prefix('api')
->group(function () {
require base_path('routes/api/auth.php');

Route::group([
'middleware' => ['auth:user'],
], function () {
require base_path('routes/api/profile.php');
});

Route::group([
'middleware' => ['auth:admin'],
'prefix' => 'admin'
], function () {
require base_path('routes/api/admin/profile.php');
});
$this->getUserRoutes();
$this->getAdminRoutes();
});

Route::middleware('web')
->group(base_path('routes/web.php'));
});
}

/**
* Get the user routes.
*
* @return void
*
* @throws BindingResolutionException
*/
private function getUserRoutes()
{
require base_path('routes/api/auth.php');

Route::group([
'middleware' => ['auth:user'],
], function () {
require base_path('routes/api/profile.php');
});
}

/**
* Get the admin routes.
*
* @return void
*/
private function getAdminRoutes()
{
Route::group([
'prefix' => 'admin',
], function () {
require base_path('routes/api/admin/auth.php');

Route::group([
'middleware' => ['auth:admin'],
], function () {
require base_path('routes/api/admin/profile.php');
});
});
}

/**
* Configure the rate limiters for the application.
*
Expand Down
94 changes: 47 additions & 47 deletions public/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,53 @@ servers:
url: 'http://localhost'
description: 'Local API server'
paths:
/api/admin/login:
post:
tags:
- adminAuth
summary: 'Login an admin'
description: 'Login an admin.'
operationId: Admin.AuthController.login
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/AdminLoginRequest'
responses:
'200':
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/LoggedInAdminResource'
'422':
description: 'Unprocessable entity'
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
'401':
description: Unauthorized
content:
application/json:
schema:
properties:
message: { type: string, example: 'Invalid credentials.' }
type: object
/api/admin/logout:
post:
tags:
- adminAuth
summary: 'Logout an admin'
description: 'Logout an admin.'
operationId: Admin.AuthController.logout
responses:
'204':
description: 'No content'
security:
-
BearerToken: []
/api/admin/profile:
get:
tags:
Expand Down Expand Up @@ -98,53 +145,6 @@ paths:
security:
-
BearerToken: []
/api/admin/login:
post:
tags:
- adminAuth
summary: 'Login an admin'
description: 'Login an admin.'
operationId: AuthController.adminLogin
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/AdminLoginRequest'
responses:
'200':
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/LoggedInAdminResource'
'422':
description: 'Unprocessable entity'
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
'401':
description: Unauthorized
content:
application/json:
schema:
properties:
message: { type: string, example: 'Invalid credentials.' }
type: object
/api/admin/logout:
post:
tags:
- adminAuth
summary: 'Logout an admin'
description: 'Logout an admin.'
operationId: AuthController.adminLogout
responses:
'204':
description: 'No content'
security:
-
BearerToken: []
/api/profile:
get:
tags:
Expand Down
Loading

0 comments on commit cda4c85

Please sign in to comment.