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

Ability to access the session data within a passport strategy #201

Closed
james2406 opened this issue Mar 3, 2021 · 2 comments · Fixed by blitz-js/blitz#2058
Closed

Ability to access the session data within a passport strategy #201

james2406 opened this issue Mar 3, 2021 · 2 comments · Fixed by blitz-js/blitz#2058
Assignees
Labels

Comments

@james2406
Copy link

james2406 commented Mar 3, 2021

What do you want and why?

I would like to be able to access the session data inside the verify callback of the passport strategy.

This session data is set on an anonymous session via the ctx.session.$setPublicData() and ctx.session.$setPrivateData() functions.

Possible implementation(s)

Potentially, the passportAuth function could accept a callback which would provide the ctx.session, like so:

export default passportAuth((ctx) => ({
  successRedirectUrl: "/",
  errorRedirectUrl: "/",
  strategies: [
    {
      strategy: new TwitterStrategy(
        {
          consumerKey: process.env.TWITTER_CONSUMER_KEY,
          //...
        },
        async function (_token, _tokenSecret, profile, done) {
          const email = profile.emails && profile.emails[0]?.value

          // WANT: access ctx.session.publicData here
 
          const user = await db.user.upsert({
            where: {email},
            create: {
              email,
              name: profile.displayName,
            },
            update: {email},
          })

          const publicData = {/* ... */}
          done(null, {publicData})
        },
      ),
    },
    },
  ],
}))
@james2406 james2406 changed the title Ability to access data on the session within a passport strategy Ability to access the session data within a passport strategy Mar 4, 2021
@flybayer
Copy link
Member

flybayer commented Mar 4, 2021

Sweet, thanks! Great idea.

This change needs to be made in the passport-adapter.ts file. We'll need to run global middleware at start of function instead of the end, we can access ctx.

So something like this:

+   // First run global middleware so we can access session
+   const globalMiddleware = getAllMiddlewareForModule({} as any)
+   await handleRequestWithMiddleware(req, res, globalMiddleware)

+   // pseudo code: if passportAuth config is callback, call it with `res.blitzCtx` as argument
 
    // rest of passport code

-    const globalMiddleware = getAllMiddlewareForModule({} as any)
-    await handleRequestWithMiddleware(req, res, [...globalMiddleware, ...middleware])
+    // Now only run the specific passport middleware that we constructed
+    await handleRequestWithMiddleware(req, res, middleware)

@james2406
Copy link
Author

Okay. This makes a lot of sense! I'm happy to take a go at this change and submit a PR.

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

Successfully merging a pull request may close this issue.

2 participants