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

Set cookie before sending the response if valid #5

Closed
Atinux opened this issue Jul 6, 2021 — with Volta.net · 4 comments
Closed

Set cookie before sending the response if valid #5

Atinux opened this issue Jul 6, 2021 — with Volta.net · 4 comments
Labels
enhancement New feature or request

Comments

Copy link
Member

Atinux commented Jul 6, 2021

This way we can use a second serverMiddleware to update the session before setting the cookie.

Example:

import { createApp } from 'h3'
import { listen } from 'listhen'
import { createAuthMiddleware } from 'ezpass'

const app = createApp()

app.useAsync(createAuthMiddleware({
  provider: 'github',
  sessionSecret: '...',
  providerOptions: {
    clientId: '...',
    clientSecret: '...'
  }
}))

app.useAsync((req) => {
  if (req.auth.session) {
    req.auth.session.visits = req.auth.session.visits || 0
    req.auth.session.visits++
  }
})
app.useAsync(req => `Welcome ${req.auth.session.user}, you visited ${req.auth.session.visits} times!`)

listen(app)
@Atinux Atinux added the enhancement New feature or request label Jul 6, 2021 — with Volta.net
Copy link
Member Author

Atinux commented Jul 6, 2021

One potential solution would be to overwrites res.send and res.end (see https://stackoverflow.com/questions/27885425/express-middleware-before-response-is-carried-out-to-client)

Copy link
Member Author

Atinux commented Jul 6, 2021

Or the possibility to inject a hook or middleware here:

This way, the end user can add another verification on top of the initial provider to either reject the auth or update the authRes.session to add some infos.

@pi0
Copy link
Member

pi0 commented Jul 7, 2021

Adding a hook seems good idea :)

@pi0 pi0 closed this as completed in 60e6658 Jul 7, 2021
@pi0
Copy link
Member

pi0 commented Jul 7, 2021

Added support for onAuthorize hook as option

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

No branches or pull requests

2 participants