Added function to register new user

This commit is contained in:
Gregorio Chiko Putra 2017-09-06 13:39:10 +07:00
parent 1ee4ca3730
commit ff67e487ce
4 changed files with 92 additions and 33 deletions

View File

@ -6,6 +6,7 @@ use App\Models\Access;
use Core\Token; use Core\Token;
use Core\Session; use Core\Session;
use Core\Redirect; use Core\Redirect;
use Core\Hash;
class Home class Home
{ {
@ -18,18 +19,52 @@ class Home
{ {
$posts = new Posts(); $posts = new Posts();
$posts->index(); $posts->index();
return true;
} }
public function login() public function login()
{ {
if (Session::exists('userid')) {
Redirect::to('/');
} else {
View::render('Access/login.html', [ View::render('Access/login.html', [
'token' => Token::generate() 'token' => Token::generate()
]); ]);
return true; }
}
public function register()
{
if (Session::exists('userid')) {
View::render('Access/registrasi.html', [
'token' => Token::generate()
]);
} else {
Redirect::to('/');
}
} }
// Methods // 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 = []) public function put($args = [])
{ {
if ($this->access->login($args)) { if ($this->access->login($args)) {

View File

@ -12,8 +12,9 @@ class Access extends \Core\Model
'username varchar(25) NOT NULL', 'username varchar(25) NOT NULL',
'password char(13) NOT NULL', 'password char(13) NOT NULL',
'salt char(23) 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', 'registered_at date NOT NULL DEFAULT CURRENT_TIMESTAMP',
'privilage int(3) NOT NULL DEFAULT 0',
'status tinyint NOT NULL DEFAULT 0', 'status tinyint NOT NULL DEFAULT 0',
'PRIMARY KEY (id)' 'PRIMARY KEY (id)'
] ]
@ -42,7 +43,7 @@ class Access extends \Core\Model
try { try {
$db = static::connectDB(); $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); $query = $db->prepare($sql);
@ -61,7 +62,7 @@ class Access extends \Core\Model
public function login($args = []) public function login($args = [])
{ {
try { try {
$username = $args['username']; $username = htmlspecialchars($args['username']);
$password = $args['password']; $password = $args['password'];
$db = static::connectDB(); $db = static::connectDB();
@ -84,7 +85,7 @@ class Access extends \Core\Model
\Core\Session::put('userid', $user['id']); \Core\Session::put('userid', $user['id']);
\Core\Session::put('username', $user['username']); \Core\Session::put('username', $user['username']);
\Core\Session::put('name', $user['name']); \Core\Session::put('name', $user['full_name']);
$user_now = $user['username']; $user_now = $user['username'];

View File

@ -1,10 +1,8 @@
<!DOCTYPE html> {% extends "base.html" %}
<html>
<head> {% block title %}Login{% endblock %}
<meta charset="utf-8">
<title>Login</title> {% block body %}
</head>
<body>
<form method="post"> <form method="post">
<h2>Login</h2> <h2>Login</h2>
@ -16,17 +14,12 @@
<label for="password">Password: </label> <label for="password">Password: </label>
<input type="password" name="password" value="" placeholder="Type your password"> <input type="password" name="password" value="" placeholder="Type your password">
<!-- method -->
<input type="hidden" name="_method" value="put"> <input type="hidden" name="_method" value="put">
<!-- Token -->
<input type="hidden" name="_token" value="{{ token }}"> <input type="hidden" name="_token" value="{{ token }}">
<br> <br>
<button type="submit" name="login">Login</button> <button type="submit" name="login">Login</button>
</form> </form>
{% endblock %}
<a href="/">-> Alternative route</a>
</body>
</html>

View File

@ -0,0 +1,30 @@
{% extends "base.html" %}
{% block title %}Registrasi{% endblock %}
{% block body %}
<form method="post">
<h2>Registrasi</h2>
<label for="full_name">Nama Lengkap: </label>
<input type="text" name="full_name" value="" placeholder="Type your name">
<br>
<label for="username">Username: </label>
<input type="text" name="username" value="" placeholder="Type your username">
<br>
<label for="password">Password: </label>
<input type="password" name="password" value="" placeholder="Type your password">
<input type="hidden" name="_method" value="post">
<input type="hidden" name="_token" value="{{ token }}">
<br>
<button type="submit">Registrasi</button>
</form>
{% endblock %}