Added function to register new user
This commit is contained in:
parent
1ee4ca3730
commit
ff67e487ce
@ -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()
|
||||
{
|
||||
if (Session::exists('userid')) {
|
||||
Redirect::to('/');
|
||||
} else {
|
||||
View::render('Access/login.html', [
|
||||
'token' => Token::generate()
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
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)) {
|
||||
|
@ -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'];
|
||||
|
||||
|
@ -1,10 +1,8 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Login</title>
|
||||
</head>
|
||||
<body>
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Login{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<form method="post">
|
||||
<h2>Login</h2>
|
||||
|
||||
@ -16,17 +14,12 @@
|
||||
<label for="password">Password: </label>
|
||||
<input type="password" name="password" value="" placeholder="Type your password">
|
||||
|
||||
<!-- method -->
|
||||
<input type="hidden" name="_method" value="put">
|
||||
|
||||
<!-- Token -->
|
||||
<input type="hidden" name="_token" value="{{ token }}">
|
||||
|
||||
<br>
|
||||
|
||||
<button type="submit" name="login">Login</button>
|
||||
</form>
|
||||
|
||||
<a href="/">-> Alternative route</a>
|
||||
</body>
|
||||
</html>
|
||||
{% endblock %}
|
||||
|
30
App/Views/Access/registrasi.html
Normal file
30
App/Views/Access/registrasi.html
Normal 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 %}
|
Loading…
Reference in New Issue
Block a user