Added redirects and middleware classes for login
This commit is contained in:
		| @@ -51,4 +51,9 @@ class Authorization | ||||
|     { | ||||
|         return $this->getUserID() !== null; | ||||
|     } | ||||
|  | ||||
|     public function logout() | ||||
|     { | ||||
|         unset($_SESSION['userid']); | ||||
|     } | ||||
| } | ||||
|   | ||||
							
								
								
									
										34
									
								
								src/Application/Auth/Middleware.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/Application/Auth/Middleware.php
									
									
									
									
									
										Normal 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'); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										35
									
								
								src/Application/Helper/Redirect.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/Application/Helper/Redirect.php
									
									
									
									
									
										Normal 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']; | ||||
|     } | ||||
| } | ||||
| @@ -15,6 +15,7 @@ | ||||
| namespace Inforbank\Application; | ||||
|  | ||||
| use \Slim\App; | ||||
| use Inforbank\Application\Helper\Redirect; | ||||
|  | ||||
| class Login | ||||
| { | ||||
| @@ -24,6 +25,7 @@ class Login | ||||
|             // Render index view | ||||
|             return $this->renderer->render($response, 'login.phtml', $args); | ||||
|         }); | ||||
|  | ||||
|         $app->post('/login', function ($request, $response, $args) { | ||||
|             // Render index view | ||||
|             $post = $request->getParsedBody(); | ||||
| @@ -32,15 +34,18 @@ class Login | ||||
|                 $resp = $this->auth->login($post['rekeningnr'], $post['pascode']); | ||||
|  | ||||
|                 if ($resp) { | ||||
|                     echo "HIER EEN REDIRECT GRAAG"; | ||||
|                     return Redirect::create($response, '/'); | ||||
|                 } else { | ||||
|                     echo "ERR PASS!"; | ||||
|                     return Redirect::create($response, '/login?error'); | ||||
|                 } | ||||
|             } 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'); | ||||
|         }); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -32,9 +32,9 @@ class Main | ||||
|     public function __construct(App $app) | ||||
|     { | ||||
|         // Add the default view routes | ||||
|         $app->get('/[{name}]', function ($request, $response, $args) { | ||||
|         $app->get('/', function ($request, $response, $args) { | ||||
|             // Render index view | ||||
|             return $this->renderer->render($response, 'index.phtml', $args); | ||||
|         }); | ||||
|         })->add(new Auth\Middleware()); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user