mirror of
https://github.com/zhigang1992/devhub.git
synced 2026-06-18 03:58:44 +08:00
Fix oauth callback on mobile
This commit is contained in:
@@ -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(/([^?]+)[?]?(.*)/) || []
|
||||
|
||||
@@ -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',
|
||||
}),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user