Skip to content

Commit e358d4c

Browse files
committed
feat: createPortalUrl
1 parent a11d0ec commit e358d4c

File tree

10 files changed

+5032
-3504
lines changed

10 files changed

+5032
-3504
lines changed

lib/sdk/clients/browser/authcode-with-pkce.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BrowserSessionManager } from '../../session-managers/index.js';
22
import { AuthCodeWithPKCE } from '../../oauth2-flows/index.js';
33
import * as utilities from '../../utilities/index.js';
4+
import type { GeneratePortalUrlParams } from '@kinde/js-utils';
45

56
import type {
67
UserType,
@@ -63,6 +64,18 @@ const createAuthCodeWithPKCEClient = (options: BrowserPKCEClientOptions) => {
6364
});
6465
};
6566

67+
/**
68+
* Method makes use of the `createPortalUrl` method of the AuthCodeWithPKCE
69+
* client above to return portal url.
70+
* @param {GeneratePortalUrlParams} options
71+
* @returns {Promise<{url: URL}>} portal URL
72+
*/
73+
const portal = async (options: GeneratePortalUrlParams): Promise<{ url: URL }> => {
74+
return await client.createPortalUrl(sessionManager, {
75+
...options,
76+
});
77+
};
78+
6679
/**
6780
* Method makes use of the `handleRedirectFromAuthDomain` method of the
6881
* `AuthCodeWithPKCE` client above to handle the redirection back to the app.
@@ -379,6 +392,7 @@ const createAuthCodeWithPKCEClient = (options: BrowserPKCEClientOptions) => {
379392
getFlag,
380393
logout,
381394
login,
395+
portal,
382396
};
383397
};
384398

lib/sdk/clients/server/authorization-code.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
} from '../types.js';
1313

1414
import type { OAuth2CodeExchangeResponse } from '../../oauth2-flows/types.js';
15+
import type { GeneratePortalUrlParams } from '@kinde/js-utils';
1516

1617
const createAuthorizationCodeClient = (
1718
options: ACClientOptions,
@@ -73,6 +74,22 @@ const createAuthorizationCodeClient = (
7374
});
7475
};
7576

77+
/**
78+
* Method makes use of the `createPortalUrl` method of the AuthCodeAbstract
79+
* client above to return login url.
80+
* @param {SessionManager} sessionManager
81+
* @param {GeneratePortalUrlParams} options
82+
* @returns {Promise<{ url: URL }>} required authorization URL
83+
*/
84+
const portal = async (
85+
sessionManager: SessionManager,
86+
options: GeneratePortalUrlParams
87+
): Promise<{ url: URL }> => {
88+
return await client.createPortalUrl(sessionManager, {
89+
...options,
90+
});
91+
};
92+
7693
/**
7794
* Method makes use of the `handleRedirectFromAuthDomain` method of the
7895
* `AuthCodeAbstract` client above to handle the redirection back to the app.
@@ -175,6 +192,7 @@ const createAuthorizationCodeClient = (
175192
getUser,
176193
logout,
177194
login,
195+
portal,
178196
};
179197
};
180198

lib/sdk/clients/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export interface BrowserPKCEClientOptions extends AuthorizationCodeOptions {
1212
sessionManager?: SessionManager;
1313
}
1414

15+
export { PortalPage } from '@kinde/js-utils';
16+
export type { GeneratePortalUrlParams } from '@kinde/js-utils';
17+
1518
export interface PKCEClientOptions extends AuthorizationCodeOptions {}
1619
export interface CCClientOptions extends ClientCredentialsOptions {}
1720
export interface ACClientOptions extends AuthorizationCodeOptions {

lib/sdk/oauth2-flows/AuthCodeAbstract.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
} from './types.js';
1414
import { createLocalJWKSet } from 'jose';
1515
import { getRemoteJwks } from '../utilities/remote-jwks-cache.js';
16+
import type { GeneratePortalUrlParams } from '@kinde/js-utils';
1617

1718
/**
1819
* Abstract class provides contract (methods) for classes implementing OAuth2.0 flows
@@ -71,6 +72,17 @@ export abstract class AuthCodeAbstract {
7172
options: AuthURLOptions
7273
): Promise<URL>;
7374

75+
/**
76+
* Abstract method mandates implementation of logic required for creating portal URL
77+
* for accessing Kinde's portal interface, utilizing session data for authentication.
78+
* @param {GeneratePortalUrlParams} options
79+
* @returns {Promise<{url: URL}>} object containing the portal URL
80+
*/
81+
public abstract createPortalUrl(
82+
sessionManager: SessionManager,
83+
options: GeneratePortalUrlParams
84+
): Promise<{ url: URL }>;
85+
7486
/**
7587
* Abstract method will implement logic required for exchanging received auth code
7688
* post user-authentication with authorization server to receive access, refresh

lib/sdk/oauth2-flows/AuthCodeWithPKCE.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import type {
1212
AuthURLOptions,
1313
AuthorizationCodeOptions,
1414
} from './types.js';
15+
import type { GeneratePortalUrlParams } from '@kinde/js-utils';
16+
import { createPortalUrl } from '../utilities/createPortalUrl.js';
1517

1618
/**
1719
* Class provides implementation for the authorization code with PKCE extension
@@ -67,6 +69,24 @@ export class AuthCodeWithPKCE extends AuthCodeAbstract {
6769
return authURL;
6870
}
6971

72+
/**
73+
* Method provides implementation for `createPortalUrl` method mandated by
74+
* `AuthCodeAbstract` parent class, see corresponding comment in parent class for
75+
* further explanation.
76+
* @param {SessionManager} sessionManager
77+
* @param {Omit<GeneratePortalUrlParams, 'domain'>} options
78+
* @returns {Promise<{url: URL}>} required authorization URL
79+
*/
80+
async createPortalUrl(
81+
sessionManager: SessionManager,
82+
options: Omit<GeneratePortalUrlParams, 'domain'>
83+
): Promise<{ url: URL }> {
84+
return await createPortalUrl(sessionManager, {
85+
domain: this.config.authDomain,
86+
...options,
87+
});
88+
}
89+
7090
/**
7191
* Method provides implementation for `refreshTokens` method mandated by
7292
* `AuthCodeAbstract` parent class, see corresponding comment in parent class for

lib/sdk/oauth2-flows/AuthorizationCode.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import type {
77
AuthorizationCodeOptions,
88
AuthURLOptions,
99
} from './types.js';
10+
import type { GeneratePortalUrlParams } from '@kinde/js-utils';
11+
import { createPortalUrl } from '../utilities/createPortalUrl.js';
1012

1113
/**
1214
* Class provides implementation for the authorization code OAuth2.0 flow.
@@ -52,6 +54,24 @@ export class AuthorizationCode extends AuthCodeAbstract {
5254
return authURL;
5355
}
5456

57+
/**
58+
* Method provides implementation for `createPortalUrl` method mandated by
59+
* `AuthCodeAbstract` parent class, see corresponding comment in parent class for
60+
* further explanation.
61+
* @param {SessionManager} sessionManager
62+
* @param {Omit<GeneratePortalUrlParams, 'domain'>} options
63+
* @returns {Promise<{url: URL}>} required authorization URL
64+
*/
65+
async createPortalUrl(
66+
sessionManager: SessionManager,
67+
options: Omit<GeneratePortalUrlParams, 'domain'>
68+
): Promise<{ url: URL }> {
69+
return await createPortalUrl(sessionManager, {
70+
domain: this.config.authDomain,
71+
...options,
72+
});
73+
}
74+
5575
/**
5676
* Method provides implementation for `refreshTokens` method mandated by
5777
* `AuthCodeAbstract` parent class, see corresponding comment in parent class for

lib/sdk/utilities/createPortalUrl.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {
2+
generatePortalUrl,
3+
MemoryStorage,
4+
setActiveStorage,
5+
StorageKeys,
6+
} from '@kinde/js-utils';
7+
import type { GeneratePortalUrlParams } from '@kinde/js-utils';
8+
import { type SessionManager } from '../session-managers/index.js';
9+
10+
export const createPortalUrl = async (
11+
sessionManager: SessionManager,
12+
options: GeneratePortalUrlParams
13+
): Promise<{ url: URL }> => {
14+
const token = await sessionManager.getSessionItem('access_token'); // Ensure session is initialized
15+
16+
if (!token) {
17+
throw new Error('No active session found.');
18+
}
19+
20+
const storage = new MemoryStorage();
21+
await storage.setSessionItem(StorageKeys.accessToken, token);
22+
setActiveStorage(storage);
23+
24+
return await generatePortalUrl({
25+
domain: options.domain,
26+
returnUrl: options.returnUrl,
27+
subNav: options.subNav,
28+
});
29+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"typescript": "^5.0.4"
7070
},
7171
"dependencies": {
72+
"@kinde/js-utils": "^0.18.1",
7273
"jose": "^5.2.2",
7374
"uncrypto": "^0.1.3"
7475
},

0 commit comments

Comments
 (0)