girift/apitwist-sso-laravel-client

This is the client Integration with ApiTwist SSO.

0.1.15 2024-04-26 12:38 UTC

README

Latest Version on Packagist GitHub Code Style Action Status Total Downloads

Client integration for ApiTwist SSO.

Installation

Install the package via composer:

composer require girift/apitwist-sso-laravel-client

Add SSO config to your .env file:

SSO_CLIENT_ID=client_id
SSO_CLIENT_SECRET=client_secret
SSO_DOMAIN='https://sso.apitwist.com'

Add HasSsoTokens trait to your User model

// ...
use Girift\SSO\Traits\HasSsoTokens;

class User extends Authenticatable
{
    use HasSsoTokens;
    // ...
}

Add middlewares to your app/Http/Kernel.php file $routeMiddleware array:

protected $routeMiddleware = [
    // ...
    'sso.auth' => \Girift\SSO\Http\Middleware\SsoAuthenticate::class,
    'sso.api' => \Girift\SSO\Http\Middleware\SsoApiAuthenticate::class,
];

You can publish and run the migrations with:

php artisan vendor:publish --tag="sso-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="sso-config"

This is the contents of the published config file:

return [
    'client_id' => env('SSO_CLIENT_ID'),
    'client_secret' => env('SSO_CLIENT_SECRET'),
    'redirect_url' => config('app.url') . '/sso/callback',
    'sso_domain' => env('SSO_DOMAIN', 'https://sso.apitwist.com'),
    'authorize_url' => config('sso.sso_domain').'/oauth/authorize',
    'api_url' => config('sso.sso_domain').'/oauth/token',
    'logout_url' => config('sso.sso_domain'). '/logout',
    'get_user_url' => config('sso.sso_domain').  '/api/user',
];

Usage

Use sso.auth along with web middleware in your routes/web.php file:

Route::middleware([ 'web', 'sso.auth' ])->get('/route', function () {
    // Your routes
});

If you see Session store not set on request. error, add theese middlewares in your app/Http/Kernel.php file $middleware array:

protected $middleware = [
    // ...
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
];

Use named routes as authentication routes:

sso.login
sso.logout

Add sso.loggedIn named route to your home page:

Route::middleware([ 'web', 'sso.auth' ])->get('/home', function () {
    // Your home page
})->name('sso.loggedIn')->name('home');

Credits

License

The MIT License (MIT). Please see License File for more information.