Skip to content

Commit bbd83ed

Browse files
author
Itai Levi
authored
[TIC-594] Allow an override for the back button on the account page (#24)
* feat(client): add redirectBackToUrl for redirect functions * fix(client): use regular encode * refactor(client): use options interface instead of extra param * 2.0.12
1 parent ea72f1d commit bbd83ed

File tree

4 files changed

+110
-40
lines changed

4 files changed

+110
-40
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.11",
8+
"version": "2.0.12",
99
"keywords": [
1010
"auth",
1111
"user",

src/client.ts

Lines changed: 97 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ const LOGGED_OUT_AT_KEY = "__PROPEL_AUTH_LOGGED_OUT_AT"
66
const AUTH_TOKEN_REFRESH_BEFORE_EXPIRATION_SECONDS = 10 * 60
77
const DEBOUNCE_DURATION_FOR_REFOCUS_SECONDS = 60
88

9+
const encodeBase64 = (str: string) => {
10+
const encode = window ? window.btoa : btoa
11+
return encode(str)
12+
}
13+
914
export interface RedirectToSignupOptions {
1015
postSignupRedirectUrl?: string
1116
userSignupQueryParameters?: Record<string, string>
@@ -16,6 +21,22 @@ export interface RedirectToLoginOptions {
1621
userSignupQueryParameters?: Record<string, string>
1722
}
1823

24+
export interface RedirectToAccountOptions {
25+
redirectBackToUrl?: string
26+
}
27+
28+
export interface RedirectToCreateOrgOptions {
29+
redirectBackToUrl?: string
30+
}
31+
32+
export interface RedirectToOrgPageOptions {
33+
redirectBackToUrl?: string
34+
}
35+
36+
export interface RedirectToSetupSAMLPageOptions {
37+
redirectBackToUrl?: string
38+
}
39+
1940
export interface IAuthClient {
2041
/**
2142
* If the user is logged in, this method returns an access token, the time (in seconds) that the token will expire,
@@ -48,18 +69,18 @@ export interface IAuthClient {
4869
/**
4970
* Gets the URL for the hosted account page.
5071
*/
51-
getAccountPageUrl(): string
72+
getAccountPageUrl(options?: RedirectToAccountOptions): string
5273

5374
/**
5475
* Gets the URL for the hosted organization page.
5576
* @param orgId The ID of the organization's page to load. If not specified, a random one will be used instead.
5677
*/
57-
getOrgPageUrl(orgId?: string): string
78+
getOrgPageUrl(orgId?: string, options?: RedirectToOrgPageOptions): string
5879

5980
/**
6081
* Gets the URL for the hosted create organization page.
6182
*/
62-
getCreateOrgPageUrl(): string
83+
getCreateOrgPageUrl(options?: RedirectToCreateOrgOptions): string
6384

6485
/**
6586
* Gets the URL for the hosted SAML configuration page.
@@ -79,18 +100,18 @@ export interface IAuthClient {
79100
/**
80101
* Redirects the user to the account page.
81102
*/
82-
redirectToAccountPage(): void
103+
redirectToAccountPage(options?: RedirectToAccountOptions): void
83104

84105
/**
85106
* Redirects the user to the organization page.
86107
* @param orgId The ID of the organization"s page to load. If not specified, a random one will be used instead.
87108
*/
88-
redirectToOrgPage(orgId?: string): void
109+
redirectToOrgPage(orgId?: string, options?: RedirectToOrgPageOptions): void
89110

90111
/**
91112
* Redirects the user to the create organization page.
92113
*/
93-
redirectToCreateOrgPage(): void
114+
redirectToCreateOrgPage(options?: RedirectToCreateOrgOptions): void
94115

95116
/**
96117
* Redirects the user to the SAML configuration page.
@@ -269,10 +290,9 @@ export function createClient(authOptions: IAuthOptions): IAuthClient {
269290
let qs = new URLSearchParams()
270291
let url = `${clientState.authUrl}/signup`
271292
if (options) {
272-
const encode = window ? window.btoa : btoa
273293
const { postSignupRedirectUrl, userSignupQueryParameters } = options
274294
if (postSignupRedirectUrl) {
275-
qs.set("rt", encode(postSignupRedirectUrl))
295+
qs.set("rt", encodeBase64(postSignupRedirectUrl))
276296
}
277297
if (userSignupQueryParameters) {
278298
Object.entries(userSignupQueryParameters).forEach(([key, value]) => {
@@ -290,10 +310,9 @@ export function createClient(authOptions: IAuthOptions): IAuthClient {
290310
let qs = new URLSearchParams()
291311
let url = `${clientState.authUrl}/login`
292312
if (options) {
293-
const encode = window ? window.btoa : btoa
294313
const { postLoginRedirectUrl, userSignupQueryParameters } = options
295314
if (postLoginRedirectUrl) {
296-
qs.set("rt", encode(postLoginRedirectUrl))
315+
qs.set("rt", encodeBase64(postLoginRedirectUrl))
297316
}
298317
if (userSignupQueryParameters) {
299318
Object.entries(userSignupQueryParameters).forEach(([key, value]) => {
@@ -307,24 +326,66 @@ export function createClient(authOptions: IAuthOptions): IAuthClient {
307326
return url
308327
}
309328

310-
const getAccountPageUrl = () => {
311-
return `${clientState.authUrl}/account`
329+
const getAccountPageUrl = (options?: RedirectToAccountOptions) => {
330+
let qs = new URLSearchParams()
331+
let url = `${clientState.authUrl}/account`
332+
if (options) {
333+
const { redirectBackToUrl } = options
334+
if (redirectBackToUrl) {
335+
qs.set("rt", encodeBase64(redirectBackToUrl))
336+
}
337+
}
338+
339+
if (qs.toString()) {
340+
url += `?${qs.toString()}`
341+
}
342+
return url
312343
}
313344

314-
const getOrgPageUrl = (orgId?: string) => {
345+
const getOrgPageUrl = (orgId?: string, options?: RedirectToOrgPageOptions) => {
346+
let qs = new URLSearchParams()
347+
let url = `${clientState.authUrl}/org`
315348
if (orgId) {
316-
return `${clientState.authUrl}/org?id=${orgId}`
317-
} else {
318-
return `${clientState.authUrl}/org`
349+
qs.set("id", orgId)
319350
}
351+
352+
if (options) {
353+
if (options.redirectBackToUrl) {
354+
qs.set("rt", encodeBase64(options.redirectBackToUrl))
355+
}
356+
}
357+
358+
if (qs.toString()) {
359+
url += `?${qs.toString()}`
360+
}
361+
return url
320362
}
321363

322-
const getCreateOrgPageUrl = () => {
323-
return `${clientState.authUrl}/create_org`
364+
const getCreateOrgPageUrl = (options?: RedirectToCreateOrgOptions) => {
365+
let qs = new URLSearchParams()
366+
let url = `${clientState.authUrl}/create_org`
367+
if (options) {
368+
const { redirectBackToUrl } = options
369+
if (redirectBackToUrl) {
370+
qs.set("rt", encodeBase64(redirectBackToUrl))
371+
}
372+
}
373+
if (qs.toString()) {
374+
url += `?${qs.toString()}`
375+
}
376+
return url
324377
}
325378

326-
const getSetupSAMLPageUrl = (orgId: string) => {
327-
return `${clientState.authUrl}/saml?id=${orgId}`
379+
const getSetupSAMLPageUrl = (orgId: string, options?: RedirectToSetupSAMLPageOptions) => {
380+
let qs = new URLSearchParams()
381+
if (options) {
382+
if (options.redirectBackToUrl) {
383+
qs.set("rt", encodeBase64(options.redirectBackToUrl))
384+
}
385+
}
386+
qs.set("id", orgId)
387+
388+
return `${clientState.authUrl}/saml?${qs.toString()}`
328389
}
329390

330391
const client = {
@@ -395,20 +456,20 @@ export function createClient(authOptions: IAuthOptions): IAuthClient {
395456
return getLoginPageUrl(options)
396457
},
397458

398-
getAccountPageUrl(): string {
399-
return getAccountPageUrl()
459+
getAccountPageUrl(options?: RedirectToAccountOptions): string {
460+
return getAccountPageUrl(options)
400461
},
401462

402-
getOrgPageUrl(orgId?: string): string {
403-
return getOrgPageUrl(orgId)
463+
getOrgPageUrl(orgId?: string, options?: RedirectToOrgPageOptions): string {
464+
return getOrgPageUrl(orgId, options)
404465
},
405466

406-
getCreateOrgPageUrl(): string {
407-
return getCreateOrgPageUrl()
467+
getCreateOrgPageUrl(options?: RedirectToCreateOrgOptions): string {
468+
return getCreateOrgPageUrl(options)
408469
},
409470

410-
getSetupSAMLPageUrl(orgId: string): string {
411-
return getSetupSAMLPageUrl(orgId)
471+
getSetupSAMLPageUrl(orgId: string, options?: RedirectToSetupSAMLPageOptions): string {
472+
return getSetupSAMLPageUrl(orgId, options)
412473
},
413474

414475
redirectToSignupPage(options?: RedirectToSignupOptions): void {
@@ -419,20 +480,20 @@ export function createClient(authOptions: IAuthOptions): IAuthClient {
419480
window.location.href = getLoginPageUrl(options)
420481
},
421482

422-
redirectToAccountPage(): void {
423-
window.location.href = getAccountPageUrl()
483+
redirectToAccountPage(options?: RedirectToAccountOptions): void {
484+
window.location.href = getAccountPageUrl(options)
424485
},
425486

426-
redirectToOrgPage(orgId?: string): void {
427-
window.location.href = getOrgPageUrl(orgId)
487+
redirectToOrgPage(orgId?: string, options?: RedirectToOrgPageOptions): void {
488+
window.location.href = getOrgPageUrl(orgId, options)
428489
},
429490

430-
redirectToCreateOrgPage(): void {
431-
window.location.href = getCreateOrgPageUrl()
491+
redirectToCreateOrgPage(options?: RedirectToCreateOrgOptions): void {
492+
window.location.href = getCreateOrgPageUrl(options)
432493
},
433494

434-
redirectToSetupSAMLPage(orgId: string) {
435-
window.location.href = getSetupSAMLPageUrl(orgId)
495+
redirectToSetupSAMLPage(orgId: string, options?: RedirectToSetupSAMLPageOptions) {
496+
window.location.href = getSetupSAMLPageUrl(orgId, options)
436497
},
437498

438499
async logout(redirectAfterLogout: boolean): Promise<void> {

src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
export type { AccessHelper, AccessHelperWithOrg } from "./access_helper"
22
export type { AuthenticationInfo, User } from "./api"
33
export { createClient } from "./client"
4-
export type { IAuthClient, IAuthOptions, RedirectToLoginOptions, RedirectToSignupOptions } from "./client"
4+
export type {
5+
IAuthClient,
6+
IAuthOptions,
7+
RedirectToAccountOptions,
8+
RedirectToCreateOrgOptions,
9+
RedirectToLoginOptions,
10+
RedirectToOrgPageOptions,
11+
RedirectToSetupSAMLPageOptions,
12+
RedirectToSignupOptions,
13+
} from "./client"
514
export { ACTIVE_ORG_ID_COOKIE_NAME } from "./cookies"
615
export { getActiveOrgId, setActiveOrgId } from "./org"
716
export type { OrgIdToOrgMemberInfo, OrgMemberInfo } from "./org"

0 commit comments

Comments
 (0)