-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
138 lines (125 loc) · 3.43 KB
/
index.js
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
//Select DOM Elements
const userNameField = document.querySelector(".username");
const passwordField = document.querySelector(".password");
const errorMessage = document.querySelector(".error");
const getloggedInUser = () => {
const loggedInUser = localStorage.getItem("loggedInUser");
if (loggedInUser) {
window.location.href = "/layout.html";
}
};
getloggedInUser();
const account1 = {
owner: "Benjamin Thompson",
transactions: [200, 455.23, -306.5, 25000, -642.21, -133.9, 79.97, 1300],
interestRate: 1.2, // %
pin: 1111,
movementsDates: [
"2019-11-18T21:31:17.178Z",
"2019-12-23T07:42:02.383Z",
"2020-01-28T09:15:04.904Z",
"2020-04-01T10:17:24.185Z",
"2020-05-08T14:11:59.604Z",
"2023-05-27T17:01:17.194Z",
"2023-07-11T23:36:17.929Z",
"2023-07-12T10:51:36.790Z",
],
currency: "EUR",
locale: "pt-PT", // de-DE
};
const account2 = {
owner: "Emma Johnson",
transactions: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
interestRate: 1.5,
pin: 2222,
movementsDates: [
"2019-11-01T13:15:33.035Z",
"2019-11-30T09:48:16.867Z",
"2019-12-25T06:04:23.907Z",
"2020-01-25T14:18:46.235Z",
"2020-02-05T16:33:06.386Z",
"2020-04-10T14:43:26.374Z",
"2020-06-25T18:49:59.371Z",
"2020-07-26T12:01:20.894Z",
],
currency: "USD",
locale: "en-US",
};
const account3 = {
owner: "Steven Thomas Williams",
transactions: [200, -200, 340, -300, -20, 50, 400, -460],
interestRate: 0.7,
pin: 3333,
movementsDates: [
"2019-11-01T13:15:33.035Z",
"2019-11-30T09:48:16.867Z",
"2019-12-25T06:04:23.907Z",
"2020-01-25T14:18:46.235Z",
"2020-02-05T16:33:06.386Z",
"2020-04-10T14:43:26.374Z",
"2020-06-25T18:49:59.371Z",
"2020-07-26T12:01:20.894Z",
],
currency: "USD",
locale: "en-US",
};
const account4 = {
owner: "Sarah Smith",
transactions: [430, 1000, 700, 50, 90],
interestRate: 1,
pin: 4444,
movementsDates: [
"2019-11-01T13:15:33.035Z",
"2019-11-30T09:48:16.867Z",
"2019-12-25T06:04:23.907Z",
"2020-01-25T14:18:46.235Z",
"2020-02-05T16:33:06.386Z",
"2020-04-10T14:43:26.374Z",
"2020-06-25T18:49:59.371Z",
"2020-07-26T12:01:20.894Z",
],
currency: "USD",
locale: "en-US",
};
const accounts = JSON.parse(localStorage.getItem("accounts")) || [
account1,
account2,
account3,
account4,
];
/* GET USERNAMES */
const computeUserNames = (accounts) => {
for (const account of accounts) {
account.userName = account.owner
.toLowerCase()
.split(" ")
.map((str) => str[0])
.join("");
}
};
computeUserNames(accounts);
/* DISPLAY ERROR */
const displayError = (message) => {
errorMessage.textContent = message;
setTimeout(() => (errorMessage.textContent = ""), 1000);
};
/* LOGIN FUNCTION */
const userLogin = (userName, password) => {
const userFound = accounts.find((account) => account.userName === userName);
if (userFound) {
if (password !== userFound.pin) {
displayError("Invalid Username or Password");
} else {
//userFound["pin"] = "****";
localStorage.setItem("loggedInUser", JSON.stringify(userFound));
localStorage.setItem("accounts", JSON.stringify(accounts));
window.location.href = "/layout.html";
}
} else {
displayError("Invalid Username or Password");
}
};
document.querySelector(".login-form").addEventListener("submit", (e) => {
e.preventDefault();
userLogin(userNameField.value, Number(passwordField.value));
});