1

我正在用 NodeJS 构建一个 web 应用程序。

应用程序的后端仅通过 Json API 与用户连接。我正在尝试做登录选项。这是可能的?如何接收来自 Passport Authenticate 的响应?

函数 post_api(req, res) {

res.writeHead(200, { 'Content-Type': 'application/json' });    
// Check method
if(!req.body.method)
  res.write( JSON.stringify({
          "status":"400",
          "response": {
            "status" : "error",
            "info":  "Method not defined."
          } 
      }));
else
{

    if(req.body.method == "login")
    { 

        passport.authenticate('local', {failureFlash: true, successFlash: true});  
        // Something here but i have no idea what...

        res.write( JSON.stringify({
            "status":"200",
            "response": {
                "status" : "error", // or success
                "info":  req.flash('error')
            }
        })); 

    }
    else
      res.write( JSON.stringify({
          "status":"404",
          "response": {
            "status" : "error",
            "info":  "Method not found."
          } 
      }));

}    
res.end(); };

这当然行不通,它应该是什么样子?

4

0 回答 0