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\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)) {

View File

@ -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'];

View File

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

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 %}