star-insure / api-client
Star Insure API Client
Requires
- php: ^8.1
- illuminate/contracts: ^9.0|^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: ^10.1
- dev-master
- 8.1.1
- 8.0.1
- 8.0.0
- 7.1.1
- 7.1.0
- 7.0.0
- 6.4.1
- 6.4.0
- 6.3.1
- 6.3.0
- 6.2.0
- 6.1.0
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.21
- 5.0.20
- 5.0.19
- 5.0.18
- 5.0.17
- 5.0.16
- 5.0.15
- 5.0.14
- 5.0.13
- 5.0.12
- 5.0.11
- 5.0.10
- 5.0.9
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.1.0
- 4.0.0
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.7.0
- 2.6.3
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.1
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.0
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-SI-204-use-spatie-once-for-memoizing-request-user
This package is auto-updated.
Last update: 2025-03-09 21:08:42 UTC
README
A package for Laravel apps that includes a wrapper for the Star Inure API and scaffolds out routes, controllers and middleware for authenticating with the Star auth app.
Installation
You can install the package via composer:
composer require star-insure/api-client
Add these values to your .env
file:
# API SIS_API_URL=http://api.starinsure.test SIS_API_AUTH_STRATEGY=user|app SIS_API_TOKEN=dev SIS_API_GROUP_ID=2 # OAuth client SIS_API_CLIENT_ID=app SIS_API_CLIENT_SECRET=secret
Publish config:
php artisan vendor:publish --tag=starinsure
Append routes
Add the below line to your /routes/web.php
file.
require __DIR__.'/oauth.php';
Usage
API
Call the Star API by instantiating a new client, or using the StarInsure\Api\Facades\StarApi
facade.
StarApi::get('/users/me'); StarApi::get('/users');
Auth
Use Laravel's provided "auth" middleware.
Route::get('/protected', function () { return 'Only authenticated users can see this.'; })->middleware('auth');
Or a route group:
Route::middleware(['auth'])->group(function () { Route::get('/protected', function () { return "Only authenticated users can see this."; }); });
The user will automatically be redirected to the auth server if no valid session, and sent back to their request destination after successfully logging in.
Helper functions
Following the Laravel style, you also have the option of using helper functions. To register the auth helper so it overrides Laravel's built-in auth()
functions, follow the steps below.
This package has a dependency on funkjedi/composer-include-files
, which allows you to load your own functions prior to any of your dependencies global functions.
Create a helpers.php
file within the app
directory (or edit your existing one):
// We use this helper purely to help the IDE recognise the global function's return type if (! function_exists('auth') && isset($_ENV['APP_ENV']) && $_ENV['APP_ENV'] === 'ide') { /** * Get the available auth instance. */ function auth(): StarInsure\Api\StarAuthManager { return new \StarInsure\Api\StarAuthManager( app(), config('star.api_url').'/api/'.config('star.version'), ); } } if (! function_exists('api')) { /** * Global helper to create an instance of the StarApi client. */ function api(string $token = null) { return new \StarInsure\Api\StarApi( auth_strategy: config('star.auth_strategy'), version: config('star.version'), apiTokenOverride: $token, ); } } if (! function_exists('appApi')) { /** * An instance of the API client for non-authenticated routes */ function appApi() { return new \StarInsure\Api\StarApi( 'app', config('star.version'), config('star.token'), config('star.group_id') ); } }
Autoload your helpers file in composer.json
:
"autoload": { ... "files": [ "app/helpers.php" ] },
After adding the helpers file to composer.json, you'll need to dump the autoloader
composer dump-autoload
You can now use the global helper functions and not worry about namespaces/imports.
$user = auth()->user(); $id = auth()->id(); $group = auth()->group(); $role = auth()->role(); $permissions = auth()->permissions(); $context = auth()->context(); $apiResponse = api()->get('/users/me', [ 'include' => 'groups' ]);