2017-09-04 250 views
0

不知道這是否可能,我是js的新手,請記住這一點。 我想在我的小表格中有多個密碼和多個用戶名。 javascript代碼爲:多用戶和密碼javascript登錄

var attempt = 3; 
function validate(){ 
var username = document.getElementById("username").value; 
var password = document.getElementById("password").value; 
if (username == "root" && password == "root"){ // here is password and username 
sweetAlert ("Login successfully"); 
window.location = "success.html"; 
return false; 
} 
else{ 
attempt --;// Decrementing by one. 
sweetAlert("You have "+attempt+" attempt(s) left."); 
// Disabling fields after 3 attempts. 
if(attempt == 0){ 
sweetAlert("Oops...", "You have failed to log in 3 times, Fields have been turned off. Please try again later", "error"); 
document.getElementById("username").disabled = true; 
document.getElementById("password").disabled = true; 
document.getElementById("submit").disabled = true; 
return false; 
} 
} 
+1

你知道這是非常差的安全性?任何人都可以閱讀您的JS代碼,即使在您的製作網站上。即使你的代碼被縮小了,人們仍然可以從代碼中讀取你的用戶名和密碼。 – Thijs

+0

這不是安全。 Bin it –

+0

是的,我完全意識到這一點,它只是在那裏阻止「普通」人員因未完成的頁面而感到困惑。 –

回答

0

這是一個使用Map存儲登錄名和密碼的解決方案。它比使用if語句更易於管理,因爲每次登錄都意味着您的邏輯發生了變化。

let 
 
    attempts = 3, 
 
    logins = new Map([ 
 
    ['root', 'root'], 
 
    ['admin', 'admin'] 
 
    ]); 
 
    formElement = document.getElementById('form'); 
 

 
function disableForm() { 
 
    const 
 
    usernameInput = document.getElementById('username'), 
 
    passwordInput = document.getElementById('password'), 
 
    submitButton = document.getElementById('submit'); 
 

 
    usernameInput.setAttribute('disabled', 'disabled'); 
 
    passwordInput.setAttribute('disabled', 'disabled'); 
 
    submitButton.setAttribute('disabled', 'disabled'); 
 
} 
 

 
function decreaseAttemptsLeft() { 
 
    // Decrease attempts by 1. 
 
    attempts--; 
 
    
 
    // Check if attempts is 0... 
 
    if (attempts === 0) { 
 
    // ... and disable the form controls. 
 
    disableForm(); 
 
    // ... and show the user a message. 
 
    alert('You have failed to log in 3 times, Fields have been turned off. Please try again later'); 
 
    } else { 
 
    // Show a message with login attempts left. 
 
    const 
 
     message = `You have ${attempts} attempt(s) left.`; 
 
    alert(message); 
 
    } 
 
} 
 

 
function onFormSubmit(event){ 
 
    // prevent the actual form submission. 
 
    event.preventDefault(); 
 
    
 
    const 
 
    usernameInput = document.getElementById('username'), 
 
    passwordInput = document.getElementById('password'); 
 
    
 
    if (
 
    // When the logins map doesn't have a key for the username OR 
 
    !logins.has(usernameInput.value) || 
 
    // The password doesn't match the value for the user name 
 
    logins.get(usernameInput.value) !== passwordInput.value 
 
) { 
 
    // Decrease the number of attempts left. 
 
    decreaseAttemptsLeft(); 
 
    // Exit the method. 
 
    return; 
 
    } 
 
    
 
    // Show the success message. 
 
    alert('Login success!'); 
 
    // Reset the attempts counter. 
 
    attempts = 3; 
 
} 
 
    
 
form.addEventListener('submit', onFormSubmit);
<form id="form"> 
 
    <label> 
 
    Username: 
 
    <input type="text" id="username"/> 
 
    </label> 
 
    <label> 
 
    Password: 
 
    <input type="password" id="password"/> 
 
    </label> 
 
    
 
    <button id="submit">Login</button> 
 
</form>

只是要非常清楚:這是安全性非常差。除了愚弄你的小妹妹/兄弟之外,別這樣做。

+0

阿哈歡呼隊友!在這種類型的代碼變得緩慢/不可用之前,我可以添加多少種限制?如上所述,是的,我知道js登錄是非常糟糕的安全智慧。但它的唯一目的是阻止「普通人」在被建造的時候感到困惑 –

+0

實際上沒有限制。您可以根據需要添加儘可能多的,並且不需要更改邏輯來驗證登錄。 – Thijs

0

這可以通過else if聲明來解決。

if (username == "root" && password == "root") { 
    // login user 1 
} 
else if(username == "username2" && password == "password2") { 
    // login user 2 
} 
else { 
    // reject 
} 

請記住,(因爲這似乎是在用戶的瀏覽器本地運行),這將是微不足道的用戶在頁面上直接編輯代碼,以接受他們選擇的任何登錄。着眼於接受服務器上的用戶憑據,只有您可以選擇使用哪個用戶名和密碼。

+0

謝謝!我需要爲此使用PHP嗎?現在我不想要一個Web服務器,因爲整個頁面只是爲了一個朋友。 –

+0

有很多種語言可以使用;使用Apache的PHP很常見。如果您對提高JavaScript技能感興趣,可以嘗試使用Node.js設置服務器。 – Aidan