From 3792738ba9ba5f20c250ac6a88449f10afbc8598 Mon Sep 17 00:00:00 2001
From: Markus Birth
Date: Fri, 20 May 2016 16:30:02 +0200
Subject: [PATCH] Basic login working.
---
README.md | 8 ++++++++
index.php | 26 ++++++++++++++++++++++++--
templates/index_html.mustache | 8 ++++++++
3 files changed, 40 insertions(+), 2 deletions(-)
create mode 100644 README.md
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..1ae8e64
--- /dev/null
+++ b/README.md
@@ -0,0 +1,8 @@
+Fake Reservation System
+=======================
+
+Allows you to send mails to your Google Inbox that trigger Google Now cards.
+
+To set up a server for yourself, you have to get an API key from
+https://console.developers.google.com/apis/, download the `client_secret.json`
+and put it into the root directory.
diff --git a/index.php b/index.php
index 0d7760e..3937e55 100644
--- a/index.php
+++ b/index.php
@@ -13,10 +13,32 @@ $data = array();
$client = new Google_Client();
$client->setAuthConfigFile('client_secret.json');
-$client->setRedirectUri('https://raspi.mbirth.de/dev/FRS/');
$client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
-$data['auth_url'] = $client->createAuthUrl();
+session_start();
+
+if (isset($_GET['code']) && $_GET['code']) {
+ // OAuth2 result
+ $client->authenticate($_GET['code']);
+ $_SESSION['access_token'] = $client->getAccessToken();
+ header('Location: ' . $client->getRedirectUri());
+ exit(0);
+}
+
+if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
+ // Authenticated
+ $client->setAccessToken($_SESSION['access_token']);
+
+ $data['auth_needed'] = false;
+
+ $oauth = new Google_Service_Oauth2($client);
+ $userdata = $oauth->userinfo->get();
+ $data['userdata'] = print_r($userdata, true);
+} else {
+ // Not authenticated
+ $data['auth_needed'] = true;
+ $data['auth_url'] = $client->createAuthUrl();
+}
$tpl = $m->loadTemplate('index_html');
echo $tpl->render($data);
diff --git a/templates/index_html.mustache b/templates/index_html.mustache
index f44a943..2110c91 100644
--- a/templates/index_html.mustache
+++ b/templates/index_html.mustache
@@ -8,9 +8,17 @@
What is this?
+{{# auth_needed}}
+{{/ auth_needed}}
+
+{{# userdata}}
+
+
{{userdata}}
+
+{{/ userdata}}
Hello, world!