diff --git a/src/index.ts b/src/index.ts index 8911fdc..5b5b651 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 +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 } diff --git a/test/index.test.ts b/test/index.test.ts index 22526e0..26afa66 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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' }) }) @@ -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' }) }) @@ -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' }) }) @@ -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' }) }) @@ -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' }) }) @@ -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' }) }) @@ -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' }) }) @@ -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' }) }) @@ -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' }) })