new:[authentication apis]add classes required for refresh token API #2

This commit is contained in:
weichien00
2021-06-26 17:16:01 +08:00
parent e0f72c25fb
commit 38cccef2fd
4 changed files with 112 additions and 0 deletions
@@ -0,0 +1,47 @@
<?php
namespace App\Classes\Modules\Accounts\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Accounts\Processors\RefreshAuthenticationTokenProcessor;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class RefreshAuthenticationTokenLogic extends AbstractControllerLogic
{
protected function notification():array {
return [
'title' => 'Refresh Authentication',
'message' => 'You have successfully your account authentication'
];
}
/** @var RefreshAuthenticationTokenProcessor */
private $refreshAuthenticationTokenProcessor;
/**
* RefreshAuthenticationTokenLogic constructor.
* @param RefreshAuthenticationTokenProcessor $refreshAuthenticationTokenProcessor
*/
public function __construct(RefreshAuthenticationTokenProcessor $refreshAuthenticationTokenProcessor)
{
$this->refreshAuthenticationTokenProcessor = $refreshAuthenticationTokenProcessor;
}
/**
* @param Request $request
* @return JsonResponse
* @throws \App\Classes\Exceptions\InternalServerErrorException
*/
protected function logic(Request $request): JsonResponse {
/** @var User $user */
$user = Auth()->user();
return $this->response($this->refreshAuthenticationTokenProcessor->execute($user));
}
}
@@ -0,0 +1,43 @@
<?php
namespace App\Classes\Modules\Accounts\Processors;
use App\Classes\Modules\Accounts\Services\GeneratesAuthenticationToken;
use App\Classes\Modules\Accounts\Services\InvalidatesAuthenticationToken;
use App\Models\User;
class RefreshAuthenticationTokenProcessor
{
/** @var InvalidatesAuthenticationToken */
private $invalidatesAuthenticationToken;
/** @var GeneratesAuthenticationToken */
private $generatesAuthenticationToken;
/**
* RefreshAuthenticationTokenLogic constructor.
* @param InvalidatesAuthenticationToken $invalidatesAuthenticationToken
* @param GeneratesAuthenticationToken $generatesAuthenticationToken
*/
public function __construct(InvalidatesAuthenticationToken $invalidatesAuthenticationToken, GeneratesAuthenticationToken $generatesAuthenticationToken)
{
$this->invalidatesAuthenticationToken = $invalidatesAuthenticationToken;
$this->generatesAuthenticationToken = $generatesAuthenticationToken;
}
/**
* @param User $user
* @return array
* @throws \App\Classes\Exceptions\InternalServerErrorException
*/
public function execute(User $user): array {
$this->invalidatesAuthenticationToken->execute();
return ['refresh_token' => $this->generatesAuthenticationToken->execute($user)];
}
}
@@ -0,0 +1,21 @@
<?php
namespace App\Http\Controllers\Accounts;
use App\Classes\Modules\Accounts\ControllersLogic\RefreshAuthenticationTokenLogic ;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class RefreshAuthenticationTokenController
{
/**
* @param Request $request
* @param RefreshAuthenticationTokenLogic $logic
* @return JsonResponse
*/
public function refresh(Request $request, RefreshAuthenticationTokenLogic $logic): JsonResponse {
return $logic->execute($request);
}
}
+1
View File
@@ -16,6 +16,7 @@ Route::group(['prefix' => 'account', 'namespace' => 'Accounts', 'as' => 'account
Route::group(['middleware' => 'valid.token'], function () {
Route::get('/logout', 'UserAuthenticationLogoutController@logout')->name('logout');
Route::get('/refresh', 'RefreshAuthenticationTokenController@refresh')->name('refresh');
});
});
});