Skip to content

Commit 404f5a5

Browse files
committed
feat(react,nextjs): expose getIdToken API
this commit introduces the getIdToken method to both AsgardeoReactClient and AsgardeoNextClient classes.
1 parent 23ac8a9 commit 404f5a5

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

packages/nextjs/src/AsgardeoNextClient.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import {
5454
Storage,
5555
TokenExchangeRequestConfig,
5656
} from '@asgardeo/node';
57-
import {AsgardeoNextConfig} from './models/config';
57+
import { AsgardeoNextConfig } from './models/config';
5858
import getSessionId from './server/actions/getSessionId';
5959
import decorateConfigWithNextEnv from './utils/decorateConfigWithNextEnv';
6060
import getClientOrigin from './server/actions/getClientOrigin';
@@ -312,8 +312,7 @@ class AsgardeoNextClient<T extends AsgardeoNextConfig = AsgardeoNextConfig> exte
312312
});
313313
} catch (error) {
314314
throw new AsgardeoRuntimeError(
315-
`Failed to fetch the user's associated organizations: ${
316-
error instanceof Error ? error.message : String(error)
315+
`Failed to fetch the user's associated organizations: ${error instanceof Error ? error.message : String(error)
317316
}`,
318317
'AsgardeoNextClient-getMyOrganizations-RuntimeError-001',
319318
'nextjs',
@@ -410,7 +409,7 @@ class AsgardeoNextClient<T extends AsgardeoNextConfig = AsgardeoNextConfig> exte
410409
* otherwise falls back to legacy client method.
411410
*/
412411
async getAccessToken(sessionId?: string): Promise<string> {
413-
const {default: getAccessToken} = await import('./server/actions/getAccessToken');
412+
const { default: getAccessToken } = await import('./server/actions/getAccessToken');
414413
const token = await getAccessToken();
415414

416415
if (typeof token !== 'string' || !token) {
@@ -433,6 +432,12 @@ class AsgardeoNextClient<T extends AsgardeoNextConfig = AsgardeoNextConfig> exte
433432
return this.asgardeo.getDecodedIdToken(sessionId as string, idToken);
434433
}
435434

435+
async getIdToken(userId?: string): Promise<string> {
436+
await this.ensureInitialized();
437+
const resolvedUserId: string = userId || ((await getSessionId()) as string);
438+
return this.asgardeo.getIdToken(resolvedUserId);
439+
}
440+
436441
override getConfiguration(): T {
437442
return this.asgardeo.getConfigData() as unknown as T;
438443
}

packages/react/src/AsgardeoReactClient.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import AuthAPI from './__temp__/api';
5656
import getMeOrganizations from './api/getMeOrganizations';
5757
import getScim2Me from './api/getScim2Me';
5858
import getSchemas from './api/getSchemas';
59-
import {AsgardeoReactConfig} from './models/config';
59+
import { AsgardeoReactConfig } from './models/config';
6060
import getAllOrganizations from './api/getAllOrganizations';
6161

6262
/**
@@ -107,7 +107,7 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
107107
}
108108

109109
return this.withLoading(async () => {
110-
return this.asgardeo.init({...config, organizationHandle: resolvedOrganizationHandle} as any);
110+
return this.asgardeo.init({ ...config, organizationHandle: resolvedOrganizationHandle } as any);
111111
});
112112
}
113113

@@ -145,8 +145,8 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
145145
baseUrl = configData?.baseUrl;
146146
}
147147

148-
const profile = await getScim2Me({baseUrl});
149-
const schemas = await getSchemas({baseUrl});
148+
const profile = await getScim2Me({ baseUrl });
149+
const schemas = await getSchemas({ baseUrl });
150150

151151
return generateUserProfile(profile, flattenUserSchema(schemas));
152152
} catch (error) {
@@ -160,6 +160,12 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
160160
});
161161
}
162162

163+
async getIdToken(): Promise<string> {
164+
return this.withLoading(async () => {
165+
return this.asgardeo.getIdToken();
166+
});
167+
}
168+
163169
async getUserProfile(options?: any): Promise<UserProfile> {
164170
return this.withLoading(async () => {
165171
try {
@@ -170,8 +176,8 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
170176
baseUrl = configData?.baseUrl;
171177
}
172178

173-
const profile = await getScim2Me({baseUrl});
174-
const schemas = await getSchemas({baseUrl});
179+
const profile = await getScim2Me({ baseUrl });
180+
const schemas = await getSchemas({ baseUrl });
175181

176182
const processedSchemas = flattenUserSchema(schemas);
177183

@@ -201,11 +207,10 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
201207
baseUrl = configData?.baseUrl;
202208
}
203209

204-
return getMeOrganizations({baseUrl});
210+
return getMeOrganizations({ baseUrl });
205211
} catch (error) {
206212
throw new AsgardeoRuntimeError(
207-
`Failed to fetch the user's associated organizations: ${
208-
error instanceof Error ? error.message : String(error)
213+
`Failed to fetch the user's associated organizations: ${error instanceof Error ? error.message : String(error)
209214
}`,
210215
'AsgardeoReactClient-getMyOrganizations-RuntimeError-001',
211216
'react',
@@ -223,7 +228,7 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
223228
baseUrl = configData?.baseUrl;
224229
}
225230

226-
return getAllOrganizations({baseUrl});
231+
return getAllOrganizations({ baseUrl });
227232
} catch (error) {
228233
throw new AsgardeoRuntimeError(
229234
`Failed to fetch all organizations: ${error instanceof Error ? error.message : String(error)}`,
@@ -282,7 +287,7 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
282287
signInRequired: true,
283288
};
284289

285-
return (await this.asgardeo.exchangeToken(exchangeConfig, (user: User) => {})) as TokenResponse | Response;
290+
return (await this.asgardeo.exchangeToken(exchangeConfig, (user: User) => { })) as TokenResponse | Response;
286291
} catch (error) {
287292
throw new AsgardeoRuntimeError(
288293
`Failed to switch organization: ${error.message || error}`,
@@ -315,7 +320,7 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
315320
sessionId?: string,
316321
): Promise<TokenResponse | Response> {
317322
return this.withLoading(async () => {
318-
return this.asgardeo.exchangeToken(config, (user: User) => {}) as unknown as TokenResponse | Response;
323+
return this.asgardeo.exchangeToken(config, (user: User) => { }) as unknown as TokenResponse | Response;
319324
});
320325
}
321326

@@ -423,7 +428,7 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
423428
baseUrl,
424429
payload:
425430
typeof firstArg === 'object' && 'flowType' in firstArg
426-
? {...(firstArg as EmbeddedFlowExecuteRequestPayload), verbose: true}
431+
? { ...(firstArg as EmbeddedFlowExecuteRequestPayload), verbose: true }
427432
: (firstArg as EmbeddedFlowExecuteRequestPayload),
428433
}) as any;
429434
}

0 commit comments

Comments
 (0)