This commit is contained in:
Jaybe
2025-03-05 14:10:06 +09:00
commit 7a77932344
1905 changed files with 122510 additions and 0 deletions
@@ -0,0 +1,99 @@
<?php
/**
* A simple example that shows how to use multiple providers, opening provider authentication in a pop-up.
*/
require 'path/to/vendor/autoload.php';
require 'config.php';
use Hybridauth\Exception\Exception;
use Hybridauth\Hybridauth;
use Hybridauth\HttpClient;
use Hybridauth\Storage\Session;
try {
$hybridauth = new Hybridauth($config);
$storage = new Session();
$error = false;
//
// Event 1: User clicked SIGN-IN link
//
if (isset($_GET['provider'])) {
// Validate provider exists in the $config
if (in_array($_GET['provider'], $hybridauth->getProviders())) {
// Store the provider for the callback event
$storage->set('provider', $_GET['provider']);
} else {
$error = $_GET['provider'];
}
}
//
// Event 2: User clicked LOGOUT link
//
if (isset($_GET['logout'])) {
if (in_array($_GET['logout'], $hybridauth->getProviders())) {
// Disconnect the adapter
$adapter = $hybridauth->getAdapter($_GET['logout']);
$adapter->disconnect();
} else {
$error = $_GET['logout'];
}
}
//
// Handle invalid provider errors
//
if ($error) {
error_log('Hybridauth Error: Provider ' . json_encode($error) . ' not found or not enabled in $config');
// Close the pop-up window
echo "
<script>
if (window.opener.closeAuthWindow) {
window.opener.closeAuthWindow();
}
</script>";
exit;
}
//
// Event 3: Provider returns via CALLBACK
//
if ($provider = $storage->get('provider')) {
$hybridauth->authenticate($provider);
$storage->set('provider', null);
// Retrieve the provider record
$adapter = $hybridauth->getAdapter($provider);
$userProfile = $adapter->getUserProfile();
$accessToken = $adapter->getAccessToken();
// add your custom AUTH functions (if any) here
// ...
$data = [
'token' => $accessToken,
'identifier' => $userProfile->identifier,
'email' => $userProfile->email,
'first_name' => $userProfile->firstName,
'last_name' => $userProfile->lastName,
'photoURL' => strtok($userProfile->photoURL, '?'),
];
// ...
// Close pop-up window
echo "
<script>
if (window.opener.closeAuthWindow) {
window.opener.closeAuthWindow();
}
</script>";
}
} catch (Exception $e) {
error_log($e->getMessage());
echo $e->getMessage();
}
@@ -0,0 +1,27 @@
<?php
/**
* Build a configuration array to pass to `Hybridauth\Hybridauth`
*
* Set the Authorization callback URL to https://path/to/hybridauth/examples/example_07/callback.php
* Understandably, you need to replace 'path/to/hybridauth' with the real path to this script.
*/
$config = [
'callback' => 'https://path/to/hybridauth/examples/example_07/callback.php',
'providers' => [
'Google' => [
'enabled' => true,
'keys' => [
'id' => '...',
'secret' => '...',
],
'scope' => 'email',
],
// 'Yahoo' => ['enabled' => true, 'keys' => ['key' => '...', 'secret' => '...']],
// 'Facebook' => ['enabled' => true, 'keys' => ['id' => '...', 'secret' => '...']],
// 'Twitter' => ['enabled' => true, 'keys' => ['key' => '...', 'secret' => '...']],
// 'Instagram' => ['enabled' => true, 'keys' => ['id' => '...', 'secret' => '...']],
],
];
@@ -0,0 +1,65 @@
<?php
/**
* Build a simple HTML page with multiple providers, opening provider authentication in a pop-up.
*/
require 'path/to/vendor/autoload.php';
require 'config.php';
use Hybridauth\Hybridauth;
$hybridauth = new Hybridauth($config);
$adapters = $hybridauth->getConnectedAdapters();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example 07</title>
<script>
function auth_popup(provider) {
// replace 'path/to/hybridauth' with the real path to this script
var authWindow = window.open('https://path/to/hybridauth/examples/example_07/callback.php?provider=' + provider, 'authWindow', 'width=600,height=400,scrollbars=yes');
window.closeAuthWindow = function () {
authWindow.close();
}
return false;
}
</script>
</head>
<body>
<h1>Sign in</h1>
<ul>
<?php foreach ($hybridauth->getProviders() as $name) : ?>
<?php if (!isset($adapters[$name])) : ?>
<li>
<a href="#" onclick="javascript:auth_popup('<?php print $name ?>');">
Sign in with <?php print $name ?>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php if ($adapters) : ?>
<h1>You are logged in:</h1>
<ul>
<?php foreach ($adapters as $name => $adapter) : ?>
<li>
<strong><?php print $adapter->getUserProfile()->displayName; ?></strong> from
<i><?php print $name; ?></i>
<span>(<a href="<?php print $config['callback'] . "?logout={$name}"; ?>">Log Out</a>)</span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</body>
</html>