-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
145 lines (120 loc) · 3.84 KB
/
script.js
File metadata and controls
145 lines (120 loc) · 3.84 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
// Taking refrence of login page
const username_login = document.getElementById("username_login");
const passwd_login = document.getElementById("passwd_login");
const login = document.getElementById("login");
const Login_form = document.getElementById("Login_form");
const error = document.getElementById("error");
// taking reference of sign up page
const Signup_form = document.getElementById("Signup_form");
const signup = document.getElementById("signup");
const username_signup = document.getElementById("username_signup");
const passwd_signup = document.getElementById("passwd_signup");
// admin credentials
let admin = {
username: "admin",
password: "admin123",
};
// when come on login page
if (Login_form) {
Login_form.addEventListener("submit", function (event) {
event.preventDefault();
if (mode.value === "customer") {
chechforparametersofcustomer(
username_login.value,
passwd_login.value,
mode.value
);
} else {
checkforparametersofadmin(username_login.value, passwd_login.value);
}
});
}
// when we come on SignUp page
if (Signup_form) {
Signup_form.addEventListener("submit", function (event) {
event.preventDefault();
const username_signup_value = username_signup.value;
const passwd_signup_value = passwd_signup.value;
console.log(username_signup_value, passwd_signup_value);
let users = getuserfromstorage();
console.log(users);
if (users) {
let check_availability = checkForUserFromStorage(username_signup_value);
console.log(check_availability);
if (check_availability)
error.innerText = "This Username already exists!!";
else {
savetostorage(
username_signup_value,
convertToHash(passwd_signup_value)
);
error.innerText = `Successfully Signed Up!!
go to login page!`;
}
} else {
error.innerText = `Successfully Signed Up!!
go to login page!`;
savetostorage(username_signup_value, convertToHash(passwd_signup_value));
}
});
}
let users = {};
// functions
// functions of signup
function getuserfromstorage() {
let users = localStorage.getItem("user_details");
return users ? JSON.parse(users) : [];
}
function checkForUserFromStorage(username) {
let users = getuserfromstorage();
if (users[username]) return true;
else return false;
}
function savetostorage(username, password) {
users[username] = {
username,
password,
is_customer: false,
};
localStorage.setItem("user_details", JSON.stringify(users));
}
// functions of login
function chechforparametersofcustomer(username, password, account_type) {
let users = getuserfromstorage();
console.log(users);
console.log(account_type);
// console.log(convertToHash(password));
if (users[username]) {
if (
users[username].password === convertToHash(password) &&
account_type === "customer"
) {
saveUserName(username);
window.open("./customer.html", "_self");
error.innerText = `Hi, ${username} you have successfully loged in`;
} else error.innerText = "username or password is incorrect!!";
} else error.innerText = "This account does not exists!!";
}
function checkforparametersofadmin(username, password) {
if (
admin.username === username &&
convertToHash(admin.password) === convertToHash(password)
) {
console.log(convertToHash(password));
error.innerText = "Successfully loged in as Admin";
window.open("./admin.html", "_self");
saveUserName("admin");
} else error.innerText = "Wrong Admin Credentials";
}
function convertToHash(str) {
var hash = 0,
i = 0,
len = str.length;
while (i < len) {
hash = ((hash << 5) - hash + str.charCodeAt(i++)) << 0;
}
return hash;
}
function saveUserName(name) {
localStorage.setItem("user", JSON.stringify(name));
}