Fix oauth callback on mobile

This commit is contained in:
Bruno Lemos
2018-11-20 16:33:48 -02:00
parent 0924745c66
commit 3e05cf03ea
2 changed files with 37 additions and 1 deletions

View File

@@ -1,6 +1,33 @@
const omit = require('lodash.omit')
const qs = require('qs')
exports.isLocalhost = host =>
host.indexOf('localhost') >= 0 ||
host.indexOf('0.0.0.0') >= 0 ||
host.indexOf('127.0.0.1') >= 0
exports.getCurrentHostURL = req =>
exports.isLocalhost(req.headers.host)
? `http://${req.headers.host.replace(/(0.0.0.0)|(127.0.0.1)/, 'localhost')}`
: `https://${req.headers.host}`
exports.getDefaultCallbackURL = req =>
`${exports.getCurrentHostURL(req)}/auth/github/callback`
exports.getCallbackURLWithQuery = (req, callbackUrl, query) => {
return exports.mergeQueryWithURL(
callbackUrl || exports.getDefaultCallbackURL(req),
omit(query || {}, [
'client_id',
'code',
'grant_type',
'redirect_uri',
'response_type',
'scope',
]),
)
}
exports.mergeQueryWithURL = (url, ...queryObjs) => {
const [, urlWithoutQuery, queryStringFromURL] =
(url || '').match(/([^?]+)[?]?(.*)/) || []

View File

@@ -1,7 +1,11 @@
const url = require('url')
const oauth = require('../../lib/oauth')
const { mergeQueryWithURL } = require('../../helpers')
const {
getCallbackURLWithQuery,
getDefaultCallbackURL,
mergeQueryWithURL,
} = require('../../helpers')
module.exports = (req, res) => {
req.query = url.parse(req.url, true).query
@@ -14,6 +18,11 @@ module.exports = (req, res) => {
},
Object.assign({}, req.query, {
client_id: process.env.GITHUB_CLIENT_ID,
redirect_uri: getCallbackURLWithQuery(
req,
req.query.redirect_uri || getDefaultCallbackURL(req),
req.query,
),
response_type: 'code',
}),
)