-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.html
126 lines (118 loc) · 4.1 KB
/
login.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="login.css" />
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
/>
<script src="https://www.gstatic.com/firebasejs/9.13.0/firebase-app.js"></script>
<!-- <script src="https://www.gstatic.com/firebasejs/9.13.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.13.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.13.0/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.13.0/firebase-database.js"></script>
-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
<!-- <script src="login.js"></script> -->
<title>login</title>
</head>
<body>
<div class="mb-5">
<h3 class="mb-3">Login</h3>
<input
type="text"
placeholder="Username"
id="user"
class="form-control mb-3"
/>
<input
type="password"
placeholder="Password"
id="password"
class="form-control mb-3"
/>
<div class="custom-control custom-switch mb-3">
<input
type="checkbox"
class="custom-control-input"
id="customSwitch1"
/>
<label class="custom-control-label" for="customSwitch1">
Keep me logged in!</label
>
</div>
<button
type="text"
id="sub_btn"
class="btn w-100 btn-outline-primary mb-3"
>
Login
</button>
<a href="register.html" class="badge badge-secondary py-1 w-100"
>Want to create a new account?</a
>
</div>
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.13.0/firebase-app.js";
const firebaseConfig = {
apiKey: "AIzaSyBY3lgxaGHp2nL7eq6H8IESdcsLFk6FpN0",
authDomain: "foodico-93201.firebaseapp.com",
projectId: "foodico-93201",
storageBucket: "foodico-93201.appspot.com",
messagingSenderId: "649175840578",
appId: "1:649175840578:web:4840358779ec0a45143adb",
measurementId: "G-JTP8GTWE08",
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// const analytics = getAnalytics(app);
import {
getDatabase,
ref,
set,
child,
get,
} from "https://www.gstatic.com/firebasejs/9.13.0/firebase-database.js";
const db = getDatabase();
const user = document.getElementById("user");
const password = document.getElementById("password");
const submit = document.getElementById("sub_btn");
function AuthenticateUser() {
const dbRef = ref(db);
get(child(dbRef, "UsersList/" + user.value)).then((snapshot) => {
if (snapshot.exists()) {
alert("Login successful");
let dbpass = decPass(snapshot.val().password);
if (dbpass == password.value) {
login(snapshot.val());
} else {
alert("user does not exist!");
}
} else {
alert("username or password is invalid");
}
});
}
function decPass(dbpass) {
var pass12 = CryptoJS.AES.decrypt(dbpass, password.value);
return pass12.toString(CryptoJS.enc.Utf8);
// console.log(pass12.toString());
}
function login(user) {
let keepLoggedIn = document.getElementById("customSwitch1").checked;
if (!keepLoggedIn) {
sessionStorage.setItem("user", JSON.stringify(user));
window.location = "userprof.html";
} else {
localStorage.setItem("keepLoggedIn", "yes");
localStorage.setItem("user", JSON.stringify(user));
window.location = "userprof.html";
}
}
submit.addEventListener("click", AuthenticateUser);
</script>
</body>
</html>