UNPKG

http-auth-passport

Version:

Passport.js integration with http-auth module.

40 lines (32 loc) 848 B
"use strict"; // Imports. // eslint-disable-next-line node/no-unpublished-require const passport = require("passport"); const util = require("util"); // Define strategy. function HttpStrategy(auth) { this.name = "http"; this.authentication = auth; passport.Strategy.call(this); } // Inherit basic strategy. util.inherits(HttpStrategy, passport.Strategy); // Define auth method. HttpStrategy.prototype.authenticate = function(req) { let self = this; // Is auth. this.authentication.isAuthenticated(req, result => { if (result instanceof Error) { self.error(result); } else if (!result.pass) { let header = self.authentication.generateHeader(result); self.fail(header); } else { self.success(result.user); } }); }; // Export. module.exports = auth => { return new HttpStrategy(auth); };