Skip to content

Commit

Permalink
fix: handle login when opening org logged out [INS-4330] (#7865)
Browse files Browse the repository at this point in the history
* fix: handle login when opening org logged out [INS-4330]

* do the redirect in organization.tsx to avoid initial login edge case errors

* undo
  • Loading branch information
filfreire authored Aug 23, 2024
1 parent b51e49e commit e9a8b14
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/insomnia/src/ui/routes/organization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ export const indexLoader: LoaderFunction = async () => {
const personalOrganizationId = personalOrganization.id;
await migrateProjectsUnderOrganization(personalOrganizationId, sessionId);

const specificOrgRedirectAfterAuthorize = window.localStorage.getItem('specificOrgRedirectAfterAuthorize');
if (specificOrgRedirectAfterAuthorize && specificOrgRedirectAfterAuthorize !== '') {
window.localStorage.removeItem('specificOrgRedirectAfterAuthorize');
return redirect(`/organization/${specificOrgRedirectAfterAuthorize}`);
}

if (personalOrganization) {
return redirect(`/organization/${personalOrganizationId}`);
}
Expand Down
13 changes: 12 additions & 1 deletion packages/insomnia/src/ui/routes/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createPlugin } from '../../plugins/create';
import { setTheme } from '../../plugins/misc';
import { exchangeCodeForToken } from '../../sync/git/github-oauth-provider';
import { exchangeCodeForGitLabToken } from '../../sync/git/gitlab-oauth-provider';
import { getLoginUrl } from '../auth-session-provider';
import { ErrorBoundary } from '../components/error-boundary';
import { showError, showModal } from '../components/modals';
import { AlertModal } from '../components/modals/alert-modal';
Expand Down Expand Up @@ -208,7 +209,17 @@ const Root = () => {
}

case 'insomnia://app/open/organization':
navigate(`/organization/${params.organizationId}`);
// if user is logged out, navigate to authorize instead
// gracefully handle open org in app from browser
const userSession = await models.userSession.getOrCreate();
if (!userSession.id || userSession.id === '') {
const url = new URL(getLoginUrl());
window.main.openInBrowser(url.toString());
window.localStorage.setItem('specificOrgRedirectAfterAuthorize', params.organizationId);
navigate('/auth/authorize');
} else {
navigate(`/organization/${params.organizationId}`);
}
break;

default: {
Expand Down

0 comments on commit e9a8b14

Please sign in to comment.