-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.js
127 lines (96 loc) · 3.87 KB
/
Form1.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
const signUpButton = document.getElementById('signUp');
const signInButton = document.getElementById('signIn');
const container = document.getElementById('container');
const signupForm = document.querySelector('#signup form');
const loginForm = document.querySelector('#login form');
signUpButton.addEventListener('click', () => {
container.classList.add("right-panel-active");
});
signInButton.addEventListener('click', () => {
container.classList.remove("right-panel-active");
});
signupForm.addEventListener('submit', function(event) {
event.preventDefault();
// Get form input values
const firstName = this.querySelector('#username').value;
const email = this.querySelector('#email').value;
const password = this.querySelector('#password').value;
// Check if user already exists in local storage
const users = JSON.parse(localStorage.getItem('users')) || [];
const userExists = users.some(user => user.email === email);
if (userExists) {
showToast('User already exists, please log in instead');
document.getElementById('signup').classList.add('hidden');
this.reset();
return;
}
// Create new user object
const newUser = {
firstName,
email,
password
};
// Add new user to local storage
users.push(newUser);
localStorage.setItem('users', JSON.stringify(users));
showToast('Account created successfully');
// Reset form inputs
this.reset();
setTimeout(function() {
document.getElementById('signup').classList.add('hidden');
}, 500);
});
// Login form submit event handler
loginForm.addEventListener('submit', function(event) {
event.preventDefault();
// Get form input values
const email = this.querySelector('#login-username').value;
const password = this.querySelector('#login-password').value;
// Check if user exists in local storage and password matches
const users = JSON.parse(localStorage.getItem('users')) || [];
const user = users.find(user => user.email === email && user.password === password);
if (!user) {
showToast('Invalid email or password, please create an account');
this.reset();
return;
}
// Set isLogin to true in local storage
localStorage.setItem('isLoggedIn', true);
showToast('Logged in successfully');
// Reset form inputs
this.reset();
setTimeout(function() {
window.location.href = 'Home.html';
}, 500);
});
if (localStorage.getItem('isLoggedIn') === 'true') {
// User is logged in, show the "logged in" section and hide the "logged out" section
document.getElementById('container').classList.add('hide');
document.getElementById('profile').classList.remove('hide');
} else {
document.getElementById('profile').classList.add('hide');
document.getElementById('container').classList.remove('hide');
}
const storeddetail = JSON.parse(localStorage.getItem('users'));
const storedName=storeddetail[0].firstName
const Uname= document.getElementById('name')
Uname.textContent=storedName;
const storedEmail=storeddetail[0].email
const Uemail= document.getElementById('Uemail')
console.log(Uemail)
Uemail.textContent=storedEmail;
const logoutBtn = document.querySelector("#profile button");
logoutBtn.addEventListener("click", function() {
localStorage.removeItem("card");
localStorage.removeItem("isLoggedIn"); // replace "key" with the name of the item you want to remove from local storage
window.location.href = 'Form1.html'
});
function showToast(message) {
const toaster = document.getElementById("toaster");
const toasterMessage = document.getElementById("toaster-message");
toasterMessage.innerText = message;
toaster.classList.add("active");
setTimeout(() => {
toaster.classList.remove("active");
}, 5000);
}