Basic HTTP authentication with Node and Express 4

后端 未结 9 2094
萌比男神i
萌比男神i 2020-11-29 15:48

It looks like implementing basic HTTP authentication with Express v3 was trivial:

app.use(express.basicAuth(\'username\', \'password\'));

V

9条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 16:54

    I used the code for the original basicAuth to find the answer:

    app.use(function(req, res, next) {
        var user = auth(req);
    
        if (user === undefined || user['name'] !== 'username' || user['pass'] !== 'password') {
            res.statusCode = 401;
            res.setHeader('WWW-Authenticate', 'Basic realm="MyRealmName"');
            res.end('Unauthorized');
        } else {
            next();
        }
    });
    

提交回复
热议问题