gpt4 book ai didi

node.js - PassportJS - 如何获取 session 对象

转载 作者:搜寻专家 更新时间:2023-11-01 00:33:45 26 4
gpt4 key购买 nike

我已经在我的 Node 应用程序中实现了 passportJS。登录后,我得到一个放置在 session 中的对象。这是对象:

{ loginStatus: 'SUCCESS',
orgRoles: { '7': 'ALL' },
orgNames: { '7': 'kianoshp@gmail.com' },
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': 'kianoshp@gmail.com' },
loginId: 4,
message: 'Success',
success: true }
deserializing { loginStatus: 'SUCCESS',
orgRoles: { '7': 'ALL' },
orgNames: { '7': 'kianoshp@gmail.com' },
loginId: 4,
message: 'Success',
success: true }

我希望能够访问我放置在 session 中的内容并从中获取值。例如,当我发出 GET 请求时,我希望能够执行以下操作:

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

非常感谢任何帮助!!

干杯,

基亚诺什

更新:我能够通过以下调用获得所需的数据:

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

输出如下:

{ loginStatus: 'SUCCESS',
orgRoles: { '7': 'ALL' },
orgNames: { '7': 'kianoshp@gmail.com' },
loginId: 4,
message: 'Success',
success: true }

这引出了一个问题,这是正确的吗?我是否必须深入研究请求对象才能获取此数据? “req.req.user”是否正确?

最佳答案

应通过req.user 检索用户对象。

您可以通过 req.isAuthenticated() 验证用户是否已登录,以避免处理未定义的 req.user。

关于node.js - PassportJS - 如何获取 session 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14243893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com