Skip to content

Commit

Permalink
feat(clerk-js): Add shortcut to active org in Clerk singleton
Browse files Browse the repository at this point in the history
We've added a new property in the Clerk singleton
called `Clerk.organization` which allows the user to
easily get access to the active organization of the
current session.
  • Loading branch information
alex-ntousias committed May 12, 2022
1 parent b5df9b0 commit 03e68d4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/clerk-js/src/core/clerk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ describe('Clerk singleton', () => {

describe('.signOut()', () => {
const mockClientDestroy = jest.fn();
const mockSession1 = { id: '1', remove: jest.fn(), status: 'active' };
const mockSession2 = { id: '2', remove: jest.fn(), status: 'active' };
const mockSession1 = { id: '1', remove: jest.fn(), status: 'active', user: {} };
const mockSession2 = { id: '2', remove: jest.fn(), status: 'active', user: {} };

beforeEach(() => {
mockClientDestroy.mockReset();
Expand Down
11 changes: 11 additions & 0 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default class Clerk implements ClerkInterface {
public static version: string = packageJSON.version;
public client?: ClientResource;
public session?: ActiveSessionResource | null;
public organization?: OrganizationResource | null;
public user?: UserResource | null;
public frontendApi: string;

Expand Down Expand Up @@ -281,6 +282,7 @@ export default class Clerk implements ClerkInterface {
if (beforeEmit) {
beforeUnloadTracker.startTracking();
this.session = undefined;
this.organization = undefined;
this.user = undefined;
this.#emit();
await beforeEmit(session);
Expand All @@ -293,6 +295,9 @@ export default class Clerk implements ClerkInterface {
}

this.session = session;
this.organization = (this.session?.user.organizationMemberships || [])
.map(om => om.organization)
.find(org => org.id === this.session?.lastActiveOrganizationId);
this.user = this.session ? this.session.user : null;

this.#emit();
Expand Down Expand Up @@ -569,13 +574,19 @@ export default class Clerk implements ClerkInterface {
(this.#options.selectInitialSession
? this.#options.selectInitialSession(newClient)
: this.#defaultSession(newClient)) || null;
this.organization = (this.session?.user.organizationMemberships || [])
.map(om => om.organization)
.find(org => org.id === this.session?.lastActiveOrganizationId);
this.user = this.session ? this.session.user : null;
}
this.client = newClient;

if (this.session) {
const lastId = this.session.id;
this.session = newClient.activeSessions.find(x => x.id === lastId);
this.organization = (this.session?.user.organizationMemberships || [])
.map(om => om.organization)
.find(org => org.id === this.session?.lastActiveOrganizationId);
this.user = this.session ? this.session.user : null;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export interface Clerk {
/** Active Session. */
session?: ActiveSessionResource | null;

/** Active Organization */
organization?: OrganizationResource | null;

/** Current User. */
user?: UserResource | null;

Expand Down

0 comments on commit 03e68d4

Please sign in to comment.