uccello/uccello-api

A REST API for Uccello.

v2.0.11 2021-07-16 14:34 UTC

README

composer require uccello/uccello-api

Config JWT

Publish the config

Run the following command to publish the package config file:

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"

You should now have a config/jwt.php file that allows you to configure the basics of this package.

Generate secret key

I have included a helper command to generate a key for you:

php artisan jwt:secret

This will update your .env file with something like JWT_SECRET=foobar

It is the key that will be used to sign your tokens. How that happens exactly will depend on the algorithm that you choose to use.

Update your User model

Firstly you need to implement the Tymon\JWTAuth\Contracts\JWTSubject contract on your User model, which requires that you implement the 2 methods getJWTIdentifier() and getJWTCustomClaims().

The example below should give you an idea of how this could look. Obviously you should make any changes, as necessary, to suit your own needs.

<?php

namespace App;

use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    // Rest omitted for brevity

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}

Configure Auth guard

Note: This will only work if you are using Laravel 5.2 and above.

Inside the config/auth.php file you will need to make a few changes to configure Laravel to use the jwt guard to power your application authentication.

Make the following changes to the file:

'defaults' => [
    'guard' => 'api',
    'passwords' => 'users',
],

...

'guards' => [
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],

Here we are telling the api guard to use the jwt driver, and we are setting the api guard as the default.

We can now use Laravel's built in Auth system, with jwt-auth doing the work behind the scenes!

Config CORS

The provided Spatie\Cors\Cors middleware must be registered in the global middleware group.

// app/Http/Kernel.php

protected $middleware = [
    ...
    \Spatie\Cors\Cors::class
];
php artisan vendor:publish --provider="Spatie\Cors\CorsServiceProvider" --tag="config"

Config

You can add this parameters to config/uccello.php:

return [
    ...
    'api' => [
        'items_per_page' => 300, // Items per page
        'max_items_per_page' => 800, // Max items per page (useful if length URL param greater than max_item_per_page)
        'throws_exception' => true, // If true send an email to user with username defined below
        'username_to_notify_on_exception' => env('USERNAME_TO_NOTIFY_ON_EXCEPTION')
    ],

Daily usage

You can pass several params to the request URL:

Param Description Default Example
descendants Activate (1) or not (0) descendant view according to the user's roles. 0 &descendants=1
select Semicolon separated list of columns to retrieve. &select=name;created_at
order Semicolon and comma separated list of the order clauses. &order=name,asc;email,desc
with Semicolon separated list of the relations to add in the response. &with=domain;clients
length Pagination length. 100 &length=300