Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,38 @@ export function oauthLoginUrl (options: Options): Result {
? options.scopes.split(/[,\s]+/).filter(Boolean)
: Array.isArray(options.scopes) ? options.scopes : []

return {
const result: Result = {
allowSignup: options.allowSignup === false ? false : true,
clientId: options.clientId,
login: options.login || null,
redirectUrl: options.redirectUrl || null,
scopes: scopesNormalized,
state: options.state || Math.random().toString(36).substr(2),
url: `${BASE_URL}?client_id=${options.clientId}`
url: ''
}
result.url = urlBuilderAuthorize(BASE_URL, result)

return result
}
type ResultKeys = Exclude<keyof Result, 'url'>
function urlBuilderAuthorize (base: string, options: Result) {
const map = {
allowSignup: 'allow_signup',
clientId: 'client_id',
login: 'login',
redirectUrl: 'redirect_url',
scopes: 'scopes',
state: 'state',
}

let url = base

Object.entries(options).filter(([k, v]) => v !== null && k !== 'url') // Filter out keys that are null and remove the url key
.filter(([, v]) => Array.isArray(v) ? v.length !== 0 : true) // Filter out empty Array
.map(([key, ]) => [ map[key as ResultKeys], `${options[key as ResultKeys]!}` ]) // Map Array with the proper URL parameter names and change the value to a string using template strings
.forEach(([key, value], index) => { // Finally, build the URL
url += index === 0 ? `?` : '&'
url += `${key}=${value}`
})
return url
}
18 changes: 9 additions & 9 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('oauthLoginUrl({clientId: "1234567890abcdef1234"})', () => {
redirectUrl: null,
scopes: [],
state: '4feornbt361',
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&state=4feornbt361'
})
})

Expand All @@ -32,7 +32,7 @@ test('oauthLoginUrl({clientId: "4321fedcba0987654321"})', () => {
redirectUrl: null,
scopes: [],
state: '4feornbt361',
url: 'https://github.com/login/oauth/authorize?client_id=4321fedcba0987654321'
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=4321fedcba0987654321&state=4feornbt361'
})
})

Expand All @@ -47,7 +47,7 @@ test('redirectUrl option', () => {
redirectUrl: 'https://example.com',
scopes: [],
state: '4feornbt361',
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&redirect_url=https://example.com&state=4feornbt361'
})
})

Expand All @@ -62,7 +62,7 @@ test('login option', () => {
redirectUrl: null,
scopes: [],
state: '4feornbt361',
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361'
})
})

Expand All @@ -78,7 +78,7 @@ test('scopes = []', () => {
redirectUrl: null,
scopes: [],
state: '4feornbt361',
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361'
})
})

Expand All @@ -94,7 +94,7 @@ test('scopes = ""', () => {
redirectUrl: null,
scopes: [],
state: '4feornbt361',
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361'
})
})

Expand All @@ -110,7 +110,7 @@ test('scopes = "user,public_repo, gist notifications"', () => {
redirectUrl: null,
scopes: ['user', 'public_repo', 'gist', 'notifications'],
state: '4feornbt361',
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&scopes=user,public_repo,gist,notifications&state=4feornbt361'
})
})

Expand All @@ -127,7 +127,7 @@ test('allowSignup = false', () => {
redirectUrl: null,
scopes: ['user', 'public_repo', 'gist', 'notifications'],
state: '4feornbt361',
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
url: 'https://github.com/login/oauth/authorize?allow_signup=false&client_id=1234567890abcdef1234&login=octocat&scopes=user,public_repo,gist,notifications&state=4feornbt361'
})
})

Expand All @@ -143,6 +143,6 @@ test('state = Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI', () => {
redirectUrl: null,
scopes: [],
state: 'Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI',
url: 'https://github.com/login/oauth/authorize?client_id=1234567890abcdef1234'
url: 'https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI'
})
})