Add Auth0, partial support
This commit is contained in:
53
src/lib/Auth.php
Normal file
53
src/lib/Auth.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace MyPrayerJournal;
|
||||
|
||||
use Auth0\SDK\Auth0;
|
||||
use Auth0\SDK\Exception\ConfigurationException;
|
||||
|
||||
class Auth
|
||||
{
|
||||
private static ?Auth0 $auth0 = null;
|
||||
|
||||
public static function client(): Auth0
|
||||
{
|
||||
if (is_null(self::$auth0)) {
|
||||
self::$auth0 = new Auth0([
|
||||
'domain' => $_ENV['AUTH0_DOMAIN'],
|
||||
'clientId' => $_ENV['AUTH0_CLIENT_ID'],
|
||||
'clientSecret' => $_ENV['AUTH0_CLIENT_SECRET'],
|
||||
'cookieSecret' => $_ENV['AUTH0_COOKIE_SECRET']
|
||||
]);
|
||||
}
|
||||
return self::$auth0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiate a log on with Auth0
|
||||
*
|
||||
* @throws ConfigurationException If the Auth0 client is not configured correctly
|
||||
*/
|
||||
public static function logOn(): never
|
||||
{
|
||||
$params = match (true) {
|
||||
$_SERVER['PHP_SELF'] <> '/user/log-on.php' => ['redirectUri' => $_SERVER['PHP_SELF']],
|
||||
default => []
|
||||
};
|
||||
|
||||
self::client()->clear();
|
||||
header('Location: ' . self::client()->login($_ENV['AUTH0_BASE_URL'] . '/user/log-on/success', $params));
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log off from this application and Auth0
|
||||
*
|
||||
* @throws ConfigurationException If the Auth0 client is not configured correctly
|
||||
*/
|
||||
public static function logOff(): never
|
||||
{
|
||||
session_destroy();
|
||||
header('Location: ' . self::client()->logout($_ENV['AUTH0_BASE_URL']));
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user