-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgetGithubAuthorizeUrl.js
More file actions
38 lines (29 loc) · 1.05 KB
/
getGithubAuthorizeUrl.js
File metadata and controls
38 lines (29 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const GITHUB_URL = process.env.GITHUB_URL || 'https://github.com'
const githubAuthorizeUrl = `${GITHUB_URL}/login/oauth/authorize`
const queryStringFromObj = queryObj =>
Object.keys(queryObj)
.filter(key => queryObj[key] !== undefined)
.map(key => `${key}=${queryObj[key]}`)
.join('&')
const getRedirectUri = (githubClientId, afterSignInUrl) => {
if (!process.browser) {
return
}
let afterAuthUrl = `${window.location.origin}/sign-in`
if (afterSignInUrl && afterSignInUrl !== '/sign-in') {
afterAuthUrl = `${afterAuthUrl}?afterSignInUrl=${afterSignInUrl}`
}
return encodeURIComponent(afterAuthUrl)
}
const getGithubAuthorizeUrl = (githubClientId, githubScope, afterSignInUrl) => {
if (!githubClientId) {
throw new Error('Client id is not defined')
}
const githubAuthorizeParams = queryStringFromObj({
client_id: githubClientId,
redirect_uri: getRedirectUri(githubClientId, afterSignInUrl),
scope: githubScope
})
return `${githubAuthorizeUrl}?${githubAuthorizeParams}`
}
export default getGithubAuthorizeUrl