2012-10-09 55 views
3

我試着寫一個skeleton app進行身份驗證使用Mozilla Persona使用thistutorialpassportdocumentation和登錄總是失敗 - 我不知道我在做什麼錯因爲我沒有看到護照上的日誌。這是我的front-end code,這是我的server code,我確實看到Persona彈出窗口,但從彈出窗口登錄時,req.isAuthenticated()始終爲false。假面(BROWSERID)認證從來沒有成功地表達3.0(與護照BROWSERID)

下面是客戶端代碼片段:

<script type="text/coffeescript"> 
    $ -> 
     $('#signin').click -> navigator.id.request() 
     $('#signout').click -> navigator.id.logout() 

     navigator.id.watch 
     onlogin: (assertion) -> 
      $.ajax 
      type: 'POST' 
      url: '/login' 
      data: assertion: assertion 

      success: (res, status, xhr) -> 
       console.log res 

      error: (res, status, xhr) -> alert "login failure" + res 

     onlogout: -> 
      console.log 'logout' 

    </script> 

這是服務器代碼:

express = require 'express' 
passport = require 'passport' 
BrowserIDStrategy = require('passport-browserid').Strategy 
app = express() 

passport.serializeUser (user, done) -> 
    log 'serializing user #{user}' 
    done null, user.email 

passport.deserializeUser (email, done) -> 
    log 'deserializing email #{email}' 
    done null, email: email 

persona_audience = "http://#{config.server.host}:#{config.server.port}" 

passport.use new BrowserIDStrategy audience: persona_audience, 
    (email, done) -> 
    log email 
    done null, email: email 

app.get '/test', (req, res) -> 
    msg = if req.isAuthenticated() then "Congratulations! You've signed in as #{req.user.email}" else "Fail :((" 
    log msg 
    res.send msg 

app.post '/login', passport.authenticate('browserid', {failureRedirect: '/test', successRedirect: '/test'}) 

app.get '/logout', (req, res) -> 
    req.logOut() 
    res.redirect '/' 

app.configure -> 
# app.use express.logger() 
    app.use express.cookieParser(config.server.session_secret) 
    app.use express.session() 
    app.use express.bodyParser() 
    app.use express.methodOverride() 
    app.use passport.initialize() 
    app.use passport.session() 
    app.use app.router 
    app.use express['static'](__dirname + '/public') 
+0

錯誤是什麼? – chovy

+0

我總是碰到'log':('''在我的代碼中的行,即''req.isAuthenticated()'總是'false' – pathikrit

+0

此外,它從不碰到'serializeUser'或'deserializeUser'方法 – pathikrit

回答

2

app.configure之前app.login修復它。有人可以解釋發生了什麼嗎?

+2

我認爲這是因爲'passport.initialize'調用註冊護照中間件(調用'serializeUser'和'deserializeUser'的東西)'get'/''處理程序在中間件之前註冊,因此永遠不要調用序列化方法,我認爲這就是無論如何。 – Jacob

-2
data: assertion: assertion 

這不是上述有效的JSON:

 data: { assertion: assertion } 
+1

實際上它是 - CoffeeScript嘗試將該片段粘貼到http://www.js2coffee.org 此外,我確定它的工作原理是因爲'req.body.assertion'是我的服務器端郵件處理程序中的有效哈希,以便jquery ajax調用 – pathikrit