-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
164 lines (127 loc) · 5.54 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
<?php
require('./templates/header.php');
//redirect logged in users to home
if (isset($_SESSION['user']))
{
header('location: index.php');
}
// init vars
$email = $password = $out_err = "";
// redirect users coming from pages that require authentication
$flow = $_GET ? ($_GET['flow'] ? $_GET['flow'] : 'index.php') : 'index.php';
// get from form submitions
if (isset($_POST['flow']))
{
switch ($_POST['flow']) {
// from create quiz page
case 'create':
$flow = 'create.php';
break;
// from posts
case 'create.php':
$flow = 'create.php';
break;
// default redirect to home
default:
$flow = 'index.php';
break;
}
}
// on form submit
if (isset($_POST['submit']))
{
$email = mysqli_real_escape_string($conn, $_POST['email']);
$sql = "SELECT username, password, is_admin FROM users WHERE email='$email'";
$result = mysqli_query($conn, $sql);
if (!$result->num_rows)
{
// no row found for given email
$out_err = "Oops! We couldn't find your account, enter a registered email or <a href='signup.php'>sign up</a>";
}
else
{
$arr = mysqli_fetch_all($result, MYSQLI_ASSOC)[0];
if (password_verify($_POST['password'], $arr['password']))
{
// password match success redirect to respective page
$_SESSION['user'] = $arr['username'];
$_SESSION['is_admin'] = $arr['is_admin'];
header("location: $flow");
}
else
{
// password didn't match
$out_err = "Invalid password, try again";
}
}
}
?>
<div class="container bg-light py-4 my-3 rounded" ng-controller="loginFormController" ng-init="login.email='<?php echo htmlspecialchars($email); ?>'">
<div class="text-center text-primary">
<h1 class="display-3 mb-5">Sign In</h1>
</div>
<div class="row justify-content-center">
<div class="col-lg-6 row justify-content-center">
<!-- errors in form validation -->
<?php if ($out_err): ?>
<div class='col-10 rounded-pill alert alert-danger lead text-center'>
<?php echo $out_err; ?>
</div>
<?php endif; ?>
<!-- ui flow message after login -->
<?php if ($flow != "index.php" && (!isset($_POST['submit']))): ?>
<div class ='col-6 rounded-pill alert alert-alt lead text-center'>
Sign in to continue...
</div>
<?php endif; ?>
<form method="POST" name="loginForm" class="row justify-content-center" action="login.php" novalidate>
<div class="mb-4 row justify-content-center">
<div class="input-group">
<!-- tooltip -->
<span class="input-group-text col-auto">
<span class="tt" data-bs-toggle="tooltip" data-bs-placement="left" title="Enter the email you signed up with">
<i class="bi bi-envelope-fill"></i>
</span>
</span>
<div class="form-floating col-10">
<input class="form-control rounded-0 rounded-end" ng-model="login.email" type="text" name="email" id="email" placeholder="name@example.com"
ng-required="true">
<label for="email" class="form-label">Email</label>
</div>
</div>
</div>
<div class="mb-4 row justify-content-center">
<div class="input-group">
<!-- tooltip -->
<span class="input-group-text col-auto">
<span class="tt" data-bs-toggle="tooltip" data-bs-placement="left" title="Enter your password">
<i class="bi bi-key-fill"></i>
</span>
</span>
<div class="form-floating col-10">
<input class="form-control rounded-0 rounded-end" type="password" name="password" id="password" placeholder="Password"
ng-required="true" ng-model="login.password">
<label for="password" class="form-label">Password</label>
</div>
</div>
</div>
<!-- attach flow to redirect -->
<input type="hidden" name="flow" value="<?php echo $flow; ?>">
<div class="mb-4 text-center">
<input class="btn btn-primary" type="submit" value="Login" name="submit"
ng-disabled="loginForm.$invalid">
</div>
</form>
<div class="mt-3 text-muted">
<small>Don't have an account yet? <span><a href="signup.php">Sign Up</a></span></small>
</div>
</div>
</div>
</div>
<script>
// loginForm controller
catglueQuiz.controller('loginFormController', $scope => {});
</script>
<?php
require('./templates/footer.php');
?>