2016-04-03 84 views
2

我爲Expressjs(此時沒有護照)使用Stormpath來處理我的用戶註冊。我非常擔心註銷用戶的正確方法。用戶註銷:重定向GET到POST(節點/快遞)

之前,我總是通過GET方法做到這一點,但Stormpath explicitly requires a POST call to the /logout page

我只顯示當用戶通過Stormpath登錄的用戶菜單,這是超級簡單的中間件,我在我的router.js在快遞:現在

router.use(stormpath.getUser, function (req, res, next) { 
    if (req.user) { 
     req.session.useremail = req.user.email; 
    } 
    next(); 
}); 

,當然在此菜單中有註銷條目。我希望我的用戶點擊此鏈接註銷,但是我知道當使用類似<a href="/logout">Logout</a>的錨鏈接時,會發送GET請求,而不是POST。

我一直在尋找無數的方法來重定向GET請求到POST,但我覺得這是絕對錯誤的方式。此外,我覺得這將毫無意義使用表單像菜單裏面:

<ul> 
    <li>User Profile</li> 
    <li>User Settings</li> 
    <form action="/logout" method="/post"> 
     <input type="submit"> 
    </form> 
</ul> 

所以現在的問題是:什麼是簡單地退出通過POST的用戶的最佳方法是什麼?

+0

你好!我在[Stormpath](https://stormpath.com)工作,並將更改實施到POST。此SO討論是我們爲什麼選擇使用POST的一個很好的總結:http://stackoverflow.com/questions/3521290/logout-get-or-post – robertjd

回答

1

我不認爲這有什麼特殊性不好有菜單的形式,但如果你真的想擁有你可能需要做一些事情,像這樣的GET請求:

var request = require('request') 

app.get('/logout', function(req, res) { 

    // Send logout POST request to stormpath REST api 
    request.post({ 
     url: 'https://stormpath/logout/url', 
     form: { 
     key: 'value' 
     } 
    }, 
    function(err, httpResponse, body) { 
     // Check if there was an error logging the user-out 
     if (err) return res.send('Error logging out') 
     // If no error, user is logged out so redirect them or something 
     res.redirect('/') 
    }) 
}) 

所以你express應用程序接受GET請求,然後代表用戶向POST請求Stormpath以將用戶登出。

如果您確實轉向使用Stormpath護照過關手段,我認爲您可以簡單地致電req.logout()

+0

最後我實現了

解決方案,但我將其標記爲一個有效的答案,因爲它實際上回答我的問題 – wiredmark