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\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()
|
||||||
{
|
{
|
||||||
View::render('Access/login.html', [
|
if (Session::exists('userid')) {
|
||||||
'token' => Token::generate()
|
Redirect::to('/');
|
||||||
]);
|
} else {
|
||||||
return true;
|
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
|
// 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)) {
|
||||||
|
@ -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'];
|
||||||
|
|
||||||
|
@ -1,32 +1,25 @@
|
|||||||
<!DOCTYPE html>
|
{% extends "base.html" %}
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<title>Login</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<form method="post">
|
|
||||||
<h2>Login</h2>
|
|
||||||
|
|
||||||
<label for="username">Username: </label>
|
{% block title %}Login{% endblock %}
|
||||||
<input type="text" name="username" value="" placeholder="Type your username">
|
|
||||||
|
|
||||||
<br>
|
{% block body %}
|
||||||
|
<form method="post">
|
||||||
|
<h2>Login</h2>
|
||||||
|
|
||||||
<label for="password">Password: </label>
|
<label for="username">Username: </label>
|
||||||
<input type="password" name="password" value="" placeholder="Type your password">
|
<input type="text" name="username" value="" placeholder="Type your username">
|
||||||
|
|
||||||
<!-- method -->
|
<br>
|
||||||
<input type="hidden" name="_method" value="put">
|
|
||||||
|
|
||||||
<!-- Token -->
|
<label for="password">Password: </label>
|
||||||
<input type="hidden" name="_token" value="{{ token }}">
|
<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>
|
<input type="hidden" name="_token" value="{{ token }}">
|
||||||
</form>
|
|
||||||
|
|
||||||
<a href="/">-> Alternative route</a>
|
<br>
|
||||||
</body>
|
|
||||||
</html>
|
<button type="submit" name="login">Login</button>
|
||||||
|
</form>
|
||||||
|
{% 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