mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
login module
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Accounts\ControllersLogic;
|
||||
|
||||
use App\Classes\Modules\Accounts\DataTransferObjects\AuthenticationCredentialsObject;
|
||||
use App\Classes\Modules\Accounts\Services\AuthenticatesUser;
|
||||
use App\Classes\Modules\Accounts\Services\AuthenticationRedirect;
|
||||
use App\Classes\Modules\Accounts\Standards\Rules\CanAuthenticateUser;
|
||||
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
||||
use App\Classes\ValueObjects\Constants\Notifications;
|
||||
use ErrorException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AuthenticateUserLogic extends AbstractControllerLogic
|
||||
{
|
||||
|
||||
protected function notification():array {
|
||||
return [
|
||||
'title' => 'Authentication',
|
||||
'message' => 'You have successfully logged in to your account'
|
||||
];
|
||||
}
|
||||
|
||||
/** @var CanAuthenticateUser */
|
||||
private $canAuthenticateUser;
|
||||
|
||||
/** @var AuthenticatesUser */
|
||||
private $authenticatesUser;
|
||||
|
||||
/** @var AuthenticationRedirect */
|
||||
private $authenticationRedirect;
|
||||
|
||||
/**
|
||||
* AuthenticateUserLogic constructor.
|
||||
* @param CanAuthenticateUser $canAuthenticateUser
|
||||
* @param AuthenticatesUser $authenticatesUser
|
||||
* @param AuthenticationRedirect $authenticationRedirect
|
||||
*/
|
||||
public function __construct(CanAuthenticateUser $canAuthenticateUser, AuthenticatesUser $authenticatesUser, AuthenticationRedirect $authenticationRedirect)
|
||||
{
|
||||
$this->canAuthenticateUser = $canAuthenticateUser;
|
||||
$this->authenticatesUser = $authenticatesUser;
|
||||
$this->authenticationRedirect = $authenticationRedirect;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
* @throws ErrorException
|
||||
*/
|
||||
protected function logic(Request $request): JsonResponse {
|
||||
|
||||
try {
|
||||
$object = new AuthenticationCredentialsObject($request->input('email'),
|
||||
$request->input('password'), $request->input('remember_me'));
|
||||
|
||||
$this->canAuthenticateUser->passes($object);
|
||||
|
||||
$token = $this->authenticatesUser->execute($object);
|
||||
|
||||
return $this->response(['access_token' => $token, 'redirect_url' => $this->authenticationRedirect->url()]);
|
||||
|
||||
} catch (\Exception $exception){
|
||||
throw new ErrorException($exception->getMessage(), $exception->getCode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Accounts\DataTransferObjects;
|
||||
|
||||
|
||||
use App\Classes\Interfaces\DataTransferObject;
|
||||
|
||||
class AuthenticationCredentialsObject implements DataTransferObject
|
||||
{
|
||||
|
||||
/** @var string */
|
||||
private $email;
|
||||
|
||||
/** @var string */
|
||||
private $password;
|
||||
|
||||
/** @var boolean */
|
||||
private $rememberUser;
|
||||
|
||||
/**
|
||||
* AuthenticationCredentials constructor.
|
||||
* @param string $email
|
||||
* @param string $password
|
||||
* @param bool $rememberUser
|
||||
*/
|
||||
public function __construct(string $email, string $password, ?bool $rememberUser = false)
|
||||
{
|
||||
$this->email = $email;
|
||||
$this->password = $password;
|
||||
$this->rememberUser = $rememberUser;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail(): string
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPassword(): string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isRememberUser(): bool
|
||||
{
|
||||
return $this->rememberUser;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Accounts\Services;
|
||||
|
||||
|
||||
use App\Classes\Exceptions\AccessUnauthorisedException;
|
||||
use App\Classes\Modules\Accounts\DataTransferObjects\AuthenticationCredentialsObject;
|
||||
use Tymon\JWTAuth\Facades\JWTAuth;
|
||||
|
||||
class AuthenticatesUser
|
||||
{
|
||||
|
||||
/**
|
||||
* @param AuthenticationCredentialsObject $object
|
||||
* @return false|string
|
||||
* @throws AccessUnauthorisedException
|
||||
*/
|
||||
public function execute(AuthenticationCredentialsObject $object){
|
||||
|
||||
if (! $token = JWTAuth::attempt(['email' => $object->getEmail(), 'password' => $object->getPassword()])) {
|
||||
throw new AccessUnauthorisedException('These credentials do not match our records.');
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Accounts\Services;
|
||||
|
||||
class AuthenticationRedirect
|
||||
{
|
||||
|
||||
public function url(){
|
||||
return route('dashboard');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Accounts\Standards\Rules;
|
||||
|
||||
|
||||
use App\Classes\General\Abstracts\AbstractRule;
|
||||
use App\Classes\Exceptions\RequestValidationException;
|
||||
use App\Classes\Modules\Accounts\DataTransferObjects\AuthenticationCredentialsObject;
|
||||
use App\Classes\Modules\Accounts\Standards\Validators\UserAuthenticationValidation;
|
||||
|
||||
class CanAuthenticateUser extends AbstractRule
|
||||
{
|
||||
|
||||
/** @var UserAuthenticationValidation */
|
||||
private $userAuthenticationValidation;
|
||||
|
||||
/**
|
||||
* CanAuthenticateUser constructor.
|
||||
* @param UserAuthenticationValidation $userAuthenticationValidation
|
||||
*/
|
||||
public function __construct(UserAuthenticationValidation $userAuthenticationValidation)
|
||||
{
|
||||
$this->userAuthenticationValidation = $userAuthenticationValidation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected function authorized(): bool
|
||||
{
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param AuthenticationCredentialsObject $object
|
||||
* @return bool
|
||||
* @throws RequestValidationException
|
||||
*/
|
||||
protected function validators($object): bool
|
||||
{
|
||||
return $this->userAuthenticationValidation->validate($object);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param AuthenticationCredentialsObject $object
|
||||
* @return bool
|
||||
*/
|
||||
protected function criteria($object): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Accounts\Standards\Validators;
|
||||
|
||||
use App\Classes\General\Abstracts\AbstractValidation;
|
||||
use App\Classes\Modules\Accounts\DataTransferObjects\AuthenticationCredentialsObject;
|
||||
|
||||
class UserAuthenticationValidation extends AbstractValidation
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param AuthenticationCredentialsObject $object
|
||||
* @return array
|
||||
*/
|
||||
protected function data($object): array {
|
||||
return [
|
||||
'email' => $object->getEmail(),
|
||||
'password' => $object->getPassword()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function rules(): array {
|
||||
return [
|
||||
'email' => 'required|email',
|
||||
'password' => 'required'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function messages(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user