Skip to content

Commit

Permalink
feat: option to opt-out of sending scope with refresh request
Browse files Browse the repository at this point in the history
  • Loading branch information
soofstad committed Dec 13, 2023
1 parent e02085e commit 1c8deeb
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ type TAuthConfig = {
tokenExpiresIn?: number // default: null
// Can be used if auth provider doesn't return refresh token expiration time in token response
refreshTokenExpiresIn?: number // default: null
// Whether or not to post 'scope' when refreshing the access token
refreshWithScope?: boolean // default: true
}

```
Expand Down
2 changes: 2 additions & 0 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export type TAuthConfig = {
refreshTokenExpiresIn?: number
storage?: 'session' | 'local'
storageKeyPrefix?: string
refreshWithScope?: boolean
}

export type TRefreshTokenExpiredEvent = {
Expand Down Expand Up @@ -105,4 +106,5 @@ export type TInternalConfig = {
refreshTokenExpiresIn?: number
storage: 'session' | 'local'
storageKeyPrefix: string
refreshWithScope: boolean
}
2 changes: 2 additions & 0 deletions src/authConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function createInternalConfig(passedConfig: TAuthConfig): TInternalConfig
onRefreshTokenExpire = undefined,
storage = 'local',
storageKeyPrefix = 'ROCP_',
refreshWithScope = true,
}: TAuthConfig = passedConfig

const config: TInternalConfig = {
Expand All @@ -30,6 +31,7 @@ export function createInternalConfig(passedConfig: TAuthConfig): TInternalConfig
onRefreshTokenExpire: onRefreshTokenExpire,
storage: storage,
storageKeyPrefix: storageKeyPrefix,
refreshWithScope: refreshWithScope,
}
validateConfig(config)
return config
Expand Down
2 changes: 1 addition & 1 deletion src/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ export const fetchWithRefreshToken = (props: {
const refreshRequest: TTokenRequestForRefresh = {
grant_type: 'refresh_token',
refresh_token: refreshToken,
scope: config.scope,
client_id: config.clientId,
redirect_uri: config.redirectUri,
...config.extraTokenParameters,
}
if (config.refreshWithScope) refreshRequest.scope = config.scope
return postTokenRequest(config.tokenEndpoint, refreshRequest)
}

Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const authConfig = {
clearURL: true,
autoLogin: false,
storage: 'local',
refreshWithScope: false,
}

function LoginInfo() {
Expand All @@ -38,6 +39,10 @@ function LoginInfo() {
{token ? (
<>
<button onClick={() => logOut('rememberThis', idTokenData.tid)}>Logout</button>
<span style={{ margin: '0 10px' }}>
Access token will expire at:{' '}
{new Date(localStorage.getItem('ROCP_tokenExpire') * 1000).toLocaleTimeString()}
</span>
<div style={{ display: 'flex', flexWrap: 'wrap' }}>
<div>
<h4>Access Token (JWT)</h4>
Expand Down

0 comments on commit 1c8deeb

Please sign in to comment.