PHP code example of smallhadroncollider / social-login

1. Go to this page and download the library: Download smallhadroncollider/social-login library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

smallhadroncollider / social-login example snippets


/*
 * GET https://mysite.com/login
 */

// start a session
session_start();
$sessionID = session_id();

// get the list of supported social login urls
$urls = $http->get("https://api.mysite.com/v1/auth/social/urls", [
    "session_id" => $sessionID,
]);

/*
 * GET https://api.mysite.com/v1/auth/social/urls
 */

use SmallHadronCollider\SocialLogin\SocialLogin;
use SmallHadronCollider\SocialLogin\Storers\MemcachedStorer;

$config = [
    "facebook" => [
        "client_id" => "1",
        "client_secret" => "secret",
        "redirect_url" => "https://mysite.com/login/social?platform=facebook",
    ],
];

$storer = new MemcachedStorer();

$login = new SocialLogin($config, $storer);
$login->setSessionID($_GET["session_id"]);

return json_encode($login->getAuthUrls());

/*
 * GET https://mysite.com/login
 */

<a href="<?= $urls["facebook"] 

/*
 * GET https://mysite.com/login/social?platform=facebook&code=blahblahblah&state=rhubarbrhubarb
 */

$code = $_GET["code"];
$state = $_GET["state"];

$userDetails = $http->post("https://api.mysite.com/v1/auth/social", [
    "code" => "{$code}:{$state}",
    "platform" => $_GET["platform"],
    "session_id" => $sessionID,
]);

/*
 * POST https://api.mysite.com/v1/auth/social
 */

// Setup SocialLogin as before... (see above)
$login = (new SocialLogin($config, $storer))->setSessionID($_POST["session_id"]);

$code = $_POST["code"];

$platform = $login->platform($_POST["platform"]);
$token = $platform->getTokenFromCode($code);
$user = $platform->getUserFromToken($code);

/*
 * POST https://api.mysite.com/v1/auth/social
 */

if (/* user does not exist in database */) {
    // Create a new user from
    // $user->id, $user->name, $user->email
}

if (/* user in database has different social id to logged in user */) {
    // return a 401 page
}

/*
 * POST https://api.mysite.com/v1/auth/social
 */

return json_encode([
    "user_id" => $user->id,
    "token" => $platform->addPlatform($token),
]);

/*
 * GET https://mysite.com/login/social?platform=facebook&code=blahblahblah&state=rhubarbrhubarb
 */

$loggedIn = $http->post("https://api.mysite.com/v1/auth", [
    "client_id" => "oauthclientid",
    "client_secret" => "blahblahblah",
    "grant_type" => "password",
    "username" => $userDetails->user_id,
    "password" => $userDetails->token,
]);

/*
 * POST https://api.mysite.com/v1/auth
 */

function checkUserLoggedIn($username, $password) {
    if (/* $user using social login */) {
        $login = new SocialLogin($config, $storer);
        $platform = $login->platformFromToken($token);
        $token = $platform->stripPlatform($token);
        $user = $platform->getUserFromToken($token);

        if ($username === $user->id) {
            return true;
        }

        return false;
    } else {
        // Login normally (e.g. check the password)
    }
}