3

我尝试编写一个骨架应用程序,使用教程护照文档使用Mozilla Persona进行身份验证,但登录总是失败 - 我不确定我做错了什么,因为我没有看到任何来自护照的日志。这是我的前端代码,这是我的服务器代码,我确实看到了 Persona 弹出窗口,但是当我从该弹出窗口登录时总是错误的。 req.isAuthenticated()

这是客户端代码的片段:

<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')
4

2 回答 2

2

app.configure之前的app.login修复它。有人可以解释发生了什么吗?

于 2012-10-12T02:26:13.600 回答
-2
    data: assertion: assertion

这不是上面的有效 json:

        data: { assertion: assertion }
于 2012-10-09T21:06:16.850 回答