1
0

Added redirects and middleware classes for login

This commit is contained in:
Christiaan Goossens 2017-03-26 16:07:10 +02:00
parent 40d3ec9052
commit 014adeed65
7 changed files with 88 additions and 35 deletions

View File

@ -1,18 +0,0 @@
body {
margin: 50px 0 0 0;
padding: 0;
width: 100%;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-align: center;
color: #aaa;
font-size: 18px;
}
h1 {
color: #719e40;
letter-spacing: -3px;
font-family: 'Lato', sans-serif;
font-size: 100px;
font-weight: 200;
margin-bottom: 0;
}

View File

@ -51,4 +51,9 @@ class Authorization
{ {
return $this->getUserID() !== null; return $this->getUserID() !== null;
} }
public function logout()
{
unset($_SESSION['userid']);
}
} }

View File

@ -0,0 +1,34 @@
<?php
/**
* Informatica Eindproject D4p
* 6in3, Stedelijk Gymnasium Nijmegen
* Docent: Hans de Wolf
*
* ==================
*
* Daniel Boutros,
* Christiaan Goossens,
* Jelmer Hinssen
*/
namespace Inforbank\Application\Auth;
use Inforbank\Application;
use Inforbank\Application\Helper\Redirect;
class Middleware
{
public function __invoke($request, $response, $next)
{
$container = Application::getContainer();
// Check for an existing session
if ($container->auth->isUserAuthenticated()) {
return $next($request, $response);
} else {
// Redirect to the login page
return Redirect::create($response, '/login');
}
}
}

View File

@ -0,0 +1,35 @@
<?php
/**
* Informatica Eindproject D4p
* 6in3, Stedelijk Gymnasium Nijmegen
* Docent: Hans de Wolf
*
* ==================
*
* Daniel Boutros,
* Christiaan Goossens,
* Jelmer Hinssen
*/
namespace Inforbank\Application\Helper;
class Redirect
{
public static function create($response, $path)
{
$response = $response->withStatus(302);
$response = $response->withHeader('Location', Redirect::getBasepath() . $path);
return $response;
}
private static function getBasepath()
{
if (isset($_SERVER['HTTPS'])) {
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
} else {
$protocol = 'http';
}
return $protocol . "://" . $_SERVER['HTTP_HOST'];
}
}

View File

@ -15,6 +15,7 @@
namespace Inforbank\Application; namespace Inforbank\Application;
use \Slim\App; use \Slim\App;
use Inforbank\Application\Helper\Redirect;
class Login class Login
{ {
@ -24,6 +25,7 @@ class Login
// Render index view // Render index view
return $this->renderer->render($response, 'login.phtml', $args); return $this->renderer->render($response, 'login.phtml', $args);
}); });
$app->post('/login', function ($request, $response, $args) { $app->post('/login', function ($request, $response, $args) {
// Render index view // Render index view
$post = $request->getParsedBody(); $post = $request->getParsedBody();
@ -32,15 +34,18 @@ class Login
$resp = $this->auth->login($post['rekeningnr'], $post['pascode']); $resp = $this->auth->login($post['rekeningnr'], $post['pascode']);
if ($resp) { if ($resp) {
echo "HIER EEN REDIRECT GRAAG"; return Redirect::create($response, '/');
} else { } else {
echo "ERR PASS!"; return Redirect::create($response, '/login?error');
} }
} catch (\Exception $e) { } catch (\Exception $e) {
echo "ERR REK!"; return Redirect::create($response, '/login?error');
} }
});
die(); $app->get('/logout', function ($request, $response, $args) {
$this->auth->logout();
return Redirect::create($response, '/login');
}); });
} }
} }

View File

@ -32,9 +32,9 @@ class Main
public function __construct(App $app) public function __construct(App $app)
{ {
// Add the default view routes // Add the default view routes
$app->get('/[{name}]', function ($request, $response, $args) { $app->get('/', function ($request, $response, $args) {
// Render index view // Render index view
return $this->renderer->render($response, 'index.phtml', $args); return $this->renderer->render($response, 'index.phtml', $args);
}); })->add(new Auth\Middleware());
} }
} }

View File

@ -2,18 +2,10 @@
<html> <html>
<head> <head>
<meta charset="utf-8"/> <meta charset="utf-8"/>
<title>Slim 3</title> <title>Inforbank</title>
<link href='//fonts.googleapis.com/css?family=Lato:300' rel='stylesheet' type='text/css'>
<link href='assets/css/demo.css' rel='stylesheet' type='text/css'>
</head> </head>
<body> <body>
<h1>Slim</h1> <p>Welkom klant <?= $_SESSION['userid'] ?></p>
<div>a microframework for PHP</div> <a href="/logout">Uitloggen</a>
<?php if (isset($name)) : ?>
<h2>Hello <?= htmlspecialchars($name); ?>!</h2>
<?php else: ?>
<p>Try <a href="http://www.slimframework.com">SlimFramework</a>
<?php endif; ?>
</body> </body>
</html> </html>