-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
119 lines (68 loc) · 3.25 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
// Import the Composer Autoloader to make the SDK classes accessible:
require 'vendor/autoload.php';
// Load our environment variables from the .env file:
(Dotenv\Dotenv::createImmutable(__DIR__))->load();
// Now instantiate the Auth0 class with our configuration:
$auth0 = new \Auth0\SDK\Auth0([
'domain' => $_ENV['AUTH0_DOMAIN'],
'clientId' => $_ENV['AUTH0_CLIENT_ID'],
'clientSecret' => $_ENV['AUTH0_CLIENT_SECRET'],
'cookieSecret' => $_ENV['AUTH0_COOKIE_SECRET']
]);
// $auth0 = new \Auth0\SDK\Auth0([
// 'domain' => 'dev-ncn7fb170hed4xdc.us.auth0.com',
// 'clientId' => 'dkWVLuZkR2lDoRaRyPFI9uahHc9MbNBd',
// 'clientSecret' => 'PoieeJBzvvGTW3kAPjvOgjrWCysz0roetAxSxD1f6-hx_P9JvaowG2aMMNtDrlOa',
// 'cookieSecret' => '8c74b22e7c3057ab6e18d2b77a54976a7dc7c18b87dc19dcb869d5a91431a526'
// ]);
// 👆 We're continuing from the steps above. Append this to your index.php file.
// Import our router library:
use Steampixel\Route;
// Define route constants:
define('ROUTE_URL_INDEX', rtrim($_ENV['AUTH0_BASE_URL'], '/'));
define('ROUTE_URL_LOGIN', ROUTE_URL_INDEX . '/login');
define('ROUTE_URL_CALLBACK', ROUTE_URL_INDEX . '/callback');
define('ROUTE_URL_LOGOUT', ROUTE_URL_INDEX . '/logout');
// 👆 We're continuing from the steps above. Append this to your index.php file.
Route::add('/', function() use ($auth0) {
$session = $auth0->getCredentials();
if ($session === null) {
// The user isn't logged in.
include('home.php');
// echo '<p>Please <a href="/login">log in</a>.</p>';
return;
}
// The user is logged in.
// echo '<pre>';
// print_r($session->user);
// echo '</pre>';
// echo '<p>You can now <a href="/logout">log out</a>.</p>';
include('mainindex.php');
});
// 👆 We're continuing from the steps above. Append this to your index.php file.
Route::add('/login', function() use ($auth0) {
// It's a good idea to reset user sessions each time they go to login to avoid "invalid state" errors, should they hit network issues or other problems that interrupt a previous login process:
$auth0->clear();
// Finally, set up the local application session, and redirect the user to the Auth0 Universal Login Page to authenticate.
header("Location: " . $auth0->login(ROUTE_URL_CALLBACK));
exit;
});
// 👆 We're continuing from the steps above. Append this to your index.php file.
Route::add('/callback', function() use ($auth0) {
// Have the SDK complete the authentication flow:
$auth0->exchange(ROUTE_URL_CALLBACK);
// Finally, redirect our end user back to the / index route, to display their user profile:
header("Location: " . ROUTE_URL_INDEX);
exit;
});
// 👆 We're continuing from the steps above. Append this to your index.php file.
Route::add('/logout', function() use ($auth0) {
// Clear the user's local session with our app, then redirect them to the Auth0 logout endpoint to clear their Auth0 session.
header("Location: " . $auth0->logout(ROUTE_URL_INDEX));
exit;
});
// 👆 We're continuing from the steps above. Append this to your index.php file.
// This tells our router that we've finished configuring our routes, and we're ready to begin routing incoming HTTP requests:
Route::run('/');
?>