2013-01-09 48 views
1

我在我的節點應用程序中實現了passportJS。登錄後,我收到了一個我在會話中放置的對象。這是對象:PassportJS - 如何獲取會話對象

{ loginStatus: 'SUCCESS', 
    orgRoles: { '7': 'ALL' }, 
    orgNames: { '7': '[email protected]' }, 
    loginId: 4, 
    message: 'Success', 
    success: true } 

這是護照配置(在CoffeeScript中):

passport.serializeUser (user, done) -> 
    console.log 'serializing', user 
    done null, user 

passport.deserializeUser (user, done) -> 
    console.log 'deserializing', user 
    done null, user 

#passport LocalStrategy 
passport.use new LocalStrategy 
    usernameField: 'email' 
    passwordField: 'password' 
    , 
    (username, password, done) -> 
     validateUser username, password, done 



validateUser = (username, password, done) -> 

    console.log 'loginName is --> ' + username + ' password --> ' + password 
    options = 
     protocol: API_PROTOCOL 
     host: API_HOST 
     pathname: '/appservices/login' 

    reqURL = url.format options 
    requestOptions = 
     headers: 
      'Content-type': 'application/json; charset=utf-8' 
     body: JSON.stringify 
      loginName: username 
      password: password 
     method: 'POST' 

    request reqURL, requestOptions, (error, response, body) -> 
     console.log 'body is --> ' + body 
     resObj = JSON.parse body.toString() 

     console.log 'loginStatus is --> ' + resObj.loginStatus 

     if resObj.loginStatus is 'FAIL' or resObj.loginStatus is 'INVALID_PASSWORD' or resObj.loginStatus is 'LOGIN_NOT_FOUND' 
      return done null, false, 
       message: 'We couldn\'t find that email/password, please try again.' 
     if resObj.loginStatus is 'ACCOUNT_LOCKED' 
      return done null, false, 
       message: 'The user has been locked out due to too many unsuccessful login attempts or the admin has locked the account. Please contact admin or customer support.' 
     if resObj.loginStatus is 'NO_LOGIN_DOMAIN_AVAILABLE' 
      return done null, false, 
       message: 'The login attempt is valid but there is no registered domain that the login can access. Please contact admin or customer support.' 
     if resObj.loginStatus isnt 'SUCCESS' 
      return done null, false, 
       message: 'The login attempt has failed. If you feel this is an error please try again or contact admin/customer support.' 

     done null, resObj 

app.configure -> 
    #app.use express.logger() 
    app.use express.static(__dirname + '/public') 
    app.use express.cookieParser() 
    app.use express.bodyParser() 
    app.use express.methodOverride() 
    app.use express.session 
     secret: '###########' 
     store: new RedisStore() 
     cookie: 
      maxAge: 3600000 
    app.use flash() 
    app.use passport.initialize() 
    app.use passport.session() 
    app.set 'view engine', 'jade' 
    app.set 'views', __dirname + '/views' 
    app.use app.router 

輸出我看到:

serializing { loginStatus: 'SUCCESS', 
    orgRoles: { '7': 'ALL' }, 
    orgNames: { '7': '[email protected]' }, 
    loginId: 4, 
    message: 'Success', 
    success: true } 
deserializing { loginStatus: 'SUCCESS', 
    orgRoles: { '7': 'ALL' }, 
    orgNames: { '7': '[email protected]' }, 
    loginId: 4, 
    message: 'Success', 
    success: true } 

我希望能夠訪問我放置了會話並從中獲取值。例如,當我發出GET請求時,我希望能夠執行以下操作:

app.get '/threatStatus', (res, req) -> 
    #I would like to retrieve the loginId for the user that I placed in the session 

任何幫助都非常感謝!

乾杯,

Kianosh

更新: 我能得到我需要通過進行下面的調用數據:

app.get '/threatStatus', (res, req) -> 
    console.log req.req.user 

,它輸出以下內容:

{ loginStatus: 'SUCCESS', 
    orgRoles: { '7': 'ALL' }, 
    orgNames: { '7': '[email protected]' }, 
    loginId: 4, 
    message: 'Success', 
    success: true } 

哪一個問題,這是正確的?我是否需要深入探索請求對象才能獲取這些數據? 'req.req.user'是否正確?

回答

4

應通過req.user檢索用戶對象。

您可以驗證用戶是否通過req.isAuthenticated()登錄,以避免處理未定義的req.user。