Commit 3bca7331 authored by Taddeus Kroes's avatar Taddeus Kroes

Added auto-login option that lasts for 2 weeks

parent 10e32f5c
...@@ -89,6 +89,9 @@ $app->hook('slim.before.router', function () use ($app, $user) { ...@@ -89,6 +89,9 @@ $app->hook('slim.before.router', function () use ($app, $user) {
$app->redirect(ROOT_URL . '/login'); $app->redirect(ROOT_URL . '/login');
}); });
// Extend maximum session lifetime to allow auto-login
$session->setExpiration($config['max_session_lifetime']);
/* /*
* Routes * Routes
*/ */
...@@ -98,5 +101,6 @@ require 'routes/register.php'; ...@@ -98,5 +101,6 @@ require 'routes/register.php';
require 'routes/user.php'; require 'routes/user.php';
require 'routes/match.php'; require 'routes/match.php';
// Certain globals should be available in all templates
$view->replace(compact('app', 'config', 'user')); $view->replace(compact('app', 'config', 'user'));
$app->run(); $app->run();
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
"database": { "database": {
"lazy": true "lazy": true
}, },
"max_session_lifetime": "2 weeks",
"log.enable": true, "log.enable": true,
"debug": false "debug": false
} }
...@@ -16,7 +16,7 @@ function login_form() { ...@@ -16,7 +16,7 @@ function login_form() {
->setRequired(); ->setRequired();
$form->addPassword('password', _('Password')) $form->addPassword('password', _('Password'))
->setRequired(); ->setRequired();
//$form->addCheckbox('remember', _('Remember me')); $form->addCheckbox('remember', _('Remember me'));
$form->addSubmit('send', _('Login')); $form->addSubmit('send', _('Login'));
return $form; return $form;
...@@ -29,7 +29,7 @@ $app->get('/login', function () use ($app, $user) { ...@@ -29,7 +29,7 @@ $app->get('/login', function () use ($app, $user) {
$app->render('login', array('form' => login_form())); $app->render('login', array('form' => login_form()));
}); });
$app->post('/login', function () use ($app, $user) { $app->post('/login', function () use ($config, $app, $user, $session) {
$form = login_form(); $form = login_form();
$form->validate(); $form->validate();
...@@ -38,6 +38,12 @@ $app->post('/login', function () use ($app, $user) { ...@@ -38,6 +38,12 @@ $app->post('/login', function () use ($app, $user) {
try { try {
$user->login($values->username, $values->password); $user->login($values->username, $values->password);
if ($values->remember)
$user->setExpiration($config['max_session_lifetime'], false);
else
$user->setExpiration(0, true);
$app->redirect(ROOT_URL); $app->redirect(ROOT_URL);
} catch (Nette\Security\AuthenticationException $e) { } catch (Nette\Security\AuthenticationException $e) {
$form->addError($e->getMessage()); $form->addError($e->getMessage());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment