2017-04-03 60 views
0

我試圖實現在此基礎上回答一個基本的登錄機制: https://stackoverflow.com/a/8003291/5111904快速登錄並重定向使用Node.js的

在我的後端我處理登錄post要求是這樣的:

app.post('/login', function (req, res) { 
    console.log(req.body); 
    if (req.body.user === 'normal' && req.body.password === '12345') { 
     req.session.user_id = 0; // This is failing (req.session is undefined) 
     res.redirect('/index'); 
    } else { 
     res.send('Bad user/pass'); 
    } 
}); 

服務器正在使用https

server = https.createServer(https_options, app).listen(PORT, HOST);

當第E客戶機點擊登錄按鈕是越來越執行該代碼:

function postLogin(){ 
    var url = "/login"; 
    var xhr = new XMLHttpRequest(); 
    var data = { 
     user: userInput.value, 
     password: passwordInput.value 
    }; 

    xhr.open("POST", url, true); 
    xhr.onreadystatechange = function (oEvent) { 
     if(xhr.readyState === 4){ 
      // Checking status codes 
      if(xhr.status === 200){ 
       onSuccess(xhr.responseText, xhr.responseType); 
      } 
      else{ 
       console.log(xhr.status); 
       onError(); 
      } 

     } 
    } 
    xhr.setRequestHeader("Content-type", "application/json; charset=UTF-8"); 
    console.log("Sending this data: " + JSON.stringify(data)); 
    xhr.send(JSON.stringify(data)); 
} 

登錄用戶應重定向到索引頁面後:

app.get('/index', (request, response) => { 
    response.render('main', {}); 
}) 

所以我得到了這個問題:

  • 在第一個代碼段中,req.session未定義
  • 在secod片段中,(xhr.responseText)的值計算爲html的 索引頁面(用戶應該被重定向)
  • 如何以正確的方式將用戶重定向到索引頁面?

將只有一個有效的用戶,所以這個代碼不打算被許多人使用,它應該只提供一個基本的安全類型。

+0

你有沒有進口'快車session',如:'導入會話從「快車session'' –

+0

@AnushShrestha我沒有 - 我想這應該解決'undefined'問題 - 將嘗試 - TY –

+0

@AnushShrestha我安裝了軟件包,並像這樣包含它:'const session = require('express-session')' - 仍然有未定義的問題 –

回答

1

實際上有三個問題在這裏:

  1. 使用與快遞

在快遞會議上,如果你想使用會話,你需要把它初始化爲中間件到應用程序。在它的文檔中有一個詳細的指南:https://github.com/expressjs/session

app.use(session({ 
    secret: 'keyboard cat', 
    cookie: { secure: true } 
})) 

該中間件將添加session方法您req對象上,你現在可以使用req.session

基本上,你寫出來之前,你的路線,你需要在你的應用程序是這樣的。

  1. 接收到xhr.responseText中的html值。

我相信你誤解AJAX如何(或XMLHttpRequest的是特定的)的作品。如果請求服務器發送重定向頭,瀏覽器會自動遵循重定向,您將不會意識到這一點,最後會得到最終響應(除了這個answer之外,我找不到任何真實的文檔。您的AJAX請求會在重定向之後發送給您最終響應。

  • 重定向到索引頁面的正確方式
  • 與AJAX工作的正確方法是讓你的網頁是如何工作的前端所有的控制。因此頁面之間重定向的責任也落在前端。在你的情況下,最好的方法是發送一個請求到/login來登錄用戶。響應將有一個狀態代碼來確定成功或失敗(成功爲200,失敗可能爲401)。然後您可以檢查響應的狀態並重定向。

    在後端:

    app.post('/login', function (req, res) { 
        if (req.body.user === 'normal' && req.body.password === '12345') { 
         req.session.user_id = 0; 
         res.status(200).send('User logged in'); 
        } else { 
         res.status(401).send('Bad user/pass'); 
        } 
    }); 
    

    在前端:

    xhr.onreadystatechange = function (oEvent) { 
        if(xhr.readyState === 4){ 
         // Checking status codes 
         if(xhr.status === 200){ 
          // user logged in 
          window.location = '/index'; 
         } 
         else{ 
          // login failed 
          console.log(xhr.status); 
          onError(); 
         } 
    
        } 
    } 
    
    +0

    工作正常,ty-bu t notice:'user_id = 0'將在'chekAuth'函數中計算if case to false:'if(req.session.user_id)' –