Skip to content

Commit 62fce6f

Browse files
Add getAuthOptions to expose resolve options for client
1 parent b3355c1 commit 62fce6f

4 files changed

Lines changed: 64 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "git",
66
"url": "https://github.com/PropelAuth/javascript"
77
},
8-
"version": "2.0.23",
8+
"version": "2.0.24",
99
"keywords": [
1010
"auth",
1111
"user",

src/client.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ export interface IAuthClient {
163163
* Cleanup the auth client if you no longer need it.
164164
*/
165165
destroy(): void
166+
167+
/**
168+
* Returns the auth options with all default values filled in.
169+
*/
170+
getAuthOptions(): IResolvedAuthOptions
166171
}
167172

168173
export interface IAuthOptions {
@@ -195,12 +200,20 @@ export interface IAuthOptions {
195200
/**
196201
* If true, disables the token refresh on initial page load.
197202
* Can help reduce duplicate token refresh requests.
198-
*
203+
*
199204
* Default false
200205
*/
201206
skipInitialFetch?: boolean
202207
}
203208

209+
export interface IResolvedAuthOptions {
210+
authUrl: string
211+
enableBackgroundTokenRefresh: boolean
212+
minSecondsBeforeRefresh: number
213+
disableRefreshOnFocus: boolean
214+
skipInitialFetch: boolean
215+
}
216+
204217
interface AccessTokenActiveOrgMap {
205218
[orgId: string]: {
206219
accessToken: string
@@ -626,6 +639,16 @@ export function createClient(authOptions: IAuthOptions): IAuthClient {
626639
clearInterval(clientState.refreshInterval)
627640
}
628641
},
642+
643+
getAuthOptions(): IResolvedAuthOptions {
644+
return {
645+
authUrl: clientState.authUrl,
646+
enableBackgroundTokenRefresh: authOptions.enableBackgroundTokenRefresh!,
647+
minSecondsBeforeRefresh: minSecondsBeforeRefresh,
648+
disableRefreshOnFocus: authOptions.disableRefreshOnFocus ?? false,
649+
skipInitialFetch: authOptions.skipInitialFetch ?? false,
650+
}
651+
},
629652
}
630653

631654
const onStorageChange = async function () {

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type {
55
AccessTokenForActiveOrg,
66
IAuthClient,
77
IAuthOptions,
8+
IResolvedAuthOptions,
89
RedirectToAccountOptions,
910
RedirectToCreateOrgOptions,
1011
RedirectToLoginOptions,

src/tests/index.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,44 @@ afterAll(() => {
6969
jest.useRealTimers()
7070
})
7171

72+
test("getAuthOptions returns defaults when no options provided", () => {
73+
let client = createClient({ authUrl: "https://www.example.com", enableBackgroundTokenRefresh: false })
74+
75+
const options = client.getAuthOptions()
76+
77+
expect(options.authUrl).toBe("https://www.example.com")
78+
expect(options.enableBackgroundTokenRefresh).toBe(false)
79+
expect(options.minSecondsBeforeRefresh).toBe(120)
80+
expect(options.disableRefreshOnFocus).toBe(false)
81+
expect(options.skipInitialFetch).toBe(false)
82+
})
83+
84+
test("getAuthOptions returns user-provided values", () => {
85+
let client = createClient({
86+
authUrl: "https://www.example.com",
87+
enableBackgroundTokenRefresh: false,
88+
minSecondsBeforeRefresh: 300,
89+
disableRefreshOnFocus: true,
90+
skipInitialFetch: true,
91+
})
92+
93+
const options = client.getAuthOptions()
94+
95+
expect(options.authUrl).toBe("https://www.example.com")
96+
expect(options.enableBackgroundTokenRefresh).toBe(false)
97+
expect(options.minSecondsBeforeRefresh).toBe(300)
98+
expect(options.disableRefreshOnFocus).toBe(true)
99+
expect(options.skipInitialFetch).toBe(true)
100+
})
101+
102+
test("getAuthOptions returns normalized authUrl", () => {
103+
let client = createClient({ authUrl: "https://www.example.com/path/to/something", enableBackgroundTokenRefresh: false })
104+
105+
const options = client.getAuthOptions()
106+
107+
expect(options.authUrl).toBe("https://www.example.com")
108+
})
109+
72110
test("cannot create client without auth url origin", () => {
73111
expect(() => {
74112
createClient({ authUrl: "" })

0 commit comments

Comments
 (0)