@@ -21,7 +21,9 @@ hooks for controlling what occurs when authentication succeeds or fails.
2121
2222## Install
2323
24- $ npm install passport
24+ ```
25+ $ npm install passport
26+ ```
2527
2628## Usage
2729
@@ -35,16 +37,18 @@ or [Twitter](http://twitter.com/)), or federated authentication using [OpenID](h
3537Before authenticating requests, the strategy (or strategies) used by an
3638application must be configured.
3739
38- passport.use(new LocalStrategy(
39- function(username, password, done) {
40- User.findOne({ username: username }, function (err, user) {
41- if (err) { return done(err); }
42- if (!user) { return done(null, false); }
43- if (!user.verifyPassword(password)) { return done(null, false); }
44- return done(null, user);
45- });
46- }
47- ));
40+ ``` javascript
41+ passport .use (new LocalStrategy (
42+ function (username , password , done ) {
43+ User .findOne ({ username: username }, function (err , user ) {
44+ if (err) { return done (err); }
45+ if (! user) { return done (null , false ); }
46+ if (! user .verifyPassword (password)) { return done (null , false ); }
47+ return done (null , user);
48+ });
49+ }
50+ ));
51+ ```
4852
4953There are 300+ strategies. Find the ones you want at: [ passportjs.org] ( http://passportjs.org )
5054
@@ -60,15 +64,17 @@ serialization and deserialization logic. In a typical application, this will be
6064as simple as serializing the user ID, and finding the user by ID when
6165deserializing.
6266
63- passport.serializeUser(function(user, done) {
64- done(null, user.id);
65- });
67+ ``` javascript
68+ passport .serializeUser (function (user , done ) {
69+ done (null , user .id );
70+ });
6671
67- passport.deserializeUser(function(id, done) {
68- User.findById(id, function (err, user) {
69- done(err, user);
70- });
71- });
72+ passport .deserializeUser (function (id , done ) {
73+ User .findById (id, function (err , user ) {
74+ done (err, user);
75+ });
76+ });
77+ ```
7278
7379#### Middleware
7480
@@ -78,24 +84,28 @@ with the required `passport.initialize()` middleware. If your application uses
7884persistent login sessions (recommended, but not required), ` passport.session() `
7985middleware must also be used.
8086
81- var app = express();
82- app.use(require('serve-static')(__dirname + '/../../public'));
83- app.use(require('cookie-parser')());
84- app.use(require('body-parser').urlencoded({ extended: true }));
85- app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
86- app.use(passport.initialize());
87- app.use(passport.session());
87+ ``` javascript
88+ var app = express ();
89+ app .use (require (' serve-static' )(__dirname + ' /../../public' ));
90+ app .use (require (' cookie-parser' )());
91+ app .use (require (' body-parser' ).urlencoded ({ extended: true }));
92+ app .use (require (' express-session' )({ secret: ' keyboard cat' , resave: true , saveUninitialized: true }));
93+ app .use (passport .initialize ());
94+ app .use (passport .session ());
95+ ```
8896
8997#### Authenticate Requests
9098
9199Passport provides an ` authenticate() ` function, which is used as route
92100middleware to authenticate requests.
93101
94- app.post('/login',
95- passport.authenticate('local', { failureRedirect: '/login' }),
96- function(req, res) {
97- res.redirect('/');
98- });
102+ ``` javascript
103+ app .post (' /login' ,
104+ passport .authenticate (' local' , { failureRedirect: ' /login' }),
105+ function (req , res ) {
106+ res .redirect (' /' );
107+ });
108+ ```
99109
100110## Strategies
101111
@@ -122,10 +132,12 @@ The following table lists commonly used strategies:
122132
123133- For a complete, working example, refer to the [ example] ( https://github.com/passport/express-4.x-local-example )
124134that uses [ passport-local] ( https://github.com/jaredhanson/passport-local ) .
125- - ** Local Strategy** : Refer to the following tutorials for setting up user authentication via LocalStrategy (` passport-local ` )
135+ - ** Local Strategy** : Refer to the following tutorials for setting up user authentication via LocalStrategy (` passport-local ` ):
126136 - Express v3x - [ Tutorial] ( http://mherman.org/blog/2013/11/11/user-authentication-with-passport-dot-js/ ) / [ working example] ( https://github.com/mjhea0/passport-local )
127137 - Express v4x - [ Tutorial] ( http://mherman.org/blog/2015/01/31/local-authentication-with-passport-and-express-4/ ) / [ working example] ( https://github.com/mjhea0/passport-local-express4 )
128- - ** Social Authentication** : Refer to this [ tutorial] ( http://mherman.org/blog/2013/11/10/social-authentication-with-passport-dot-js/ ) for setting up various social authentication strategies, including a working example found on this [ repo] ( https://github.com/mjhea0/passport-examples ) .
138+ - ** Social Authentication** : Refer to the following tutorials for setting up various social authentication strategies:
139+ - Express v3x - [ Tutorial] ( http://mherman.org/blog/2013/11/10/social-authentication-with-passport-dot-js/ ) / [ working example] ( https://github.com/mjhea0/passport-examples )
140+ - Express v4x - [ Tutorial] ( http://mherman.org/blog/2015/09/26/social-authentication-in-node-dot-js-with-passport ) / [ working example] ( https://github.com/mjhea0/passport-social-auth )
129141
130142## Related Modules
131143
@@ -140,8 +152,10 @@ that build upon or integrate with Passport.
140152
141153## Tests
142154
143- $ npm install
144- $ make test
155+ ```
156+ $ npm install
157+ $ make test
158+ ```
145159
146160## Credits
147161
0 commit comments