Repository navigation
Expand file tree
/
Copy pathauth_process.php
More file actions
145 lines (127 loc) · 3.9 KB
/
Copy pathauth_process.php
File metadata and controls
145 lines (127 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
include './db_connection.php';
include './base.php';
if(isset($_POST['register'])) {
$email = $_POST["email"];
$password = $_POST["password"];
// validasi masukan
try {
validate($email, "Email", ["notblank" => true]);
validate($password, "Katasandi", ["notblank" => true, "min" => 8]);
} catch(ValidationException $e) {
echo '<script>
alert(" ' . $e->getMessage() . ' ");
window.location = "register.php";
</script>';
return;
}
try {
$statement = $conn->prepare("SELECT email, password FROM tb_pengguna WHERE email = ?");
$statement->bind_param("s", $email);
$statement->execute();
$result = $statement->get_result();
$data = $result->fetch_assoc();
if ($data) {
echo "
<script>
alert('Email sudah terdaftar');
window.location = 'register.php';
</script>
";
return;
}
} catch(Exception $e) {
echo "
<script>
alert('Terjadi kesalahan');
window.location = 'register.php';
</script>
";
}
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$uuid = uuid4();
try {
$statement = $conn->prepare("INSERT INTO tb_pengguna (id, email, password) VALUES (?, ?, ?)");
$statement->bind_param("sss", $uuid, $email, $hashed_password);
$statement->execute();
echo "
<script>
alert('Registrasi berhasil, silahkan Login');
window.location = 'login.php';
</script>
";
} catch (Excpetion $e) {
echo "
<script>
alert('Terjadi kesalahan');
window.location = 'register.php';
</script>
";
}
return;
}
if (isset($_POST["login"])) {
$email = $_POST["email"];
$password = $_POST["password"];
// validasi masukan
try {
validate($email, "Email", ["notblank" => true]);
validate($password, "Katasandi", ["notblank" => true]);
} catch(ValidationException $e) {
echo '<script>
alert(" ' . $e->getMessage() . ' ");
window.location = "login.php";
</script>';
return;
}
try {
$statement = $conn->prepare("SELECT id, email, password FROM tb_pengguna WHERE email = ?");
$statement->bind_param("s", $email);
$statement->execute();
$result = $statement->get_result();
$data = $result->fetch_assoc();
if (!$data) {
echo "
<script>
alert('Pengguna dengan ' .$email . ' tidak di temukan');
window.location = 'login.php';
</script>
";
return;
}
$isPassowordSame = password_verify($password, $data["password"]);
if ($isPassowordSame) {
echo "
<script>
alert('Login berhasil');
window.location = 'index.php';
</script>
";
session_start();
$_SESSION["email"] = $email;
$_SESSION["userId"] = $data["id"];
header("location:dashboard/index.php");
} else {
echo "
<script>
alert('Login gagal katasandi salah');
window.location = 'login.php';
</script>
";
}
} catch (Excpetion $e) {
echo "
<script>
alert('Terjadi kesalahan');
window.location = 'login.php';
</script>
";
}
return;
}
if (isset($_POST["logout"])) {
session_start();
session_destroy();
header("location:index.php");
}
?>