Skip to content

Commit

Permalink
has pass
Browse files Browse the repository at this point in the history
  • Loading branch information
William committed Sep 27, 2024
1 parent 8396d9a commit 369b027
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
10 changes: 7 additions & 3 deletions threejs/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,28 @@ export async function getUser(username) {
}
}

export function addUser(username) {
export function addUser(username, password) {
const newUserRef = ref(db, `users/${username}`);
set(newUserRef, {
username: username,
highScore: 0,
password: password,
})
.catch((error) => {
console.error("Error adding user: ", error);
});
}

export async function logUser(username) {
export async function logUser(username, password = localStorage.getItem("password")) {
const userRef = ref(db, `users/${username}`);
const user = await getUser(username);
if (user === null) {
addUser(username);
addUser(username, password);
loggedUser = await getUser(username);
} else {
if (user.password !== password) {
return null;
}
loggedUser = user;
}

Expand Down
32 changes: 27 additions & 5 deletions threejs/snake.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsSHA/3.3.1/sha.js"></script>
<meta charset="utf-8">
<title>Snake de William NOEL</title>
<style>
Expand Down Expand Up @@ -155,12 +156,19 @@ <h5 class="modal-title" id="userLoginModalLabel">Connexion pour sauvegarder votr
</div>
<div class="modal-body">
<form>
<div id="errorLogin" class="alert alert-danger" role="alert" style="display: none;">
Nom d'utilisateur ou mot de passe incorrect
</div>
<div class="mb-3">
<label for="username" class="form-label">Nom d'utilisateur</label>
<input type="text" class="form-control" id="username" aria-describedby="usernameHelp">
<div id="usernameHelp" class="form-text">Votre nom d'utilisateur sera affiché dans le
classement</div>
</div>
<div class="mb-3">
<label for="password" class="form-label">Mot de passe</label>
<input type="password" class="form-control" id="password">
</div>
<button id="login" type="submit" class="btn btn-primary">Connexion</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Plus tard</button>
</form>
Expand Down Expand Up @@ -206,17 +214,31 @@ <h5 class="modal-title" id="userLoginModalLabel">Connexion pour sauvegarder votr
if (localStorage.getItem('username') === null) {
userLoginModal.show();
} else {
await logUser(localStorage.getItem('username'));
document.getElementById('loggedUsername').innerText = loggedUser.username;
document.getElementById('username').setAttribute('readonly', true);
document.getElementById('highscore').innerText = loggedUser.highScore || 0;
await logUser(localStorage.getItem('username'), localStorage.getItem('password'));
if (loggedUser == null) {
userLoginModal.show();
} else {
document.getElementById('loggedUsername').innerText = loggedUser.username;
document.getElementById('username').setAttribute('readonly', true);
document.getElementById('highscore').innerText = loggedUser.highScore || 0;
}
}

document.getElementById('login').addEventListener('click', async (event) => {
event.preventDefault();
let username = document.getElementById('username').value;
let password = document.getElementById('password').value;
var hashObj = new jsSHA("SHA-512", "TEXT", { numRounds: 1 });
hashObj.update(password);
let hashedPassword = hashObj.getHash("HEX");
if (username != '') {
await logUser(username);
await logUser(username, hashedPassword);
if (loggedUser == null) {
document.getElementById('errorLogin').style.display = 'block';
return;
}
localStorage.setItem('password', hashedPassword);
document.getElementById('errorLogin').style.display = 'none';
document.getElementById('loggedUsername').innerText = loggedUser.username;
document.getElementById('highscore').innerText = loggedUser.highScore || 0;
}
Expand Down

0 comments on commit 369b027

Please sign in to comment.