Skip to content

Commit

Permalink
fix: make scope optional and don't send it if undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianvitterso authored and soofstad committed Jun 19, 2023
1 parent 06e3c9f commit 59f0cb7
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ReactNode } from 'react'

interface TTokenRqBase {
grant_type: string
scope: string
scope?: string
client_id: string
redirect_uri: string
}
Expand Down Expand Up @@ -85,7 +85,7 @@ export type TInternalConfig = {
authorizationEndpoint: string
tokenEndpoint: string
redirectUri: string
scope: string
scope?: string
state?: string
logoutEndpoint?: string
logoutRedirect?: string
Expand Down
2 changes: 1 addition & 1 deletion src/authConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function createInternalConfig(passedConfig: TAuthConfig): TInternalConfig
autoLogin = true,
clearURL = true,
decodeToken = true,
scope = '',
scope = undefined,
preLogin = () => null,
postLogin = () => null,
onRefreshTokenExpire = undefined,
Expand Down
5 changes: 4 additions & 1 deletion src/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ export async function redirectToLogin(config: TInternalConfig, customState?: str
const params = new URLSearchParams({
response_type: 'code',
client_id: config.clientId,
scope: config.scope,
redirect_uri: config.redirectUri,
code_challenge: codeChallenge,
code_challenge_method: 'S256',
...config.extraAuthParameters,
})

if (config.scope !== undefined) {
params.append('scope', config.scope)
}

sessionStorage.removeItem(stateStorageKey)
const state = customState ?? config.state
if (state) {
Expand Down
2 changes: 1 addition & 1 deletion tests/login.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('First page visit should redirect to auth provider for login', async () =>
await waitFor(() => {
expect(window.location.replace).toHaveBeenCalledWith(
expect.stringMatching(
/^myAuthEndpoint\?response_type=code&client_id=myClientID&scope=someScope\+openid&redirect_uri=http%3A%2F%2Flocalhost%2F&code_challenge=.{43}&code_challenge_method=S256&state=testState/gm
/^myAuthEndpoint\?response_type=code&client_id=myClientID&redirect_uri=http%3A%2F%2Flocalhost%2F&code_challenge=.{43}&code_challenge_method=S256&scope=someScope\+openid&state=testState/gm
)
)
})
Expand Down

0 comments on commit 59f0cb7

Please sign in to comment.