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"]
/*
* 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
}