-
Notifications
You must be signed in to change notification settings - Fork 1
/
callback.php
37 lines (27 loc) · 1011 Bytes
/
callback.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
<?php
#error_reporting(E_ALL);
#ini_set('display_errors', 1);
session_start();
$provider = require 'providerConfig.php';
try {
if (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
throw new Exception('Invalid state');
}
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
$resourceOwner = $provider->getResourceOwner($token);
$userData = $resourceOwner->toArray();
// Save user data and token in session or database here
$_SESSION['user'] = $userData["name"];
$_SESSION['token'] = $token->getToken();
// Redirect to index.php
header('Location: index.php');
exit();
} catch (Exception $e) {
// Log the error message and display a generic error message to the user
error_log($e->getMessage());
echo '<p>An error occurred during the login process. Please try again.</p>';
echo '<p><a href="index.php">Try again</a></p>';
}