Skip to content

Commit 679278a

Browse files
committed
Refactor aggregate-changelogs.js for improved readability and consistency
- Consolidated array declaration for SKIP_PACKAGES into a single line. - Removed unnecessary line breaks and adjusted spacing for function definitions and conditions. - Enhanced code clarity by simplifying the structure of the findChangelogs function.
1 parent cd28e5b commit 679278a

File tree

5 files changed

+102
-72
lines changed

5 files changed

+102
-72
lines changed

.github/workflows/release.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ env:
1717
jobs:
1818
release:
1919
name: 📦 Release
20-
if: "!contains(github.event.head_commit.message, 'ci skip') && !contains(github.event.head_commit.message, 'skip ci')"
20+
if:
21+
"!contains(github.event.head_commit.message, 'ci skip') && !contains(github.event.head_commit.message, 'skip ci')"
2122
runs-on: ubuntu-latest
2223
strategy:
2324
matrix:
@@ -60,7 +61,7 @@ jobs:
6061
id: changesets
6162
uses: changesets/action@v1
6263
with:
63-
title: "[Release] [GitHub Action] Update package versions"
64+
title: '[Release] [GitHub Action] Update package versions'
6465
publish: pnpm publish:packages
6566
version: pnpm version:packages
6667
commit: "[WSO2 Release] [GitHub Action] [Release] [skip ci] update package versions"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@
4141
"publishConfig": {
4242
"access": "restricted"
4343
}
44-
}
44+
}

packages/react/src/AsgardeoReactClient.ts

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -125,37 +125,41 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
125125
}
126126

127127
async getDecodedIdToken(sessionId?: string): Promise<IdToken> {
128-
return this.asgardeo.getDecodedIdToken(sessionId);
128+
return this.withLoading(async () => {
129+
return this.asgardeo.getDecodedIdToken(sessionId);
130+
});
129131
}
130132

131133
async getUserProfile(options?: any): Promise<UserProfile> {
132-
try {
133-
let baseUrl = options?.baseUrl;
134+
return this.withLoading(async () => {
135+
try {
136+
let baseUrl = options?.baseUrl;
134137

135-
if (!baseUrl) {
136-
const configData = await this.asgardeo.getConfigData();
137-
baseUrl = configData?.baseUrl;
138-
}
138+
if (!baseUrl) {
139+
const configData = await this.asgardeo.getConfigData();
140+
baseUrl = configData?.baseUrl;
141+
}
139142

140-
const profile = await getScim2Me({baseUrl});
141-
const schemas = await getSchemas({baseUrl});
143+
const profile = await getScim2Me({baseUrl});
144+
const schemas = await getSchemas({baseUrl});
142145

143-
const processedSchemas = flattenUserSchema(schemas);
146+
const processedSchemas = flattenUserSchema(schemas);
144147

145-
const output = {
146-
schemas: processedSchemas,
147-
flattenedProfile: generateFlattenedUserProfile(profile, processedSchemas),
148-
profile,
149-
};
148+
const output = {
149+
schemas: processedSchemas,
150+
flattenedProfile: generateFlattenedUserProfile(profile, processedSchemas),
151+
profile,
152+
};
150153

151-
return output;
152-
} catch (error) {
153-
return {
154-
schemas: [],
155-
flattenedProfile: extractUserClaimsFromIdToken(await this.getDecodedIdToken()),
156-
profile: extractUserClaimsFromIdToken(await this.getDecodedIdToken()),
157-
};
158-
}
154+
return output;
155+
} catch (error) {
156+
return {
157+
schemas: [],
158+
flattenedProfile: extractUserClaimsFromIdToken(await this.getDecodedIdToken()),
159+
profile: extractUserClaimsFromIdToken(await this.getDecodedIdToken()),
160+
};
161+
}
162+
});
159163
}
160164

161165
override async getMyOrganizations(options?: any, sessionId?: string): Promise<Organization[]> {
@@ -201,13 +205,14 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
201205
}
202206

203207
override async getCurrentOrganization(): Promise<Organization | null> {
204-
const idToken: IdToken = await this.getDecodedIdToken();
205-
206-
return {
207-
orgHandle: idToken?.org_handle,
208-
name: idToken?.org_name,
209-
id: idToken?.org_id,
210-
};
208+
return this.withLoading(async () => {
209+
const idToken: IdToken = await this.getDecodedIdToken();
210+
return {
211+
orgHandle: idToken?.org_handle,
212+
name: idToken?.org_name,
213+
id: idToken?.org_id,
214+
};
215+
});
211216
}
212217

213218
override async switchOrganization(organization: Organization, sessionId?: string): Promise<TokenResponse | Response> {
@@ -258,8 +263,8 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
258263
return this.asgardeo.isInitialized();
259264
}
260265

