Permalink
Browse files

finish adding support for basic login fixes #35

  • Loading branch information...
1 parent 5893dca commit 54f686ba5a2ffe37da707df4bd1ae5d10e22e85f @skyronic committed Dec 6, 2015
View
@@ -3,4 +3,5 @@
// Note: this prevents usage in MVC frameworks, which will be supported soon
//error_reporting(E_ALL & ~(E_NOTICE | E_WARNING | E_DEPRECATED));
//ini_set('display_errors', 1);
-require __DIR__."/vendor/autoload.php";
+require __DIR__."/vendor/autoload.php";
+session_start();
View
@@ -10,13 +10,16 @@
$app->setAppName ("Admin Panel");
$login = new BasicLoginPage ();
-if ($login->hasInput ()) {
+if ($login->userTriedLogin ()) {
$username = $login->getUserName ();
$password = $login->getPassword ();
if ($username === 'admin' && $password === 'demo') {
$login->success ();
}
+ else {
+ $login->fail ("Please check your password");
+ }
}
$app->useLogin ($login);
@@ -53,5 +56,3 @@
// Render the app. This will display the HTML
$app->render ();
-
-?>
@@ -34,8 +34,10 @@ public function setStaticRoot ($staticRoot) {
}
public function useLogin ($login) {
- $this->addPage ($login);
- $this->render ();
+ if (!$login->check ()) {
+ $this->addPage ($login);
+ $this->render ();
+ }
}
/**
@@ -1,6 +1,7 @@
<?php
namespace CrudKit\Pages;
+use CrudKit\Util\FlashBag;
use CrudKit\Util\RouteGenerator;
use CrudKit\Util\TwigUtil;
use CrudKit\Util\UrlHelper;
@@ -14,30 +15,57 @@ public function __construct () {
$this->route = new RouteGenerator();
}
- public function hasInput () {
-
+ public function userTriedLogin () {
+ return $this->url->has ('username') && $this->url->has ('password');
}
public function getUserName () {
return $this->url->get ('username');
}
public function getPassword () {
- return $this->url->get ('username');
+ return $this->url->get ('password');
}
// TODO: Refactor this into a session helper
public function check () {
- return isset($_SESSION['__ck_logged_in']) && $_SESSION['__ck_logged_in'] === true;
+ return isset($_SESSION['__ck_logged_in']) && $_SESSION['__ck_logged_in'] == true;
}
public function doLogin ($username = null) {
$_SESSION['__ck_logged_in'] = true;
$_SESSION['__ck_username'] = $username;
}
+ protected $loginQueued = false;
+
+ public function queueLogin () {
+ $this->loginQueued = true;
+ }
+
+ public function success () {
+ // Don't do the login writing to the session just yet
+ // since we might be on a different page id than expected,
+ // we need to do a clean redirect
+ $this->queueLogin ();
+ }
+
+ protected $error = null;
+ public function fail ($error) {
+ $this->error = $error;
+ }
+
function render()
{
+ if ($this->loginQueued) {
+ FlashBag::add ('success', "Login Success");
+ $this->doLogin ();
+ return array (
+ 'type' => 'redirect',
+ 'url' => $this->route->root ()
+ );
+ }
+
return array (
'type' => 'template',
'template' => 'pages/login.twig',
@@ -46,7 +74,9 @@ function render()
'title' => $this->app->getAppName (),
'endpoint' => $this->route->root (),
'valueBag' => json_encode(ValueBag::getValues()),
- 'bodyclass' => 'login-page'
+ 'bodyclass' => 'login-page',
+ 'error' => $this->error,
+ 'username' => $this->url->get ('username', '')
]
);
}
@@ -29,6 +29,10 @@ public function resetGetParams ($params = array()) {
return self::$url->setQuery($params);
}
+ public function has ($key) {
+ return $this->get ($key, '__ck_undefined') !== "__ck_undefined";
+ }
+
public function get ($key, $default = null) {
$postdata = file_get_contents("php://input", 'rb');
$json_post = array();
@@ -6,22 +6,24 @@
{{ title }}
</div>
<div class="login-box-body">
+ {% if error %}
+ <div class="alert alert-error">{{ error }}</div>
+ {% endif %}
<p class="login-box-msg">Please sign in</p>
- <form action="{{ endpoint }}" method="post">
+ <form action="" method="post">
<div class="form-group has-feedback">
- <input type="text" class="form-control" placeholder="Username">
+ <input type="text" class="form-control" placeholder="Username" name="username" value="{{ username }}">
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
- <input type="password" class="form-control" placeholder="Password">
+ <input type="password" class="form-control" placeholder="Password" name="password">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-4 col-xs-offset-8">
<button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
</div>
- <!-- /.col -->
</div>
</form>
</div>

0 comments on commit 54f686b

Please sign in to comment.