Giter VIP home page Giter VIP logo

codeigniter4-socialite's Introduction

About

CodeIgniter4 Socialite is Forked from Laravel Socialite wrapper around OAuth 1 & OAuth 2 libraries for working with codeigniter4 compatibility.

Introduction

CodeIgniter4 Socialite provides an expressive, fluent interface to OAuth authentication with Facebook, Google, LinkedIn, GitHub, GitLab and Bitbucket. It handles almost all of the boilerplate social authentication code you are dreading writing.

Installation

To get started with Socialite, use the Composer package manager to add the package to your project's dependencies:

composer require agungsugiarto/codeigniter4-socialite

Configuration

Copy the config file from vendor/agungsugiarto/codeigniter4-socialite/src/Config/Socialite.php to config folder of your codeigniter4 application change the namespace to Config and change class extends from BaseConfig to \Fluent\Socialite\Config\Socialite

Before using Socialite, you will need to add credentials for the OAuth providers your application utilizes. These credentials should be placed in your application's app/Config/Socialite.php configuration file, and should use the key facebook, linkedin, google, github, gitlab, or bitbucket, depending on the providers your application requires:

/**
 * {@inheritdoc}
 */
public $services = [
    // ..
    'github' => [
        'client_id'     => '',
        'client_secret' => '',
        'redirect'      => '',
    ],
];

If the redirect option contains a relative path, it will automatically be resolved to a fully qualified URL.

Authentication

Routing

To authenticate users using an OAuth provider, you will need two routes: one for redirecting the user to the OAuth provider, and another for receiving the callback from the provider after authentication. The example controller below demonstrates the implementation of both routes:

use Fluent\Socialite\Facades\Socialite;

$routes->get('auth/redirect', function () {
    return Socialite::driver('github')->redirect();
});

$routes->get('auth/callback', function () {
    $user = Socialite::driver('github')->user();
    // $user->token
});

The redirect method provided by the Socialite facade takes care of redirecting the user to the OAuth provider, while the user method will read the incoming request and retrieve the user's information from the provider after they are authenticated.

Optional Parameters

A number of OAuth providers support optional parameters in the redirect request. To include any optional parameters in the request, call the with method with an associative array:

use Fluent\Socialite\Facades\Socialite;

return Socialite::driver('google')
    ->with(['hd' => 'example.com'])
    ->redirect();

When using the with method, be careful not to pass any reserved keywords such as state or response_type.

Access Scopes

Before redirecting the user, you may also add additional "scopes" to the authentication request using the scopes method. This method will merge all existing scopes with the scopes that you supply:

use Fluent\Socialite\Facades\Socialite;

return Socialite::driver('github')
    ->scopes(['read:user', 'public_repo'])
    ->redirect();

You can overwrite all existing scopes on the authentication request using the setScopes method:

use Fluent\Socialite\Facades\Socialite;

return Socialite::driver('github')
    ->setScopes(['read:user', 'public_repo'])
    ->redirect();

Retrieving User Details

After the user is redirected back to your authentication callback route, you may retrieve the user's details using Socialite's user method. The user object returned by the user method provides a variety of properties and methods you may use to store information about the user in your own database. Different properties and methods may be available depending on whether the OAuth provider you are authenticating with supports OAuth 1.0 or OAuth 2.0:

$routes->get('/auth/callback', function () {
    $user = Socialite::driver('github')->user();

    // OAuth 2.0 providers...
    $token = $user->token;
    $refreshToken = $user->refreshToken;
    $expiresIn = $user->expiresIn;

    // All providers...
    $user->getId();
    $user->getNickname();
    $user->getName();
    $user->getEmail();
    $user->getAvatar();
});

Retrieving User Details From A Token (OAuth2)

If you already have a valid access token for a user, you can retrieve their details using Socialite's userFromToken method:

use Fluent\Socialite\Facades\Socialite;

$user = Socialite::driver('github')->userFromToken($token);

Stateless Authentication

The stateless method may be used to disable session state verification. This is useful when adding social authentication to an API:

use Fluent\Socialite\Facades\Socialite;

return Socialite::driver('google')->stateless()->user();

Contributing

Thank you for considering contributing to Socialite!.

License

CodeIgniter4 Socialite is open-sourced software licensed under the MIT license.

codeigniter4-socialite's People

Contributors

aaronpk avatar adamgoose avatar agungsugiarto avatar alihamze avatar barryvdh avatar bostjanob avatar browner12 avatar carusogabriel avatar crynobone avatar cupoftea696 avatar derekmd avatar driesvints avatar edgarsn avatar everywell avatar georgecoca avatar grahamcampbell avatar hareku avatar iliyazelenko avatar isaackearl avatar it-can avatar jdrzejb avatar joecohens avatar kerryjones avatar lucasmichot avatar luiz-brandao avatar max13 avatar ntzm avatar sammyk avatar taylorotwell avatar teertz avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.