-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
192 lines (167 loc) · 7.06 KB
/
login.php
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
include 'functions.php';
// Connect to MySQL using the below function
$pdo = pdo_connect_mysql();
// Error output variable
$login_errors = '';
// User authentication
if (isset($_POST['login'], $_POST['email'], $_POST['password'])) {
// Retrieve the account from the database
$stmt = $pdo->prepare('SELECT * FROM accounts WHERE email = ?');
$stmt->execute([ $_POST['email'] ]);
$account = $stmt->fetch(PDO::FETCH_ASSOC);
// Check if account exists and the password is correct
if ($account && password_verify($_POST['password'], $account['password'])) {
// Declare the session variables. Session variable we'll use to determine whether a user is logged-in
$_SESSION['account_loggedin'] = TRUE;
$_SESSION['account_id'] = $account['id'];
$_SESSION['account_role'] = $account['role'];
$_SESSION['account_email'] = $account['email'];
$_SESSION['account_name'] = $account['full_name'];
// Chat system
$_SESSION['chat_widget_account_loggedin'] = TRUE;
$_SESSION['chat_widget_account_id'] = $account['id'];
$_SESSION['chat_widget_account_role'] = $account['role'];
update_info($pdo, $account['id'], $account['email'], $account['secret']);
// Redirect to the tickets page
header('Location: tickets.php');
exit;
} else {
$login_errors = 'Incorrect email and/or password!';
}
}
// Error output variable
$register_errors = [];
// User registration
if (isset($_POST['register'], $_POST['name'], $_POST['password'], $_POST['cpassword'], $_POST['email'])) {
// Make sure the submitted registration values are not empty.
if (empty($_POST['name']) || empty($_POST['password']) || empty($_POST['cpassword']) || empty($_POST['email'])) {
// One or more values are empty.
$register_errors[] = 'Please complete the registration form!';
}
// Check to see if the email is valid.
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$register_errors[] = 'Email is not valid!';
}
// Name must contain only characters and numbers.
if (!preg_match('/^[a-zA-Z0-9 ]+$/', $_POST['name'])) {
$register_errors[] = 'Name is not valid!';
}
// Name must be between 3 and 20 characters long.
if (strlen($_POST['name']) > 20 || strlen($_POST['name']) < 3) {
$register_errors[] = 'Name must be between 3 and 20 characters long!';
}
// Password must be between 5 and 20 characters long.
if (strlen($_POST['password']) > 20 || strlen($_POST['password']) < 5) {
$register_errors[] = 'Password must be between 5 and 20 characters long!';
}
// Check if the password and confirm password match.
if ($_POST['password'] != $_POST['cpassword']) {
$register_errors[] = 'Passwords do not match!';
}
// IF there are no errors...
if (!$register_errors) {
// Check if the account with that email already exist
$stmt = $pdo->prepare('SELECT id, password FROM accounts WHERE email = ?');
$stmt->execute([ $_POST['email'] ]);
$account = $stmt->fetch(PDO::FETCH_ASSOC);
// Check if the account exists in the database
if ($account) {
// Email already exist
$register_errors[] = 'Email already exists! Please login instead!';
} else {
// We do not want to expose passwords in our database and therefore we shall hash the password
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
// Email doesn't exist, insert new account...
$stmt = $pdo->prepare('INSERT INTO accounts (full_name, password, email) VALUES (?, ?, ?)');
$stmt->execute([ $_POST['name'], $password, $_POST['email'] ]);
// Automatically authenticate the user
$id = $pdo->lastInsertId();
$_SESSION['account_loggedin'] = TRUE;
$_SESSION['account_id'] = $id;
$_SESSION['account_role'] = 'Member';
$_SESSION['account_email'] = $_POST['email'];
$_SESSION['account_name'] = $_POST['name'];
// Chat system
$_SESSION['chat_widget_account_loggedin'] = TRUE;
$_SESSION['chat_widget_account_id'] = $id;
$_SESSION['chat_widget_account_role'] = 'Member';
// Update secret code
update_info($pdo, $id, $_POST['email']);
// Redirect to the tickets page
header('Location: tickets.php');
exit;
}
}
}
?>
<?=template_header('Login')?>
<style>
.gl-btn {
display: flex;
text-decoration: none;
position: relative;
border-radius: 4px;
text-align: center;
align-items: center;
justify-content: center;
color: #fff;
font-weight: 500;
font-size: 14px;
width: 92%;
margin: 15px 0 5px 0;
padding: 15px;
transition: background-color 0.2s;
}
.gl-btn i {
position: absolute;
left: 15px;
}
.gl-btn:hover {
color: #fff;
transition: background-color 0.2s;
}
.gl-btn {
background-color: #d6523e;
}
.gl-btn:hover {
background-color: #cf412c;
}
</style>
<div class="content login">
<div class="con">
<h2>Login</h2>
<form action="" method="post">
<label for="email">Email</label>
<input id="email" type="email" name="email" placeholder="Email" required>
<label for="password">Password</label>
<input id="password" type="password" name="password" placeholder="Password" required>
<?php if ($login_errors): ?>
<p class="error-msg"><?=$login_errors?></p>
<?php endif; ?>
<button type="submit" name="login" class="btn">Login</button>
<a href="google-oauth.php" class="gl-btn"><i class="fa-brands fa-google"></i>Login with Google</a>
</form>
</div>
<div class="con">
<h2>Register</h2>
<form action="" method="post" autocomplete="off">
<label for="name">Name</label>
<input id="name" type="text" name="name" placeholder="Name" required>
<label for="rpassword">Password</label>
<input id="rpassword" type="password" name="password" placeholder="Password" autocomplete="new-password" required>
<label for="cpassword">Confirm Password</label>
<input id="cpassword" type="password" name="cpassword" placeholder="Confirm Password" required>
<label for="remail">Email</label>
<input id="remail" type="email" name="email" placeholder="Email" required>
<?php if ($register_errors): ?>
<p class="error-msg"><?=implode('<br>', $register_errors)?></p>
<?php endif; ?>
<button type="submit" name="register" class="btn">Register</button>
<a href="google-oauth.php" class="gl-btn"><i class="fa-brands fa-google"></i>Register with Google</a>
</form>
</div>
</div>