Login, logout a HP.

This commit is contained in:
severak 2020-01-27 16:39:57 +01:00
parent 43ce858dc4
commit 0767a5b15b
3 changed files with 56 additions and 5 deletions

47
app.php
View File

@ -1,5 +1,7 @@
<?php
// DEPENDENCIES
use severak\forms\form;
$dependencies['config'] = $config;
$singletons['pdo'] = function() {
$config = di('config');
@ -11,11 +13,52 @@ $singletons['rows'] = function(){
// ROUTY
// HP & LOGIN
route('', '/', function (){
throw new Exception('Not yet implemented.');
if (!user()) return redirect('/login/');
return render('home');
});
// správa stálých položek na baru
route('', '/login/', function ($req){
/** @var Psr\Http\Message\ServerRequestInterface $req */
/** @var severak\database\rows $rows */
$rows = di('rows');
$form = new form(['method'=>'POST']);
$form->field('username', ['required'=>true, 'label'=>'Jméno']);
$form->field('password', ['type'=>'password', 'required'=>true, 'label'=>'Heslo']);
$form->field('_login', ['type'=>'submit', 'label'=>'Přihlásit se']);
if ($req->getMethod()=='POST') {
$form->fill($req->getParsedBody());
if ($form->validate()) {
$uz = $rows->one('users', ['username'=>$form->values['username']]);
if (!$uz) {
$form->error('username', 'Uživatel nenalezen');
} elseif (password_verify($form->values['password'], $uz['password'])) {
unset($uz['password']);
$_SESSION['user'] = $uz;
return redirect('/');
} else {
$form->error('password', 'Špatné heslo.');
}
}
}
return render('form', ['form'=>$form]);
});
route('', '/logout/', function ($req){
unset($_SESSION['user']);
unset($_SESSION['flashes']);
return redirect('/');
});
route('', '/heslo/', function (){
return '123 - ' . password_hash('123', PASSWORD_BCRYPT);
});
// NABÍDKA
route('GET', '/items', function ($req){
/** @var severak\database\rows $rows */
$rows = di('rows');

View File

@ -22,8 +22,12 @@ Debugger::$errorTemplate = __DIR__ . '/tpl/500.htm';
$routeCollector = new FastRoute\RouteCollector(new FastRoute\RouteParser\Std(), new FastRoute\DataGenerator\GroupCountBased());
// user
// requireLogin
function user()
{
return $_SESSION['user'] ?? false;
}
// TODO - requireLogin
$dependencies = $singletons = [];
@ -42,7 +46,7 @@ function di($service)
function flash($msg)
{
// stub
$_SESSION['flashes'][] = $msg;
}
function redirect($url, $status = 302)
@ -94,6 +98,8 @@ require 'app.php';
// finally running the APP
session_start(); // TODO - nakonfit session
$routeDispatcher = new FastRoute\Dispatcher\GroupCountBased($routeCollector->getData());
$request = new Nyholm\Psr7\ServerRequest($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], getallheaders());
if ($request->getMethod()=='POST') {

2
tpl/home.php Normal file
View File

@ -0,0 +1,2 @@
<?= render('_header'); ?>
<?= render('_footer'); ?>