https://github.com/robertklep/passport-request
Request authentication strategy for Passport
https://github.com/robertklep/passport-request
authentication node passport
Last synced: 2 months ago
JSON representation
Request authentication strategy for Passport
- Host: GitHub
- URL: https://github.com/robertklep/passport-request
- Owner: robertklep
- Created: 2016-06-08T10:47:53.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-06-08T10:53:36.000Z (about 10 years ago)
- Last Synced: 2025-02-15T02:43:08.369Z (over 1 year ago)
- Topics: authentication, node, passport
- Language: JavaScript
- Size: 1000 Bytes
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# passport-request
[Passport](http://passportjs.org/) strategy for authenticating against
Express' [`request`](http://expressjs.com/en/4x/api.html#req) object.
## Install
```bash
$ npm install passport-request
```
## Usage
#### Configure Strategy
The strategy requires a `verify` callback, which gets passed the request object and a `done` callback that should be called with the results of the authentication.
For example, to allow authentication using `POST /login/:username` and a `password` parameter in the request body:
```js
var RequestStrategy = require('passport-request').Strategy;
passport.use(new RequestStrategy(function(req, done) {
var username = req.params.username;
var password = req.body.password;
User.findOne({ username : username }, function (err, user) {
if (err) {
return done(err);
} else if (! user) {
return done(null, false);
} else if (! user.verifyPassword(password)) {
return done(null, false);
} else {
return done(null, user);
}
});
));
```
#### Authenticate Requests
Use `passport.authenticate()`, specifying the `'request'` strategy, to
authenticate requests.
For example, as route middleware in an [Express](http://expressjs.com/)
application:
```js
app.post('/login/:username',
passport.authenticate('request', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
}
);
```
## Credits
- [Robert Klep](http://github.com/robertklep)
- [Jared Hanson](http://github.com/jaredhanson)
## License
[The MIT License](http://opensource.org/licenses/MIT)