Split templates for logged out and logged in state. Added Logout option.

Added rewriting to allow cleaner URLs.
This commit is contained in:
2016-05-20 19:06:02 +02:00
parent 3792738ba9
commit a583351ac4
6 changed files with 69 additions and 4 deletions
+23 -2
View File
@@ -17,8 +17,22 @@ $client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
session_start();
if (isset($_GET['action']) && $_GET['action'] == 'logout') {
// Delete session and redirect to self
#$client->setAccessToken($_SESSION['access_token']);
#$client->revokeToken(); // removed granted permissions from account
$_SESSION = array();
if (ini_get('session.use_cookies')) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time()-42000, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
}
session_destroy();
header('Location: ' . $client->getRedirectUri());
exit(0);
}
if (isset($_GET['code']) && $_GET['code']) {
// OAuth2 result
// Validate OAuth2 result, set access token and redirect to self
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: ' . $client->getRedirectUri());
@@ -33,12 +47,19 @@ if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$oauth = new Google_Service_Oauth2($client);
$userdata = $oauth->userinfo->get();
$data['user']['name_first'] = $userdata->givenName;
$data['userdata'] = print_r($userdata, true);
// TODO: Check $userdata->verifiedEmail and deny if not verified.
$tpl = $m->loadTemplate('loggedin_html');
} else {
// Not authenticated
$data['auth_needed'] = true;
$data['auth_url'] = $client->createAuthUrl();
$tpl = $m->loadTemplate('index_html');
}
$tpl = $m->loadTemplate('index_html');
$data['action'] = $_GET['action'];
echo $tpl->render($data);