<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
californiamountainsnake / simple-laravel-auth-system example snippets
class MyValidatorService extends AuthValidatorService
{
public function api_token(): array
{
return [
AuthMiddleware::API_TOKEN_REQUEST_PARAM => [
'min:64',
'max:64',
]
];
}
}
class AppServiceProvider extends ServiceProvider
{
public function boot (): void {
$this->app->singleton(AuthRoleService::class, static function () {
return new AuthRoleService(true);
});
}
public function register(): void {
// Binding Interfaces To Implementations.
$this->app->singleton(AuthenticatorInterface::class, BasicHttpAuthenticator::class);
$this->app->singleton(AuthValidatorServiceInterface::class, YourValidatorService::class);
$this->app->singleton(AuthUserRepository::class, YourUserRepository::class);
$this->app->singleton(AuthHashFunction::class, static function () {
return new class implements AuthHashFunction
{
public function getHashFunction(): callable
{
return static function ($_token) {
// You can use something like this:
// return sha1($_token);
return $_token;
};
}
};
});
}
}
class ApiUserController extends AuthApiUserController
{
// Realise the abstract methods.
}
use CaliforniaMountainSnake\JsonResponse\JsonResponse;
use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use InvalidArgumentException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
class Handler extends ExceptionHandler {
/**
* Render an exception into an HTTP response.
*
* @param Request $request
* @param Exception $exception
*
* @return Response
* @throws BindingResolutionException
* @throws InvalidArgumentException
*/
public function render($request, Exception $exception)
{
if ($exception instanceof MethodNotAllowedException || $exception instanceof MethodNotAllowedHttpException) {
return JsonResponse::error([__('auth_middleware.method_not_allowed')],
JsonResponse::HTTP_METHOD_NOT_ALLOWED)
->withCors()// Optional.
->make();
}
return parent::render($request, $exception);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.