Skip to content

feat: createPortalUrl #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/sdk/clients/browser/authcode-with-pkce.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BrowserSessionManager } from '../../session-managers/index.js';
import { AuthCodeWithPKCE } from '../../oauth2-flows/index.js';
import * as utilities from '../../utilities/index.js';
import type { GeneratePortalUrlParams } from '@kinde/js-utils';

import type {
UserType,
Expand Down Expand Up @@ -63,6 +64,18 @@ const createAuthCodeWithPKCEClient = (options: BrowserPKCEClientOptions) => {
});
};

/**
* Method makes use of the `createPortalUrl` method of the AuthCodeWithPKCE
* client above to return portal url.
* @param {GeneratePortalUrlParams} options
* @returns {Promise<{url: URL}>} portal URL
*/
const portal = async (options: GeneratePortalUrlParams): Promise<{ url: URL }> => {
return await client.createPortalUrl(sessionManager, {
...options,
});
};

/**
* Method makes use of the `handleRedirectFromAuthDomain` method of the
* `AuthCodeWithPKCE` client above to handle the redirection back to the app.
Expand Down Expand Up @@ -379,6 +392,7 @@ const createAuthCodeWithPKCEClient = (options: BrowserPKCEClientOptions) => {
getFlag,
logout,
login,
portal,
};
};

Expand Down
18 changes: 18 additions & 0 deletions lib/sdk/clients/server/authorization-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
} from '../types.js';

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

const createAuthorizationCodeClient = (
options: ACClientOptions,
Expand Down Expand Up @@ -73,6 +74,22 @@ const createAuthorizationCodeClient = (
});
};

/**
* Method makes use of the `createPortalUrl` method of the AuthCodeAbstract
* client above to return login url.
* @param {SessionManager} sessionManager
* @param {GeneratePortalUrlParams} options
* @returns {Promise<{ url: URL }>} required authorization URL
*/
const portal = async (
sessionManager: SessionManager,
options: GeneratePortalUrlParams
): Promise<{ url: URL }> => {
return await client.createPortalUrl(sessionManager, {
...options,
});
};

/**
* Method makes use of the `handleRedirectFromAuthDomain` method of the
* `AuthCodeAbstract` client above to handle the redirection back to the app.
Expand Down Expand Up @@ -175,6 +192,7 @@ const createAuthorizationCodeClient = (
getUser,
logout,
login,
portal,
};
};

Expand Down
3 changes: 3 additions & 0 deletions lib/sdk/clients/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export interface BrowserPKCEClientOptions extends AuthorizationCodeOptions {
sessionManager?: SessionManager;
}

export { PortalPage } from '@kinde/js-utils';
export type { GeneratePortalUrlParams } from '@kinde/js-utils';

export interface PKCEClientOptions extends AuthorizationCodeOptions {}
export interface CCClientOptions extends ClientCredentialsOptions {}
export interface ACClientOptions extends AuthorizationCodeOptions {
Expand Down
12 changes: 12 additions & 0 deletions lib/sdk/oauth2-flows/AuthCodeAbstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
} from './types.js';
import { createLocalJWKSet } from 'jose';
import { getRemoteJwks } from '../utilities/remote-jwks-cache.js';
import type { GeneratePortalUrlParams } from '@kinde/js-utils';

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

/**
* Abstract method mandates implementation of logic required for creating portal URL
* for accessing Kinde's portal interface, utilizing session data for authentication.
* @param {GeneratePortalUrlParams} options
* @returns {Promise<{url: URL}>} object containing the portal URL
*/
public abstract createPortalUrl(
sessionManager: SessionManager,
options: GeneratePortalUrlParams
): Promise<{ url: URL }>;

/**
* Abstract method will implement logic required for exchanging received auth code
* post user-authentication with authorization server to receive access, refresh
Expand Down
20 changes: 20 additions & 0 deletions lib/sdk/oauth2-flows/AuthCodeWithPKCE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import type {
AuthURLOptions,
AuthorizationCodeOptions,
} from './types.js';
import type { GeneratePortalUrlParams } from '@kinde/js-utils';
import { createPortalUrl } from '../utilities/createPortalUrl.js';

/**
* Class provides implementation for the authorization code with PKCE extension
Expand Down Expand Up @@ -67,6 +69,24 @@ export class AuthCodeWithPKCE extends AuthCodeAbstract {
return authURL;
}

/**
* Method provides implementation for `createPortalUrl` method mandated by
* `AuthCodeAbstract` parent class, see corresponding comment in parent class for
* further explanation.
* @param {SessionManager} sessionManager
* @param {Omit<GeneratePortalUrlParams, 'domain'>} options
* @returns {Promise<{url: URL}>} required authorization URL
*/
async createPortalUrl(
sessionManager: SessionManager,
options: Omit<GeneratePortalUrlParams, 'domain'>
): Promise<{ url: URL }> {
return await createPortalUrl(sessionManager, {
domain: this.config.authDomain,
...options,
});
}

/**
* Method provides implementation for `refreshTokens` method mandated by
* `AuthCodeAbstract` parent class, see corresponding comment in parent class for
Expand Down
20 changes: 20 additions & 0 deletions lib/sdk/oauth2-flows/AuthorizationCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type {
AuthorizationCodeOptions,
AuthURLOptions,
} from './types.js';
import type { GeneratePortalUrlParams } from '@kinde/js-utils';
import { createPortalUrl } from '../utilities/createPortalUrl.js';

/**
* Class provides implementation for the authorization code OAuth2.0 flow.
Expand Down Expand Up @@ -52,6 +54,24 @@ export class AuthorizationCode extends AuthCodeAbstract {
return authURL;
}

/**
* Method provides implementation for `createPortalUrl` method mandated by
* `AuthCodeAbstract` parent class, see corresponding comment in parent class for
* further explanation.
* @param {SessionManager} sessionManager
* @param {Omit<GeneratePortalUrlParams, 'domain'>} options
* @returns {Promise<{url: URL}>} required authorization URL
*/
async createPortalUrl(
sessionManager: SessionManager,
options: Omit<GeneratePortalUrlParams, 'domain'>
): Promise<{ url: URL }> {
return await createPortalUrl(sessionManager, {
domain: this.config.authDomain,
...options,
});
}

/**
* Method provides implementation for `refreshTokens` method mandated by
* `AuthCodeAbstract` parent class, see corresponding comment in parent class for
Expand Down
29 changes: 29 additions & 0 deletions lib/sdk/utilities/createPortalUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
generatePortalUrl,
MemoryStorage,
setActiveStorage,
StorageKeys,
} from '@kinde/js-utils';
import type { GeneratePortalUrlParams } from '@kinde/js-utils';
import { type SessionManager } from '../session-managers/index.js';

export const createPortalUrl = async (
sessionManager: SessionManager,
options: GeneratePortalUrlParams
): Promise<{ url: URL }> => {
const token = await sessionManager.getSessionItem('access_token'); // Ensure session is initialized

if (!token) {
throw new Error('No active session found.');
}

const storage = new MemoryStorage();
await storage.setSessionItem(StorageKeys.accessToken, token);
setActiveStorage(storage);

return await generatePortalUrl({
domain: options.domain,
returnUrl: options.returnUrl,
subNav: options.subNav,
});
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"typescript": "^5.0.4"
},
"dependencies": {
"@kinde/js-utils": "^0.18.1",
"jose": "^5.2.2",
"uncrypto": "^0.1.3"
},
Expand Down
Loading