261-
override isSignedIn(): Promise<boolean> {
262-
return this.asgardeo.isSignedIn();
266+
override async isSignedIn(): Promise<boolean> {
267+
return await this.asgardeo.isSignedIn();
263268
}
264269

265270
override getConfiguration(): T {
@@ -355,7 +360,9 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
355360
}
356361

357362
override async getAccessToken(sessionId?: string): Promise<string> {
358-
return this.asgardeo.getAccessToken(sessionId);
363+
return this.withLoading(async () => {
364+
return this.asgardeo.getAccessToken(sessionId);
365+
});
359366
}
360367
}
361368

packages/react/src/contexts/Asgardeo/AsgardeoProvider.tsx

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
getBrandingPreference,
2727
GetBrandingPreferenceConfig,
2828
BrandingPreference,
29+
IdToken,
2930
} from '@asgardeo/browser';
3031
import {FC, RefObject, PropsWithChildren, ReactElement, useEffect, useMemo, useRef, useState, useCallback} from 'react';
3132
import AsgardeoContext from './AsgardeoContext';
@@ -88,6 +89,8 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = ({
8889
...rest,
8990
});
9091

92+
const [isUpdatingSession, setIsUpdatingSession] = useState<boolean>(false);
93+
9194
// Branding state
9295
const [brandingPreference, setBrandingPreference] = useState<BrandingPreference | null>(null);
9396
const [isBrandingLoading, setIsBrandingLoading] = useState<boolean>(false);
@@ -129,27 +132,32 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = ({
129132

130133
(async (): Promise<void> => {
131134
// User is already authenticated. Skip...
132-
if (await asgardeo.isSignedIn()) {
133-
await updateSession();
135+
const isAlreadySignedIn: boolean = await asgardeo.isSignedIn();
134136

137+
if (isAlreadySignedIn) {
138+
await updateSession();
135139
return;
136140
}
137141

138-
if (hasAuthParams(new URL(window.location.href), afterSignInUrl)) {
142+
const currentUrl: URL = new URL(window.location.href);
143+
const hasAuthParamsResult: boolean = hasAuthParams(currentUrl, afterSignInUrl);
144+
145+
if (hasAuthParamsResult) {
139146
try {
140147
await signIn(
141148
{callOnlyOnRedirect: true},
142149
// authParams?.authorizationCode,
143150
// authParams?.sessionState,
144151
// authParams?.state,
145152
);
146-
147153
// setError(null);
148154
} catch (error) {
149155
if (error && Object.prototype.hasOwnProperty.call(error, 'code')) {
150156
// setError(error);
151157
}
152158
}
159+
} else {
160+
// TODO: Add a debug log to indicate that the user is not signed in
153161
}
154162
})();
155163
}, []);
@@ -177,6 +185,8 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = ({
177185
clearInterval(interval);
178186
}
179187
}, 1000);
188+
} else {
189+
// TODO: Add a debug log to indicate that the user is already signed in.
180190
}
181191
} catch (error) {
182192
setIsSignedInSync(false);
@@ -207,8 +217,12 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = ({
207217
*/
208218
useEffect(() => {
209219
const checkLoadingState = (): void => {
210-
const loadingState = asgardeo.isLoading();
211-
setIsLoadingSync(loadingState);
220+
// Don't override loading state during critical session updates
221+
if (isUpdatingSession) {
222+
return;
223+
}
224+
225+
setIsLoadingSync(asgardeo.isLoading());
212226
};
213227

214228
// Initial check
@@ -220,25 +234,44 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = ({
220234
return (): void => {
221235
clearInterval(interval);
222236
};
223-
}, [asgardeo]);
237+
}, [asgardeo, isLoadingSync, isSignedInSync, isUpdatingSession]);
224238

225239
const updateSession = async (): Promise<void> => {
226240
try {
241+
// Set flag to prevent loading state tracking from interfering
242+
setIsUpdatingSession(true);
227243
setIsLoadingSync(true);
228244
let _baseUrl: string = baseUrl;
229245

246+
const decodedToken: IdToken = await asgardeo.getDecodedIdToken();
247+
230248
// If there's a `user_org` claim in the ID token,
231249
// Treat this login as a organization login.
232-
if ((await asgardeo.getDecodedIdToken())?.['user_org']) {
250+
if (decodedToken?.['user_org']) {
233251
_baseUrl = `${(await asgardeo.getConfiguration()).baseUrl}/o`;
234252
setBaseUrl(_baseUrl);
235253
}
236254

237-
setUser(await asgardeo.getUser({baseUrl: _baseUrl}));
238-
setUserProfile(await asgardeo.getUserProfile({baseUrl: _baseUrl}));
239-
setCurrentOrganization(await asgardeo.getCurrentOrganization());
240-
setMyOrganizations(await asgardeo.getMyOrganizations());
255+
const user: User = await asgardeo.getUser({baseUrl: _baseUrl});
256+
const userProfile: UserProfile = await asgardeo.getUserProfile({baseUrl: _baseUrl});
257+
const currentOrganization: Organization = await asgardeo.getCurrentOrganization();
258+
const myOrganizations: Organization[] = await asgardeo.getMyOrganizations();
259+
260+
// Update user data first
261+
setUser(user);
262+
setUserProfile(userProfile);
263+
setCurrentOrganization(currentOrganization);
264+
setMyOrganizations(myOrganizations);
265+
266+
// CRITICAL: Update sign-in status BEFORE setting loading to false
267+
// This prevents the race condition where ProtectedRoute sees isLoading=false but isSignedIn=false
268+
const currentSignInStatus = await asgardeo.isSignedIn();
269+
setIsSignedInSync(await asgardeo.isSignedIn());
270+
} catch (error) {
271+
// TODO: Add an error log.
241272
} finally {
273+
// Clear the flag and set final loading state
274+
setIsUpdatingSession(false);
242275
setIsLoadingSync(asgardeo.isLoading());
243276
}
244277
};
@@ -302,6 +335,7 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = ({
302335

303336
const signIn = async (...args: any): Promise<User> => {
304337
try {
338+
setIsUpdatingSession(true);
305339
setIsLoadingSync(true);
306340
const response: User = await asgardeo.signIn(...args);
307341

@@ -313,12 +347,14 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = ({
313347
} catch (error) {
314348
throw new Error(`Error while signing in: ${error}`);
315349
} finally {
350+
setIsUpdatingSession(false);
316351
setIsLoadingSync(asgardeo.isLoading());
317352
}
318353
};
319354

320355
const signInSilently = async (options?: SignInOptions): Promise<User | boolean> => {
321356
try {
357+
setIsUpdatingSession(true);
322358
setIsLoadingSync(true);
323359
const response: User | boolean = await asgardeo.signInSilently(options);
324360

@@ -335,12 +371,14 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = ({
335371
'An error occurred while trying to sign in silently.',
336372
);
337373
} finally {
374+
setIsUpdatingSession(false);
338375
setIsLoadingSync(asgardeo.isLoading());
339376
}
340377
};
341378

342379
const switchOrganization = async (organization: Organization): Promise<void> => {
343380
try {
381+
setIsUpdatingSession(true);
344382
setIsLoadingSync(true);
345383
await asgardeo.switchOrganization(organization);
346384

@@ -355,6 +393,7 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = ({
355393
'An error occurred while switching to the specified organization.',
356394
);
357395
} finally {
396+
setIsUpdatingSession(false);
358397
setIsLoadingSync(asgardeo.isLoading());
359398
}
360399
};

scripts/aggregate-changelogs.js

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,19 @@ const path = require('path');
2626
const rootDir = process.cwd();
2727
const outputFile = path.join(rootDir, 'CHANGELOG.md');
2828

29-
3029
// List of package or directory names to skip when aggregating changelogs
31-
const SKIP_PACKAGES = [
32-
'__legacy__',
33-
'node_modules',
34-
'dist',
35-
'build',
36-
'coverage',
37-
'scripts',
38-
'docs',
39-
];
30+
const SKIP_PACKAGES = ['__legacy__', 'node_modules', 'dist', 'build', 'coverage', 'scripts', 'docs'];
4031

41-
const findChangelogs = (dir) => {
32+
const findChangelogs = dir => {
4233
let changelogs = [];
43-
const entries = fs.readdirSync(dir, { withFileTypes: true });
34+
const entries = fs.readdirSync(dir, {withFileTypes: true});
4435

4536
for (const entry of entries) {
4637
const fullPath = path.join(dir, entry.name);
4738

48-
if (
49-
entry.isDirectory() &&
50-
!SKIP_PACKAGES.includes(entry.name)
51-
) {
39+
if (entry.isDirectory() && !SKIP_PACKAGES.includes(entry.name)) {
5240
changelogs = changelogs.concat(findChangelogs(fullPath));
53-
} else if (
54-
entry.isFile() &&
55-
entry.name === 'CHANGELOG.md' &&
56-
fullPath !== outputFile
57-
) {
41+
} else if (entry.isFile() && entry.name === 'CHANGELOG.md' && fullPath !== outputFile) {
5842
// Check if the changelog is inside a skipped package
5943
const relPath = path.relative(rootDir, fullPath);
6044
const parts = relPath.split(path.sep);
@@ -65,8 +49,7 @@ const findChangelogs = (dir) => {
6549
}
6650

6751
return changelogs;
68-
}
69-
52+
};
7053

7154
const aggregate = () => {
7255
const changelogFiles = findChangelogs(rootDir);
@@ -93,6 +76,6 @@ const aggregate = () => {
9376
fs.writeFileSync(outputFile, toc + output);
9477

9578
console.log(`Aggregated ${changelogFiles.length} changelogs into ${outputFile}`);
96-
}
79+
};
9780

9881
aggregate();

0 commit comments

Comments
 (0)