Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

userProperty option doesn't work #192

Open
ViacheslavBinetskyiMackiev opened this issue Nov 14, 2023 · 1 comment
Open

userProperty option doesn't work #192

ViacheslavBinetskyiMackiev opened this issue Nov 14, 2023 · 1 comment

Comments

@ViacheslavBinetskyiMackiev
Copy link

ViacheslavBinetskyiMackiev commented Nov 14, 2023

I tried to change userProperty in initialize function but in but userProperty still 'user' and there is no custom userProperty in ctx.state. I forked repo and pass userProperty in _initialize function of passport so its triggers userProperty change but doesn't set this property on ctx.state so there is no user. Any thoughts? In v3 branch version userProperty changes and its use passport version 0.0.4. But in main with passport 0.0.6 i receive this behaviour.

@Ianc22
Copy link

Ianc22 commented Jun 13, 2024

Yeah very frustrating

function initialize(passport) {
  const middleware = promisify(_initialize(passport))
  return function passportInitialize(ctx, next) {
    // koa <-> connect compatibility:
    const userProperty = passport._userProperty || 'user'
    // check ctx.req has the userProperty
    if (!ctx.req.hasOwnProperty(userProperty)) {
      Object.defineProperty(ctx.req, userProperty, {
        enumerable: true,
        get: function() {
          return ctx.state[userProperty]
        },
        set: function(val) {
          ctx.state[userProperty] = val
        }
      })
    }

    // create mock object for express' req object
    const req = createReqMock(ctx, userProperty)


    // add Promise-based login method
    const login  = req.login
    ctx.login = ctx.logIn = function(user, options) {
      return new Promise((resolve, reject) => {
        login.call(req, user, options, err => {
          if (err) reject(err)
          else resolve()
        })
      })
    }

    // add Promise-based logout method
    const logout  = req.logout
    ctx.logout = ctx.logOut = function(options) {
      return new Promise((resolve, reject) => {
        logout.call(req, options, err => {
          if (err) reject(err)
          else resolve()
        })
      })
    }

    // add aliases for passport's request extensions to Koa's context
    ctx.isAuthenticated     = req.isAuthenticated.bind(req)
    ctx.isUnauthenticated   = req.isUnauthenticated.bind(req)

    return middleware(req, ctx).then(function() {
      return next()
    })
  }
}

Here not passing options for a start

function initialize(passport) {
const middleware = promisify(_initialize(passport))
}}

function initialize(passport, options) {
const middleware = promisify(_initialize(passport, options))
}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants