2013-03-08 73 views
3

我有這樣一個集合MongoDB中的MongoDB和認證和護照的node.js

{ 
    username:silver, 
    Email:[email protected], 
    password:silvester, 
} 

所以要驗證我將獲取從數據庫中的數據,然後我會檢查指定的電子郵件是存在的或不符合if語句像這樣

app.post("/login",function(req,res){ 

    var email=req.body['emailid']; 

    collection.find({email:[email protected]}).toArray(function(err,res) 
    { 
     if(res.length==0){ 
     console.log("name is not exist"); 
     }else{ 
     if(res.email==email){ 
      console.log("email is exist"); 
     }else{ 
      console.log("not exist"); 
     } 
     } 
    }); 
}); 

所以在這裏如何使用護照模塊進行身份驗證。我知道它帶有配置示例代碼。 我正在使用express3.x框架.so如何配置它也。

回答

3

Here你可以閱讀關於本地策略,和here關於配置。

你的本地策略應該是這樣的:

passport.use(new LocalStrategy({ 
     emailField: 'email', 
     passwordField: 'passw', 
    }, 

    function (emailField, passwordField, done) { 
     process.nextTick(function() { 
      db.collection(dbCollection, function (error, collection) { 
       if (!error) { 
        collection.findOne({ 
         'email': [email protected] 
         'password': silvester // use there some crypto function 
        }, function (err, user) { 
         if (err) { 
          return done(err); 
         } 
         if (!user) { 
          console.log('this email does not exist'); 
          return done(null, false); 
         } 
         return done(null, user); 
        }); 
       } else { 
        console.log(5, 'DB error'); 
       } 
      }); 
     }); 
    })); 
+0

我應該寫像上面這樣app.post POST請求( '/登錄',函數(REQ內編碼,RES){passport.use(新LocalStrategy( { emailField:'email', passwordField:'passw', },})讓我知道它.. – silvesterprabu 2013-03-08 09:21:02

+0

我不確定這是個好主意。另外,請不要忘記關於'passport.serializeUser'和'passport.deserializeUser'。 – msmirnov 2013-03-08 09:26:07

+0

如何使用護照來驗證mongodb的數據 – silvesterprabu 2013-03-08 09:32:02