2016-04-25 35 views
0

每個教程或未完成的文檔都沒有工作。這就是爲什麼我在這裏問:有沒有一個簡單的教程,這對於護照和風帆真的起作用?有沒有將passport.js集成到sails.js的文檔或教程?

+0

http://iliketomatoes.com/implement-passport-js-authentication-with-sails-js-0-10-2/ 這是很好的教程! – vkstack

+0

也許你需要閱讀參考文檔並自己編寫代碼,而不是尋找「教程」 –

+0

也許...但也許文檔沒有完成或不起作用。你試過了嗎?它真的有用嗎?發佈網址,我會閱讀並嘗試 - 當某些事情不起作用時,我會問你。謝謝。 – Suisse

回答

2

按照此步驟兩整合護照帆JS 第一: - 列表裏面application_directory /的package.json這些依賴關係下的依賴 //application_directory/package.json

{ 
... 
    "dependencies": { 
    ... 
    "passport": "~0.1.16", 
    "passport-local": "~0.1.6", 
    "bcrypt": "~0.7.6" 
    } 
... 
} 

2- 要創建用戶模型運行以下命令:

sails generate model user 

3 model user.js will如下所示

var bcrypt = require('bcrypt'); 

module.exports = { 

    attributes: { 
    username: { 
     type: 'string', 
     required: true, 
     unique: true 
    }, 
    password: { 
     type: 'string', 
     required: true 
    }, 
    toJSON: function() { 
     var obj = this.toObject(); 
     delete obj.password; 
     return obj; 
    } 
    }, 

    beforeCreate: function(user, cb) { 
    bcrypt.genSalt(10, function(err, salt) { 
     bcrypt.hash(user.password, salt, function(err, hash) { 
     if (err) { 
      console.log(err); 
      cb(err); 
     }else{ 
      user.password = hash; 
      cb(null, user); 
     } 
     }); 
    }); 
    } 

}; 

- 要創建的控制器在帆鍵入命令

sails generate controller 

AuthController將如下所示:

var passport = require('passport'); 
module.exports = { 

    login: function (req, res) { 
    res.view(); 
    }, 
    process: function(req, res){ 
    passport.authenticate('local', function(err, user, info) { 
     if ((err) || (!user)) { 
     return res.send({ 
     message: 'login failed' 
     }); 
     res.send(err); 
     } 
     req.logIn(user, function(err) { 
     if (err) res.send(err); 
     return res.send({ 
      message: 'login successful' 
     }); 
     }); 
    })(req, res); 
    }, 
    logout: function (req,res){ 
    req.logout(); 
    res.send('logout successful'); 
    } 
}; 

module.exports.blueprints = { 
    actions: true, 
    rest: true, 

    shortcuts: true 

}; 

- 添加以下代碼到application_directory/config/routes.js

module.exports.routes = { 
    // (This would also work if you had a file at: `/views/home.ejs`) 
    '/': { 
    view: 'home/index' 
    }, 

    '/login': { 
    controller: 'AuthController', 
    action: 'login' 
    }, 
    '/logout': { 
    controller: 'AuthController', 
    action: 'logout' 
    } 
    ...... 
} 

- 裏面application_directory/config中創建一個文件passport.js和下面的代碼添加到

var passport = require('passport'), 
LocalStrategy = require('passport-local').Strategy; 
module.exports = { 
    express: { 
    customMiddleware: function(app){ 
     console.log('Express midleware for passport'); 
     app.use(passport.initialize()); 
     app.use(passport.session()); 
    } 
    } 
}; 

- 內/ API /服務/創建一個文件passport.js和下面的代碼添加到

var passport = require('passport'), 
    LocalStrategy = require('passport-local').Strategy, 
    bcrypt = require('bcrypt'); < /code> 
//helper functions 
function findById(id, fn) { 
    User.findOne(id).done(function (err, user) { 
    if (err) { 
     return fn(null, null); 
    } else { 
     return fn(null, user); 
    } 
    }); 
} 

function findByUsername(u, fn) { 
    User.findOne({ 
    username: u 
    }).done(function (err, user) { 
    // Error handling 
    if (err) { 
     return fn(null, null); 
     // The User was found successfully! 
    } else { 
     return fn(null, user); 
    } 
    }); 
} 

passport.serializeUser(function (user, done) { 
    done(null, user.id); 
}); 

passport.deserializeUser(function (id, done) { 
    findById(id, function (err, user) { 
    done(err, user); 
    }); 
}); 

passport.use(new LocalStrategy(
    function (username, password, done) { 
    // asynchronous verification, for effect... 
    process.nextTick(function() { 
     findByUsername(username, function (err, user) { 
     if (err) 
      return done(null, err); 
     if (!user) { 
      return done(null, false, { 
      message: 'Unknown user ' + username 
      }); 
     } 
     bcrypt.compare(password, user.password, function (err, res) { 
      if (!res) 
      return done(null, false, { 
       message: 'Invalid Password' 
      }); 
      var returnUser = { 
      username: user.username, 
      createdAt: user.createdAt, 
      id: user.id 
      }; 
      return done(null, returnUser, { 
      message: 'Logged In Successfully' 
      }); 
     }); 
     }) 
    }); 
    } 
)); 

- 修改authenticat ed.js文件目前內部/ API /政策/

/** 
* Allow any authenticated user. 
*/ 

    module.exports = function (req, res, ok) { 

     // User is allowed, proceed to controller 

      var is_auth = req.isAuthenticated() 
      if (is_auth) return next(); 
      // User is not allowed 
      else return res.redirect("/login"); 
     }; 
+0

thx艾哈邁德,這在最後一個剪輯「isAuthenticated()」來自哪裏?我在bcrypt.compare()函數中還返回了「var returnUser」中的userrole(isAdmin) - 我該如何爲此編寫策略?我如何檢查用戶是否是管理員? – Suisse

+0

好的,我找到了。 if(req.isAuthenticated()){console.log(req.user.isAdmin);} – Suisse

相關問題