zlt/laravel-api-auth is a Laravel package.
It currently has 0 GitHub stars and 212 downloads on Packagist (latest version v0.2.9.5).
Install it with composer require zlt/laravel-api-auth.
Discover more Laravel packages by zlt
or browse all Laravel packages to compare alternatives.
Last updated
This package provides basic api authorization and easy-to-use servicing your Models.
Post::whereBetween(...)->orderByDesc(...)->get(), you can use like
PostService::get([
'startDate' => '2022-01-01',
'endDate' => '2022-06-01',
'orderBy' => 'date',
'isDesc' => true,
])
$ composer require zlt/laravel-api-auth
$ php artisan vendor:publish --provider="Zlt\LaravelApiAuth\LaravelApiAuthServiceProvider"
The following are the default config.
<?php
return [
/*
* If true, `api/login` and `api/register` routes will be registered.
*/
'shouldIncludeRoutes' => true,
/*
* Define your auth model
*/
'authUser' => App\Models\User::class,
/*
* If true, action will try to create token in registration and appending token in login.
*/
'enableSanctum' => true,
/*
* In some cases, you might want to use other column than `email`
*/
'username' => 'email',
/*
* Validation rule for password in registration
*/
'password:rule' => 'required|min:8',
/*
* Determine if api routes should have `application/json` as Accept header
*/
'addApplicationJsonHeader' => true,
/**
* When using `Zlt\LaravelApiAuth\Utils\CanCache` trait, you can define cache prefix here.
*/
'cache-prefix' => null,
];
There are already api routes defined for you to authorize a user such as /api/login, /api/register
and /api/delete.
In order to disable those routes, you can set the shouldIncludeRoutes option to false in
the config/laravel-api-auth.php file.
Zlt\LaravelApiAuth\Actions\Login:class can be used to log user in.
values as parameter. Let action handle validation and checking credentials.Zlt\LaravelApiAuth\Support\ApiResponse will be always returned with corresponding message, additional data and
status code.$values = ['email'=>'[email protected]','password' => '12345678'];
$login = new Zlt\LaravelApiAuth\Actions\Login;
$response = $login(values: $values);
Zlt\LaravelApiAuth\Actions\Register::class can be used to register a user.
values as parameter. Let action handle validation and checking credentials.Zlt\LaravelApiAuth\Support\ApiResponse will be always returned with corresponding message, additional data and
status code.$values = ['email'=>'[email protected]','password' => '12345678'];
$register = new Zlt\LaravelApiAuth\Actions\Register;
$response = $register(values:$values);
Zlt\LaravelApiAuth\Actions\Delete::class can be used to delete a user.
values as parameter. Let action handle validation and checking credentials.Zlt\LaravelApiAuth\Support\ApiResponse will be always returned with corresponding message, additional data and
status code.$values = ['email'=>'[email protected]'];
$delete = new Zlt\LaravelApiAuth\Actions\Delete;
$response = $delete($values);
The idea of servicing is to provide a easy-to-use way to build database query and execute it.
For example, you have a Post model and you want to build query instead of using
like Post::whereBetween('date',['2022-01-01','2022-06-01'])->orderByDesc('date')->get(). Then you can use the
following service:
<?php
namespace App\Services;
use App\Models\Post;
use Zlt\LaravelApiAuth\Enums\Operator;
use Zlt\LaravelApiAuth\Services\BaseService;
use Zlt\LaravelApiAuth\Support\QueryableColumn;
/**
* @method static ApiResponse get(array $values);
* @method static ApiResponse count(array $values);
*/
class PostService extends BaseService
{
public function __construct()
{
$this->registerQueryColumn(QueryableColumn::from(
'date',
['startDate', 'endDate'],
'date_format:Y-m-d|required_with:startDate|required_with:endDate',
Operator::BETWEEN,
));
parent::__construct(Post::query());
}
static function getInstance(): static
{
return new static();
}
}
Then call static method get or count to get the result.
PostService::get([
'startDate' => '2022-01-01',
'endDate' => '2022-06-01',
'orderBy' => 'date',
'isDesc' => true,
]);
PostService::count([
'startDate' => '2022-01-01',
'endDate' => '2022-06-01',
'orderBy' => 'date',
'isDesc' => true,
]);
First, you need to extend Zlt\LaravelApiAuth\Services\BaseService and implement the getInstance method.
class YourService extends Zlt\LaravelApiAuth\Services\BaseService{
static function getInstance(): static
{
}
}
Then, you need to pass your Builder instance to constructor or inside the constructor.
// Pass to constructor
static function getInstance(): static
{
return new static(YourModel::query());
}
// Inside constructor
public function __construct()
{
parent::__construct(YourModel::query());
}
You can use some built-in features offered by package.
orderBylimitoffsethiddenFieldsselectedFieldsFor example,
PostService::get([
'orderBy' => 'date',
'isDesc' => true,
'limit' => 10,
'offset' => 0,
'hiddenFields' => ['id','created_at','updated_at'],
'selectedFields' => ['title','date'],
]);
You can register queryable column and cast the value before using in query.
For example, you want to get posts between two dates. ( Assume there is date column)
// Register queryable column in constructor
class YourService extends Zlt\LaravelApiAuth\Services\BaseService{
public function __construct()
{
$this->registerQueryColumn(QueryableColumn::from(
'date', // DB Column
['startDate', 'endDate'], // Request Parameters
'date_format:Y-m-d|required_with:startDate|required_with:endDate', // Validation Rule
Operator::BETWEEN, // Operator
));
parent::__construct(YourModel::query());
}
}
// And then
YourService::get([
'startDate' => '2022-01-01',
'endDate' => '2022-06-01',
]);
You can also cast the value before using in query.
public function __construct()
{
$this->registerQueryColumn(QueryableColumn::from(
'date', // DB Column
['startDate', 'endDate'], // Request Parameters
'date_format:Y-m-d|required_with:startDate|required_with:endDate', // Validation Rule
Operator::BETWEEN, // Operator
function ($value, $parameter) {
$date = new Carbon($value);
if ($parameter === 'startDate') {
return $date->startOfDay()->timestamp;
}
return $date->endOfDay()->timestamp;
}
));
parent::__construct(YourModel::query());
}
This package also offers a trait to cache response.
use Zlt\LaravelApiAuth\Utils\CanCache;
class YourClass {
use CanCache;
public function yourMethod(Request $request){
if($this->checkIfCacheKeyExists($request)){
return $this->getCacheData($request);
}
$response = $this->computeData();
$this->storeCache($request,$response);
return $response;
}
}
Why I created this package is because I needed to use such features in my projects. But now I think it would probably help others too. So I decided to share it with you. I hope you find it useful.
Feel free to contribute to this package.
If you find it useful, please give it a star or buy me a coffee via Binance.
Cheers!