{"id":201206,"date":"2013-11-07T13:14:02","date_gmt":"2013-11-07T09:14:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=201206"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=201206","title":{"rendered":"<span class=\"post_title\">\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 Passport.js<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t<a href=\"http:\/\/passportjs.org\/\"><img decoding=\"async\" src=\"http:\/\/habr.habrastorage.org\/post_images\/7de\/295\/6b4\/7de2956b40fe944f455aae15838f7fe6.png\"\/><\/a><br \/>  PassportJS \u2014 \u044d\u0442\u043e middleware \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0434 node.js. Passport \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u00ab\u0412\u041a\u043e\u043d\u0442\u0430\u043a\u0442\u0435\u00bb \u0438 \u043f\u0440\u043e\u0447\u0438\u0435 \u0442\u0432\u0438\u0442\u0442\u0435\u0440\u044b. \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"http:\/\/passportjs.org\/guide\/providers\/\">\u0437\u0434\u0435\u0441\u044c<\/a>. \u042f \u0445\u043e\u0447\u0443 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 middleware \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0441\u0430\u043c\u043e\u0439 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043b\u043e\u0433\u0438\u043d\u0430 \u0438 \u043f\u0430\u0440\u043e\u043b\u044f. <\/p>\n<p>  \u0414\u043b\u044f \u0441\u0430\u043c\u044b\u0445 \u043d\u0435\u0442\u0435\u0440\u043f\u0435\u043b\u0438\u0432\u044b\u0445 \u2014 \u043f\u0440\u043e\u0435\u043a\u0442 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"https:\/\/github.com\/shuvalov-anton\/passport-auth-example\">\u0437\u0434\u0435\u0441\u044c<\/a>.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u0417\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/h4>\n<p>  <\/p>\n<ul>\n<li>\u00abexpress\u00bb: \u00ab3.3.7\u00bb,<\/li>\n<li>\u00abpassport\u00bb: &quot;~0.1.17&quot;,<\/li>\n<li>\u00abpassport-local\u00bb: &quot;~0.1.6&quot;,<\/li>\n<li>\u00abmongoose\u00bb: &quot;~3.8.0&quot;,<\/li>\n<\/ul>\n<p>  \u0422\u0430\u043a \u0436\u0435 \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0442\u0438\u043b\u0438\u0442. \u0411\u0435\u0437 \u043d\u0438\u0445 \u0432\u043f\u043e\u043b\u043d\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c:<\/p>\n<ul>\n<li>\u00abjade\u00bb: &quot;*&quot;<\/li>\n<li>\u00abbootable\u00bb: &quot;~0.1.0&quot;<\/li>\n<li>\u00abnconf\u00bb: &quot;~0.6.8&quot;<\/li>\n<li>\u00abrequire-tree\u00bb: &quot;~0.3.2&quot;<\/li>\n<li>\u00abwinston\u00bb: &quot;~0.7.2&quot;<\/li>\n<\/ul>\n<h4>\u041c\u043e\u0434\u0435\u043b\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/h4>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u044f \u0434\u0443\u043c\u0430\u044e, \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:  <\/p>\n<pre><code class=\"javascript\">var UserSchema = new mongoose.Schema({   username: {     type: String,     unique: true,     required: true   },   password: {     type: String,     required: true   }, }); mongoose.model('user', UserSchema); <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043f\u043e\u0441\u043e\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u043e\u043b\u044c, \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043c\u0430\u0433\u0438\u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u0431\u043e\u0433\u0430 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438, \u043d\u043e \u044f \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u044d\u0442\u043e \u043d\u0430 \u0432\u0430\u0441 =).<\/p>\n<h4>\u0421\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/h4>\n<h5>\u0412\u0435\u0440\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/h5>\n<p>  \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.   <\/p>\n<pre><code class=\"javascript\">var passport       = require('passport'); var LocalStrategy  = require('passport-local').Strategy;  passport.use(new LocalStrategy({   usernameField: 'email',   passwordField: 'password' }, function(username, password,done){   User.findOne({ username : username},function(err,user){     return err        ? done(err)       : user         ? password === user.password           ? done(null, user)           : done(null, false, { message: 'Incorrect password.' })         : done(null, false, { message: 'Incorrect username.' });   }); })); <\/code><\/pre>\n<p>  LocalStrategy \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 2 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430: \u043e\u0431\u044a\u0435\u043a\u0442 \u0441 \u043e\u043f\u0446\u0438\u044f\u043c\u0438 \u0438 middleware \u0434\u043b\u044f \u0432\u0435\u0440\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. <br \/>  \u041f\u043e-\u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u0435\u0441\u043b\u0438 \u0432 `LocalStrategy` \u043d\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043e\u043f\u0446\u0438\u0439 \u2014 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043a\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u0444\u043e\u0440\u043c\u0430\u0445 \u0441 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 `username` \u0438 `password`. \u041f\u0440\u0438 \u0436\u0435\u043b\u0430\u043d\u0438\u0438, \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u0438\u043c\u0435\u043d\u0430 \u0444\u043e\u0440\u043c, \u043a\u0430\u043a \u044f, \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0438 \u0441\u0434\u0435\u043b\u0430\u043b.<br \/>  \u0412\u0442\u043e\u0440\u043e\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u2014 middleware \u2014 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b `username`, `passport` \u0438 `done`. \u0412 done, \u0432\u0442\u043e\u0440\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c, \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0435\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0439 \u0435\u0441\u0442\u044c. <\/p>\n<h5>\u041f\u0440\u0438\u0432\u044f\u0437\u043a\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e<\/h5>\n<p>  \u0412 \u0442\u0438\u043f\u0438\u0447\u043d\u043e\u043c \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438, \u0443\u0447\u0435\u0442\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438. \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0442\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u043d\u0435\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0441\u0435\u0441\u0441\u0438\u044e, \u0430 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0441\u0435\u0441\u0441\u0438\u0438, \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0432 cookies \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430.<\/p>\n<p>  \u041a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c cookies, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e passport \u0441\u043c\u043e\u0436\u0435\u0442 \u043e\u043f\u043e\u0437\u043d\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0441\u0435\u0441\u0441\u0438\u0438. \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438\u043b\u0438 \u0434\u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0441\u0435\u0441\u0441\u0438\u0438, \u043f\u0430\u0441\u043f\u043e\u0440\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 `passport.serializeUser()` \u0438 `passport.deserializeUser()`. <\/p>\n<pre><code class=\"javascript\">passport.serializeUser(function(user, done) {   done(null, user.id); });   passport.deserializeUser(function(id, done) {   User.findById(id, function(err,user){     err        ? done(err)       : done(null,user);   }); }); <\/code><\/pre>\n<h4>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 Passport \u043a Express<\/h4>\n<p>  \u041e\u043a\u0435\u0439, \u0441 \u044d\u0442\u0438\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c Passport \u043a Express:<\/p>\n<pre><code class=\"javascript\">\/\/ Middlewares, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b \u0434\u043e passport: app.use(express.cookieParser()); app.use(express.bodyParser()); app.use(express.session({ secret: 'SECRET' }));   \/\/ Passport: app.use(passport.initialize()); app.use(passport.session()); <\/code><\/pre>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0440\u043e\u0443\u0442\u0435\u0440\u0430 \u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432<\/h4>\n<p>  \u041d\u0430\u0441\u0442\u0430\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0440\u043e\u0443\u0442\u0435\u0440\u0430. \u041f\u0440\u0438\u0432\u044f\u0436\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u043c:<\/p>\n<pre><code class=\"javascript\">\/\/ Auth system app.post('\/login',                  controllers.users.login); app.post('\/register',               controllers.users.register); app.get('\/logout',                  controllers.users.logout); <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0434\u0435\u043c \u0441\u0430\u043c\u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b:  <\/p>\n<pre><code class=\"javascript\"> \/\/ \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432\u0435\u0440\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0443\u044e \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438 \u0432\u044b\u0448\u0435.  \/\/ \u0412\u043e\u043e\u0431\u0449\u0435, passport.authenticate() \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 req.logIn \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u0437\u0434\u0435\u0441\u044c \u0436\u0435 \u044f \u0443\u043a\u0430\u0437\u0430\u043b \u044d\u0442\u043e \u044f\u0432\u043d\u043e. \u042d\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e \u0432 \u043e\u0442\u043b\u0430\u0434\u043a\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0441\u044e\u0434\u0430 console.log(), \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442... \/\/ \u041f\u0440\u0438 \u0443\u0434\u0430\u0447\u043d\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0431\u0443\u0434\u0443\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0432 req.user module.exports.login = function(req, res, next) {   passport.authenticate('local',     function(err, user, info) {       return err          ? next(err)         : user           ? req.logIn(user, function(err) {               return err                 ? next(err)                 : res.redirect('\/private');             })           : res.redirect('\/');     }   )(req, res, next); };  \/\/ \u0417\u0434\u0435\u0441\u044c \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e =) module.exports.logout = function(req, res) {   req.logout();   res.redirect('\/'); };  \/\/ \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0435\u0433\u043e \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u0442\u0443\u0442 \u0436\u0435, \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434 `req.logIn`, \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0443\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f module.exports.register = function(req, res, next) {   var user = new User({ username: req.body.email, password: req.body.password});   user.save(function(err) {     return err       ? next(err)       : req.logIn(user, function(err) {         return err           ? next(err)           : res.redirect('\/private');       });   }); }; <\/code><\/pre>\n<h4>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/h4>\n<p>  \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e req.isAuthenticated(). \u042f \u0432\u044b\u043d\u0435\u0441\u0443 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0432 middleware.<\/p>\n<pre><code class=\"javascript\">exports.mustAuthenticatedMw = function (req, res, next){   req.isAuthenticated()     ? next()     : res.redirect('\/'); }; <\/code><\/pre>\n<p>  \u0418 \u0434\u043e\u0431\u0430\u0432\u043b\u044e \u0432 routes.<\/p>\n<pre><code class=\"javascript\">  App.all('private', mustAuthenticatedMw);   App.all('private\/*', mustAuthenticatedMw); <\/code><\/pre>\n<h4>HTML<\/h4>\n<p>  \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0444\u043e\u0440\u043c\u0443 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438, \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u0438 \u043a\u043d\u043e\u043f\u043a\u0443 \u043b\u043e\u0433\u0430\u0443\u0442\u0430.<\/p>\n<p>  \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f:  <\/p>\n<pre><code class=\"javascript\">h4 Register your account form(action='\/register' method='post')   fieldset     label(for='email') Email     input(type='email' name='email' placeholder='Your Email')     label(for='password') Password     input(type='password' name='password' placeholder='Your Password')     button(type='Register') <\/code><\/pre>\n<p>  \u041b\u043e\u0433\u0438\u043d  <\/p>\n<pre><code class=\"javascript\">h4 Login to your account form(action='\/login' method='post')   fieldset     label(for='email') Email     input(type='email' name='email' placeholder='Your Email')     label(for='password') Password     input(type='password' name='password' placeholder='Your Password')     button(type='Login') <\/code><\/pre>\n<p>  \u041b\u043e\u0433\u0430\u0443\u0442  <\/p>\n<pre><code class=\"javascript\">a(href='\/logout') logout <\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0436, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443. \u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0441\u043e\u0442\u043d\u0438 \u043a\u043e\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0444\u043e\u0440\u043a\u043d\u0443\u0442\u044c <a href=\"https:\/\/github.com\/shuvalov-anton\/passport-auth-example\">\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043d\u0430 GitHub<\/a>.    \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/201206\/\"> http:\/\/habrahabr.ru\/post\/201206\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t<a href=\"http:\/\/passportjs.org\/\"><img decoding=\"async\" src=\"http:\/\/habr.habrastorage.org\/post_images\/7de\/295\/6b4\/7de2956b40fe944f455aae15838f7fe6.png\"\/><\/a><br \/>  PassportJS \u2014 \u044d\u0442\u043e middleware \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0434 node.js. Passport \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u00ab\u0412\u041a\u043e\u043d\u0442\u0430\u043a\u0442\u0435\u00bb \u0438 \u043f\u0440\u043e\u0447\u0438\u0435 \u0442\u0432\u0438\u0442\u0442\u0435\u0440\u044b. \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"http:\/\/passportjs.org\/guide\/providers\/\">\u0437\u0434\u0435\u0441\u044c<\/a>. \u042f \u0445\u043e\u0447\u0443 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 middleware \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0441\u0430\u043c\u043e\u0439 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043b\u043e\u0433\u0438\u043d\u0430 \u0438 \u043f\u0430\u0440\u043e\u043b\u044f. <\/p>\n<p>  \u0414\u043b\u044f \u0441\u0430\u043c\u044b\u0445 \u043d\u0435\u0442\u0435\u0440\u043f\u0435\u043b\u0438\u0432\u044b\u0445 \u2014 \u043f\u0440\u043e\u0435\u043a\u0442 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"https:\/\/github.com\/shuvalov-anton\/passport-auth-example\">\u0437\u0434\u0435\u0441\u044c<\/a>.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-201206","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/201206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=201206"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/201206\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=201206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=201206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=201206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}