Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions packages/clerk-js/src/ui/components/SignIn/SignInStart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ function SignInStartInternal(): JSX.Element {
setShouldAutofocus(true);
};

/**
* Redirect to account switcher if user already has active sessions in multi-session mode
*/
useEffect(() => {
const hasActiveSessions = (clerk.client?.signedInSessions?.length ?? 0) > 0;
const isMultiSessionMode = !authConfig.singleSessionMode;

if (hasActiveSessions && isMultiSessionMode) {
void navigate('choose');
}
}, [clerk.client?.signedInSessions, authConfig.singleSessionMode, navigate]);

// switch to the phone input (if available) if a "+" is entered
// (either by the browser or the user)
// this does not work in chrome as it does not fire the change event and the value is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,111 @@ describe('SignInStart', () => {
);
});
});

describe('Active session redirect', () => {
describe('multi-session mode', () => {
it('redirects to /choose when user has active sessions', async () => {
const { wrapper, fixtures } = await createFixtures(f => {
f.withEmailAddress();
f.withMultiSessionMode();
f.withUser({
email_addresses: ['[email protected]'],
});
});

fixtures.clerk.client.signedInSessions = [
{
id: 'sess_123',
user: fixtures.clerk.user,
status: 'active',
} as any,
];

render(<SignInStart />, { wrapper });

await waitFor(() => {
expect(fixtures.router.navigate).toHaveBeenCalledWith('choose');
});
});

it('redirects to /choose when user has multiple active sessions', async () => {
const { wrapper, fixtures } = await createFixtures(f => {
f.withEmailAddress();
f.withMultiSessionMode();
f.withUser({
email_addresses: ['[email protected]'],
});
});

fixtures.clerk.client.signedInSessions = [
{
id: 'sess_123',
user: fixtures.clerk.user,
status: 'active',
} as any,
{
id: 'sess_456',
user: { id: 'user_456' },
status: 'active',
} as any,
];

render(<SignInStart />, { wrapper });

await waitFor(() => {
expect(fixtures.router.navigate).toHaveBeenCalledWith('choose');
});
});

it('does NOT redirect when user has no active sessions', async () => {
const { wrapper, fixtures } = await createFixtures(f => {
f.withEmailAddress();
f.withMultiSessionMode();
});

fixtures.clerk.client.signedInSessions = [];

render(<SignInStart />, { wrapper });

await waitFor(
() => {
expect(fixtures.router.navigate).not.toHaveBeenCalledWith('choose');
},
{ timeout: 100 },
);

screen.getByText(/email address/i);
});
});

describe('single-session mode', () => {
it('does NOT redirect to /choose when user has active session in single-session mode', async () => {
const { wrapper, fixtures } = await createFixtures(f => {
f.withEmailAddress();
f.withUser({
email_addresses: ['[email protected]'],
});
});

fixtures.clerk.client.signedInSessions = [
{
id: 'sess_123',
user: fixtures.clerk.user,
status: 'active',
} as any,
];

fixtures.environment.authConfig.singleSessionMode = true;

render(<SignInStart />, { wrapper });

await waitFor(
() => {
expect(fixtures.router.navigate).not.toHaveBeenCalledWith('choose');
},
{ timeout: 100 },
);
});
});
});
});
Loading