forked from b00tc4mp/isdi-bootcamp-202409
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unsocial login, register and home exercise WIP b00tc4mp#167
- Loading branch information
1 parent
d35bd21
commit c7188c3
Showing
1 changed file
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Unsocial</title> | ||
<link rel="shortcut icon" href="https://b00tc4mp.com/favicon.ico" type="image/x-icon"> | ||
<style> | ||
@import url('https://fonts.googleapis.com/css2?family=Sixtyfour&display=swap'); | ||
|
||
:root { | ||
--color: dodgerblue; | ||
--font: 'Sixtyfour'; | ||
font-family: var(--font); | ||
} | ||
|
||
body { | ||
background-color: lightgrey; | ||
color: var(--colorX); | ||
} | ||
|
||
input { | ||
background-color: inherit; | ||
font-family: inherit; | ||
} | ||
|
||
a:visited { | ||
color: var(--color); | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<h1>Unsocial</h1> | ||
|
||
<section> | ||
<h2>Login</h2> | ||
|
||
<form> | ||
<label for="username">Username</label> | ||
<input type="text" id="username" /> | ||
|
||
<label for="password">Password</label> | ||
<input type="password" id="password" /> | ||
|
||
<button type="submit">Login</button> | ||
</form> | ||
|
||
<a href="">Register</a> | ||
</section> | ||
|
||
<section> | ||
<h2>Register</h2> | ||
|
||
<form> | ||
<label for="name">Name</label> | ||
<input type="text" id="name" /> | ||
|
||
<label for="email">E-mail</label> | ||
<input type="email" id="email" /> | ||
|
||
<label for="username">Username</label> | ||
<input type="text" id="username" /> | ||
|
||
<label for="password">Password</label> | ||
<input type="password" id="password" /> | ||
|
||
<button type="submit">Register</button> | ||
</form> | ||
|
||
<a href="">Login</a> | ||
</section> | ||
|
||
<section> | ||
<h2>Home</h2> | ||
|
||
<h3>Hello, User!</h3> | ||
|
||
<button type="submit">Logout</button> | ||
</section> | ||
|
||
<script> | ||
var users = [ | ||
{ name: 'Peter Pan', email: '[email protected]', username: 'peterpan', password: '123123123' }, | ||
{ name: 'Wendy Darling', email: '[email protected]', username: 'wendydarling', password: '123123123' } | ||
] | ||
|
||
var loggedInUser = null | ||
|
||
var sections = document.querySelectorAll('section') | ||
|
||
var loginSection = sections[0] | ||
loginSection.style.display = '' | ||
|
||
var registerSection = sections[1] | ||
registerSection.style.display = 'none' | ||
|
||
var homeSection = sections[2] | ||
homeSection.style.display = 'none' | ||
|
||
var anchors = document.querySelectorAll('a') | ||
|
||
var registerAnchor = anchors[0] | ||
// registerAnchor.onclick = function (event) { | ||
registerAnchor.addEventListener('click', function (event) { | ||
event.preventDefault() | ||
|
||
loginSection.style.display = 'none' | ||
registerSection.style.display = '' | ||
// } | ||
}) | ||
|
||
var loginAnchor = anchors[1] | ||
//loginAnchor.onclick = function (event) { | ||
loginAnchor.addEventListener('click', function (event) { | ||
event.preventDefault() | ||
|
||
registerSection.style.display = 'none' | ||
loginSection.style.display = '' | ||
// } | ||
}) | ||
|
||
var forms = document.querySelectorAll('form') | ||
|
||
var loginForm = forms[0] | ||
loginForm.addEventListener('submit', function (event) { | ||
event.preventDefault() | ||
|
||
var inputs = loginForm.querySelectorAll('input') | ||
|
||
var usernameInput = inputs[0] | ||
var username = usernameInput.value | ||
|
||
var passwordInput = inputs[1] | ||
var password = passwordInput.value | ||
|
||
if (users.find(function (user) { | ||
if (user.username === username && user.password === password) { | ||
loggedInUser = user | ||
return true | ||
} | ||
})) { | ||
loginForm.reset() | ||
loginSection.style.display = 'none' | ||
homeSection.style.display = '' | ||
var h3 = homeSection.querySelector('h3') | ||
h3.innerText = "Hello, " + loggedInUser.name + "!" | ||
} else { | ||
alert('Not today') | ||
} | ||
}) | ||
|
||
var registerForm = forms[1] | ||
|
||
//registerForm.onsubmit = function (event) { | ||
registerForm.addEventListener('submit', function (event) { | ||
event.preventDefault() | ||
|
||
var inputs = registerForm.querySelectorAll('input') | ||
|
||
var nameInput = inputs[0] | ||
var name = nameInput.value | ||
|
||
var emailInput = inputs[1] | ||
var email = emailInput.value | ||
|
||
var usernameInput = inputs[2] | ||
var username = usernameInput.value | ||
|
||
var passwordInput = inputs[3] | ||
var password = passwordInput.value | ||
|
||
var user = { name: name, email: email, username: username, password: password } | ||
// users[users.length] = user | ||
users.push(user) | ||
|
||
registerForm.reset() | ||
registerSection.style.display = 'none' | ||
loginSection.style.display = '' | ||
// } | ||
|
||
}) | ||
|
||
var buttons = document.querySelectorAll('button') | ||
|
||
var logoutButton = buttons[2] | ||
// var logoutButton = homeSection.querySelector('button') | ||
logoutButton.addEventListener('click', function (event) { | ||
event.preventDefault() | ||
loggedInUser = null | ||
|
||
loginSection.style.display = '' | ||
homeSection.style.display = 'none' | ||
}) | ||
|
||
</script> | ||
</body> | ||
|
||
</html> |