This package provides a set of services for Laravel applications. It includes:
- Socialite Provider for Core Auth
- Core API Client
- Core User Model (for use with the Core API)
Add this into your composer.json file
"repositories": [
{
"url": "https://github.com/epourashava/core-services.git",
"type": "git"
}
],
composer require epourashava/core-services
php artisan vendor:publish --tag=core-config
We use Socialite for authentication. To use the Core provider, you need to add the following to your config/services.php
file:
'core-oauth2' => [
'client_id' => env('CORE_CLIENT_ID'),
'client_secret' => env('CORE_CLIENT_SECRET'),
'redirect' => env('CORE_REDIRECT_URI'),
'base_url' => env('CORE_BASE_URL'),
],
Core Auth may require you to authorize against a custom URL, which you may provide as the base URL.
CORE_BASE_URL=http://core.test/
CORE_CLIENT_ID=your-client-id
CORE_CLIENT_SECRET=your-client-secret
CORE_REDIRECT_URI=http://your-callback-url
You should now be able to use the provider like you would regularly use Socialite (assuming you have the facade installed or run composer require laravel/socialite
):
return Socialite::driver('core-oauth2')->redirect();
$user = Socialite::driver('core-oauth2')->user();
$token = $user->token;
$refreshToken = $user->refreshToken;
$expiresIn = $user->expiresIn;
// $user->getId();
// $user->getNickname();
// $user->getName();
// $user->getEmail();
// $user->getAvatar();
The Core User model is a simple model that extends Laravel's default User model. It includes a few extra fields that are returned from the Core API.
use Core\Models\User as CoreUser;
class User extends CoreUser
{
//
}
Update the migration file to use the Core User model:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->text('access_token')->nullable();
$table->text('refresh_token')->nullable();
$table->timestamp('expires_at')->nullable();
$table->string('avatar')->nullable();
$table->string('provider')->default('core');
$table->rememberToken();
$table->timestamps();
});
The Core API Client is a simple client that allows you to make requests to the Core API. It uses Laravel's HTTP client.
use Core\Client;
$client = new Client();
$response = $client->get('/users');
// Or use the OAuth2Client class to make authenticated requests
use Core\OAuth2Client;
$client = OAuth2Client::instance();
// Get the user
$response = $client->getUser();