Skip to content
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
7 changes: 7 additions & 0 deletions .changeset/some-moons-show.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@asgardeo/javascript': patch
'@asgardeo/nextjs': patch
'@asgardeo/react': patch
---

Expose `getAccessToken` & `exchangeToken` from `useAsgardeo`
4 changes: 3 additions & 1 deletion packages/javascript/src/AsgardeoJavaScriptClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {Config, SignInOptions, SignOutOptions, SignUpOptions} from './models/con
import {Storage} from './models/store';
import {EmbeddedFlowExecuteRequestPayload, EmbeddedFlowExecuteResponse} from './models/embedded-flow';
import {EmbeddedSignInFlowHandleRequestPayload} from './models/embedded-signin-flow';
import {TokenResponse} from './models/token';
import {TokenExchangeRequestConfig, TokenResponse} from './models/token';
import {Organization} from './models/organization';
import {User, UserProfile} from './models/user';

Expand Down Expand Up @@ -55,6 +55,8 @@ abstract class AsgardeoJavaScriptClient<T = Config> implements AsgardeoClient<T>

abstract getConfiguration(): T;

abstract exchangeToken(config: TokenExchangeRequestConfig, sessionId?: string): Promise<TokenResponse | Response>;

abstract signIn(
options?: SignInOptions,
sessionId?: string,
Expand Down
9 changes: 8 additions & 1 deletion packages/javascript/src/models/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import {EmbeddedSignInFlowHandleRequestPayload} from './embedded-signin-flow';
import {Organization} from './organization';
import {User, UserProfile} from './user';
import {TokenResponse} from './token';
import {TokenExchangeRequestConfig, TokenResponse} from './token';
import {Storage} from './store';
import {SignInOptions, SignOutOptions, SignUpOptions} from './config';

Expand Down Expand Up @@ -65,6 +65,13 @@ export interface AsgardeoClient<T> {

getConfiguration(): T;

/**
* Swaps the current access token with a new one based on the provided configuration (with a grant type).
* @param config - Configuration for the token exchange request.
* @param sessionId - Optional session ID to be used for the token exchange.
*/
exchangeToken(config: TokenExchangeRequestConfig, sessionId?: string): Promise<TokenResponse | Response>;

updateUserProfile(payload: any, userId?: string): Promise<User>;

/**
Expand Down
6 changes: 5 additions & 1 deletion packages/nextjs/src/AsgardeoNextClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
extractUserClaimsFromIdToken,
TokenResponse,
Storage,
TokenExchangeRequestConfig,
} from '@asgardeo/node';
import {AsgardeoNextConfig} from './models/config';
import getSessionId from './server/actions/getSessionId';
Expand Down Expand Up @@ -381,6 +382,10 @@ class AsgardeoNextClient<T extends AsgardeoNextConfig = AsgardeoNextConfig> exte
return this.asgardeo.isSignedIn(sessionId as string);
}

override exchangeToken(config: TokenExchangeRequestConfig, sessionId?: string): Promise<TokenResponse | Response> {
return this.asgardeo.exchangeToken(config, sessionId);
}

/**
* Gets the access token from the session cookie if no sessionId is provided,
* otherwise falls back to legacy client method.
Expand All @@ -390,7 +395,6 @@ class AsgardeoNextClient<T extends AsgardeoNextConfig = AsgardeoNextConfig> exte
const token = await getAccessToken();

if (typeof token !== 'string' || !token) {
throw new Error('Access token not found');
throw new AsgardeoRuntimeError(
'Failed to get access token.',
'AsgardeoNextClient-getAccessToken-RuntimeError-003',
Expand Down
11 changes: 9 additions & 2 deletions packages/nextjs/src/server/asgardeo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,29 @@
* under the License.
*/

import {TokenExchangeRequestConfig} from '@asgardeo/node';
import AsgardeoNextClient from '../AsgardeoNextClient';
import getSessionIdAction from './actions/getSessionId';

const asgardeo = async () => {
const getAccessToken = async (id: string) => {
const getAccessToken = async (sessionId: string) => {
const client: AsgardeoNextClient = AsgardeoNextClient.getInstance();
return await client.getAccessToken(id);
return await client.getAccessToken(sessionId);
};

const getSessionId = async () => {
return await getSessionIdAction();
};

const exchangeToken = async (config: TokenExchangeRequestConfig, sessionId: string) => {
const client: AsgardeoNextClient = AsgardeoNextClient.getInstance();
return await client.exchangeToken(config, sessionId);
};

return {
getAccessToken,
getSessionId,
exchangeToken,
};
};

Expand Down
10 changes: 10 additions & 0 deletions packages/react/src/AsgardeoReactClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
HttpRequestConfig,
HttpResponse,
Storage,
TokenExchangeRequestConfig,
} from '@asgardeo/browser';
import AuthAPI from './__temp__/api';
import getMeOrganizations from './api/getMeOrganizations';
Expand Down Expand Up @@ -271,6 +272,15 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
return this.asgardeo.getConfigData() as unknown as T;
}

override async exchangeToken(
config: TokenExchangeRequestConfig,
sessionId?: string,
): Promise<TokenResponse | Response> {
return this.withLoading(async () => {
return this.asgardeo.exchangeToken(config, (user: User) => {}) as unknown as TokenResponse | Response;
});
}

override signIn(
options?: SignInOptions,
sessionId?: string,
Expand Down
27 changes: 26 additions & 1 deletion packages/react/src/contexts/Asgardeo/AsgardeoContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@
*/

import {Context, createContext} from 'react';
import {HttpRequestConfig, HttpResponse, IdToken, Organization, SignInOptions} from '@asgardeo/browser';
import {
HttpRequestConfig,
HttpResponse,
IdToken,
Organization,
SignInOptions,
TokenExchangeRequestConfig,
TokenResponse,
} from '@asgardeo/browser';
import AsgardeoReactClient from '../../AsgardeoReactClient';

/**
Expand Down Expand Up @@ -97,6 +105,21 @@ export type AsgardeoContextProps = {
* @returns A promise that resolves to the decoded ID token payload.
*/
getDecodedIdToken?: () => Promise<IdToken>;

/**
* Retrieves the access token stored in the storage.
* This function retrieves the access token and returns it.
* @remarks This does not work in the `webWorker` or any other worker environment.
* @returns A promise that resolves to the access token.
*/
getAccessToken?: () => Promise<string>;

/**
* Swaps the current access token with a new one based on the provided configuration (with a grant type).
* @param config - Configuration for the token exchange request.
* @returns A promise that resolves to the token response or the raw response.
*/
exchangeToken?: (config: TokenExchangeRequestConfig) => Promise<TokenResponse | Response>;
};

/**
Expand Down Expand Up @@ -124,6 +147,8 @@ const AsgardeoContext: Context<AsgardeoContextProps | null> = createContext<null
},
signInOptions: {},
getDecodedIdToken: null,
getAccessToken: null,
exchangeToken: null,
});

AsgardeoContext.displayName = 'AsgardeoContext';
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/contexts/Asgardeo/AsgardeoProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = ({
},
signInOptions,
getDecodedIdToken: asgardeo.getDecodedIdToken.bind(asgardeo),
exchangeToken: asgardeo.exchangeToken.bind(asgardeo),
syncSession,
}),
[
Expand Down
Loading