Skip to content

Core/Pourashava oauth2 provider for laravel socialite.

Notifications You must be signed in to change notification settings

epourashava/core-services

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Core Services

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)

Installation & Basic Usage

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

Socialite Usage

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'),
],

Add variables to .env

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

Usage

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();

Callback

$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();

Model

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.

Usage

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();
});

API Client

The Core API Client is a simple client that allows you to make requests to the Core API. It uses Laravel's HTTP client.

Usage

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();

About

Core/Pourashava oauth2 provider for laravel socialite.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages