首页 > 解决方案 > 护照的身份验证功能如何知道要验证哪个请求?

问题描述

护照文档使用它来保护路线:

app.get('/api/me',
  passport.authenticate('basic', { session: false }),
  function(req, res) {
    res.json(req.user);
  });

如何authenticate()知道要验证哪个请求?我没有将请求传递给它。

标签: node.jsexpresspassport.js

解决方案


The passport.authenticate returns a function.

You can try this

console.log(passport.authenticate('basic', { session: false }));

and it will print something like

function(req, res, next){ ... }

This means that the app.get will look something like this after your app starts

app.get('/api/me',
    function(req, res, next){
         // now passport has access to the "req" even though you didn't pass request to it
         // passport's authentication logic here
    },
    function(req, res) {
       res.json(req.user);
    });

推荐阅读