From ff67e487ceef3181a54b5b3ace76160a8d747657 Mon Sep 17 00:00:00 2001 From: Gregorio Chiko Putra Date: Wed, 6 Sep 2017 13:39:10 +0700 Subject: [PATCH] Added function to register new user --- App/Controllers/Home.php | 45 ++++++++++++++++++++++++++++---- App/Models/Access.php | 9 ++++--- App/Views/Access/login.html | 41 ++++++++++++----------------- App/Views/Access/registrasi.html | 30 +++++++++++++++++++++ 4 files changed, 92 insertions(+), 33 deletions(-) create mode 100644 App/Views/Access/registrasi.html diff --git a/App/Controllers/Home.php b/App/Controllers/Home.php index 6895903..e438258 100644 --- a/App/Controllers/Home.php +++ b/App/Controllers/Home.php @@ -6,6 +6,7 @@ use App\Models\Access; use Core\Token; use Core\Session; use Core\Redirect; +use Core\Hash; class Home { @@ -18,18 +19,52 @@ class Home { $posts = new Posts(); $posts->index(); - return true; } public function login() { - View::render('Access/login.html', [ - 'token' => Token::generate() - ]); - return true; + if (Session::exists('userid')) { + Redirect::to('/'); + } else { + View::render('Access/login.html', [ + 'token' => Token::generate() + ]); + } + } + + public function register() + { + if (Session::exists('userid')) { + View::render('Access/registrasi.html', [ + 'token' => Token::generate() + ]); + } else { + Redirect::to('/'); + } } // Methods + public function post($args = []) + { + $table = 'user'; + + $date = new \DateTime(); + $now = $date->format('Y-m-d'); + $args['registered_at'] = $now; + + $salt = Hash::salt(); + $password = Hash::make($args['password'], $salt); + $args['salt'] = $salt; + $args['password'] = $password; + + $args['full_name'] = htmlspecialchars($args['full_name']); + $args['username'] = htmlspecialchars($args['username']); + + $this->access->entry($table, $args); + + // Redirect::to('/'); + } + public function put($args = []) { if ($this->access->login($args)) { diff --git a/App/Models/Access.php b/App/Models/Access.php index 201c2e1..8423172 100644 --- a/App/Models/Access.php +++ b/App/Models/Access.php @@ -12,8 +12,9 @@ class Access extends \Core\Model 'username varchar(25) NOT NULL', 'password char(13) NOT NULL', 'salt char(23) NOT NULL', - 'name varchar(50) NOT NULL', + 'full_name varchar(50) NOT NULL', 'registered_at date NOT NULL DEFAULT CURRENT_TIMESTAMP', + 'privilage int(3) NOT NULL DEFAULT 0', 'status tinyint NOT NULL DEFAULT 0', 'PRIMARY KEY (id)' ] @@ -42,7 +43,7 @@ class Access extends \Core\Model try { $db = static::connectDB(); - $sql = "SELECT id, username, name, registered_at FROM user WHERE id = ?"; + $sql = "SELECT id, username, full_name, registered_at FROM user WHERE id = ?"; $query = $db->prepare($sql); @@ -61,7 +62,7 @@ class Access extends \Core\Model public function login($args = []) { try { - $username = $args['username']; + $username = htmlspecialchars($args['username']); $password = $args['password']; $db = static::connectDB(); @@ -84,7 +85,7 @@ class Access extends \Core\Model \Core\Session::put('userid', $user['id']); \Core\Session::put('username', $user['username']); - \Core\Session::put('name', $user['name']); + \Core\Session::put('name', $user['full_name']); $user_now = $user['username']; diff --git a/App/Views/Access/login.html b/App/Views/Access/login.html index 9107b67..e3767cf 100644 --- a/App/Views/Access/login.html +++ b/App/Views/Access/login.html @@ -1,32 +1,25 @@ - - - - - Login - - -
-

Login

+{% extends "base.html" %} - - +{% block title %}Login{% endblock %} -
+{% block body %} + +

Login

- - + + - - +
- - + + -
+ - -
+ - -> Alternative route - - +
+ + + +{% endblock %} diff --git a/App/Views/Access/registrasi.html b/App/Views/Access/registrasi.html new file mode 100644 index 0000000..be7dce5 --- /dev/null +++ b/App/Views/Access/registrasi.html @@ -0,0 +1,30 @@ +{% extends "base.html" %} + +{% block title %}Registrasi{% endblock %} + +{% block body %} +
+

Registrasi

+ + + + +
+ + + + +
+ + + + + + + + +
+ + +
+{% endblock %}