-
Notifications
You must be signed in to change notification settings - Fork 257
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
Allow to customize "missing provider" error URL? #296
Comments
Hi @endel, Thanks for your continued interest in using Grant, I really appreciate that. The Here is how a potential implementation may look like for Express: var Grant = require('../../').express()
var grant = Grant(require('./config.json'))
express()
// ...
.use('/connect/:provider', (req, res, next) => {
Object.keys(grant.config).includes(req.params.provider)
? next()
: res.status(404).send('Not found')
})
.use(grant)
// ...
.listen(3000) Similar implementation can be achieved for any of the provided handlers. If you want to handle that error in some other way, other than a In the end whether you want a redirect to some other route or simply responding with a
Now when thinking about it again I don't know why the author of that thread considered having a middleware in front of Grant being a bit of a hack, so that I had to come up with that really weird implementation instead. There are many other cases where you may want to augment the Grant middleware with a little bit of code, for example recently we had the case of handling alternating domains, but even for builtin features that is required in some cases, like dynamic state overrides and the state transport itself. Let me know if I am missing something about your setup in particular. |
I spent a bit more time thinking about this and I wanted to elaborate a little bit more on what are we trying to fix with this proposed feature. With this proposed feature we are trying to solve the issue of redirecting to the root This covers:
To fix 1. we can add a simple guard middleware before Grant and handle it accordingly: .use('/connect/:provider', (req, res, next) => {
Object.keys(grant.config).includes(req.params.provider)
? next()
: res.status(404).send('Not found')
}) To fix 2. we can define a default {
"defaults": {
"callback": "/default"
}
} With this setup:
There is one more case to cover and that is someone accessing the .use('/connect/:provider/callback', (req, res, next) => {
Object.keys(grant.config).includes(req?.session?.grant?.provider)
? next()
: res.status(404).send('Not found')
}) Note that Here is the full example: {
"defaults": {
"origin": "http://localhost:3000",
"transport": "querystring",
"callback": "/default"
},
"google": {
"key": "APP_ID",
"secret": "APP_SECRET",
"scope": [
"openid"
]
}
} var express = require('express')
var session = require('express-session')
var Grant = require('../../').express()
var grant = Grant(require('./config.json'))
express()
.use(session({secret: 'grant', saveUninitialized: true, resave: false}))
.use('/connect/:provider', (req, res, next) => {
Object.keys(grant.config).includes(req.params.provider)
? next()
: res.status(404).send('Not found')
})
.use('/connect/:provider/callback', (req, res, next) => {
Object.keys(grant.config).includes(req?.session?.grant?.provider)
? next()
: res.status(404).send('Not found')
})
.use(grant)
.get('/default', (req, res) => {
res.end(JSON.stringify(req.query, null, 2))
})
.listen(3000) Navigating to:
To sum it up:
@endel let me know what you think |
Thank you SO much for the detailed response - your first suggestion was exactly what I was looking for. The further advice is also immensely helpful 💙 |
Hi @simov,
I would like to be able to customize the default response location if a provider hasn't been configured yet. Currently, it is hardcoded to
"/"
:grant/lib/response.js
Line 108 in c220e9e
The reasoning is: I'd like to provide a friendly message when a provider wasn't configured, instead of showing this:
I'd like to send a PR to allow customizing this, would you mind pointing me a preferred direction to allow such customization?
(Thank you for your work again 🙏
grant
has been in my toolbelt for years, and it's really great!)The text was updated successfully, but these errors were encountered: