Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

much better login/out handling

  • Loading branch information...
commit 914db6416f3d1cb05bd95a55933b1e85a03d5620 1 parent 385d0cd
@BigBlueHat BigBlueHat authored
View
21 index.php
@@ -11,6 +11,7 @@
// set up the app
MustacheView::$mustacheDirectory = 'vendors';
$app = new Slim(array('view' => 'MustacheView'));
+$app->add(new Slim_Middleware_SessionCookie());
$env = $app->environment();
$app->view()->appendData(array(
'app_title' => 'Beernique',
@@ -18,8 +19,6 @@
'current_url' => $env['PATH_INFO']
));
-$app->add(new Slim_Middleware_SessionCookie());
-
// http://www.php.net/manual/en/function.max.php#97004
function max_key($array) {
foreach ($array as $key => $val) {
@@ -34,12 +33,18 @@ function max_key($array) {
die($e->getMessage());
}
+$app->hook('slim.before', function () use ($app) {
+ $app->view()->appendData(array('user' =>
+ (isset($_SESSION['user'])
+ ? $_SESSION['user']
+ : false)));
+});
// GET route
$app->get('/', function () use ($app, $cb) {
$on_index = true;
- if (isset($_SESSION['email']) && $_SESSION['email'] !== ''
- && ($users_beers = $cb->get(sha1($_SESSION['email']))) !== null) {
+ if (isset($_SESSION['user']) && $_SESSION['user'] !== ''
+ && ($users_beers = $cb->get(sha1($_SESSION['user']))) !== null) {
$users_beers = array_filter(explode('|', $users_beers));
$users_beer_counts = array_count_values($users_beers);
if (count($users_beers) > 0) {
@@ -100,7 +105,7 @@ function max_key($array) {
// This logs the user in if we have an account for that email address,
// or creates it otherwise
//$email = sha1($r['body']['email']);
- $email = $_SESSION['email'] = $r['body']->email;
+ $email = $_SESSION['user'] = $r['body']->email;
if ($cb->get(sha1($email)) === null) {
$cb->set(sha1($email), '');
}
@@ -113,13 +118,13 @@ function max_key($array) {
});
$app->post('/browserid/logout', function() use ($app) {
- $_SESSION['email'] = null;
+ $_SESSION['user'] = null;
});
$app->get('/browserid/whoami', function() use ($app) {
$app->response()->header('Content-Type', 'application/json');
- if (isset($_SESSION['email'])) {
- echo json_encode($_SESSION['email']);
+ if (isset($_SESSION['user'])) {
+ echo json_encode($_SESSION['user']);
}
});
View
6 routes/beers.php
@@ -21,7 +21,7 @@ function breweryUrl($name) {
});
$app->get('/beers/:id', function($id) use ($app, $cb) {
- if (!isset($_SESSION['email'])) {
+ if (!isset($_SESSION['user'])) {
$app->halt(401);
} else {
$beer_id = 'beer_' . str_replace(' ', '_', urldecode($id));
@@ -45,7 +45,7 @@ function breweryUrl($name) {
// POST route for "drinking"
$app->post('/beers/', function () use ($app, $cb) {
// TODO: add better login required handler thing
- if (!isset($_SESSION['email'])) {
+ if (!isset($_SESSION['user'])) {
$app->halt(401);
}
$id = $app->request()->params('id');
@@ -62,7 +62,7 @@ function breweryUrl($name) {
404);
exit;
}
- $email = sha1($_SESSION['email']);
+ $email = sha1($_SESSION['user']);
if ($cb->get($email) !== null) {
$cb->append($email, '|' . $beer_id);
} else {
View
5 templates/index.mustache
@@ -1,7 +1,10 @@
<header class="hero-unit">
<h1>{{app_title}}</h1>
<h3>Code Responsibly</h3>
- <form id="get-beer" class="form-search" action="beers/" method="POST">
+ <p class="when-logged-out"><a href="#" class="login" title="Sign-in with BrowserID"><img src="{{base_url}}/assets/ico/sign_in_blue.png" alt="Sign in"></a>
+ to start adding beers
+ </p>
+ <form id="get-beer" class="when-logged-in form-search" action="beers/" method="POST">
<input type="search" name="id" placeholder="Whatcha drinkin'?"/>
<button class="btn" type="submit">Drink</button>
</form>
View
31 templates/layout.mustache
@@ -13,6 +13,12 @@
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
+ {{^user}}
+ .when-logged-in {display:none}
+ {{/user}}
+ {{#user}}
+ .when-logged-out {display:none}
+ {{/user}}
</style>
<link href="{{base_url}}/assets/css/bootstrap-responsive.css" rel="stylesheet">
@@ -36,17 +42,16 @@
<div class="container">
<a class="brand" href="{{base_url}}/">{{app_title}}</a>
{{^on_index}}
+ {{#user}}
<form action="{{base_url}}/beers/" method="POST" class="navbar-search pull-left">
<input type="search" placeholder="Whatcha drinkin'?" class="search-query span2" name="id">
</form>
+ {{/user}}
{{/on_index}}
<ul class="nav pull-right">
<li>
- <a href="#" id="login" title="Sign-in with BrowserID">
- <img src="{{base_url}}/assets/ico/sign_in_blue.png" alt="Sign in">
- </a>
- <a href="#" id="logout" style="display:none">
- Hi yeah, <span id="whoami">??</span>
+ <a href="#" id="logout" class="when-logged-in">
+ Hi yeah, <span id="whoami">{{user}}</span>
Wanna logout?
</a>
</li>
@@ -59,7 +64,7 @@
{{{content}}}
{{#on_index}}
{{#has_beers}}
- <div class="row">
+ <div class="row when-logged-in">
<div class="span4">
<h3>You've been drinking...</h3>
<ul>
@@ -103,14 +108,16 @@
<script src="https://browserid.org/include.js" type="text/javascript"></script>
<script>
function loggedIn(res) {
+ console.log('loggedin');
$('#whoami').html(res);
- $('#login').hide();
- $('#logout').show();
- }
+ $('.when-logged-out').hide();
+ $('.when-logged-in').show();
+ }
function loggedOut() {
- $('#login').show();
- $('#logout').hide();
+ console.log('loggedout');
+ $('.when-logged-out').show();
+ $('.when-logged-in').hide();
}
function gotVerifiedEmail(assertion) {
@@ -148,7 +155,7 @@
}
}, 'json');
- $('#login').on('click', function() {
+ $('.login').on('click', function() {
navigator.id.get(gotVerifiedEmail, {allowPersistent: true});
return false;
});
Please sign in to comment.
Something went wrong with that request. Please try again.