2011-12-01 23 views
3

好吧,我放棄了。Redis和NodeJs - 無法驗證用戶

我試過很多事情來創建一個簡單的登錄表單。該表單本身呈現罰款,它只是處理POST數據是錯誤的:-)

我有一個Redis數據庫中有一些鍵。首先,有一個users設置用戶列表(目前,只有用戶:管理員)。其次,只有一個密碼hkey user:admin。看到下面的屏幕。

Redis screenshot

用下面的代碼,當我提交表單,它不會在Redis的回調函數得到來電:

var username = req.body.username; 
var password = req.body.password; 
// Now, check if the user he entered exists 
client.sismember(['users', 'user:' + username], function(err, reply) { 
    // IT NEVER GETS THERE 
    if (reply) { 
    // If he does, check if the password matches 
    client.hget(['user:' + username, 'password'], function(err, reply) { 
     if (reply === password) { 
     // If the password matches, add the session and redirects to home 
     req.session.username = username; 
     res.redirect('/home'); 
     } 
     else { 
     options.error = "Password do not match."; 
     res.render('guest', options); 
     } 
    }); 
    } 
    else { 
    options.error = "Username does not exist."; 
    res.render('guest', options); 
    } 
}); 

我使用的console.log來檢查一些東西,並且usernamepassword填寫得很好。

關於redis服務器連接(如果關閉服務器,我有這個問題)沒有錯誤,所以問題不存在。

我試過使用client.send_command()來代替,但沒有改變。

任何幫助將不勝感激!

+0

我正在爲您投票。這是我能爲你做的最多事情:-) –

回答

5

好的,答案是非常愚蠢的。我的意思是,我很愚蠢,沒有看到這一點。

這種異步性使得它很難完全得到!

問題是,後來在代碼中,在最後,我關閉了與client.end()的redis連接。

但是這個函數在回調被觸發之前被調用,即使它在代碼中很好。

+0

謝謝你的注意。它真的幫助我運行我的應用程序。 :d –