Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto log out when session expires #7317

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 39 additions & 36 deletions packages/insomnia/src/ui/routes/organization.tsx
Expand Up @@ -130,13 +130,18 @@ function sortOrganizations(accountId: string, organizations: Organization[]): Or
export const indexLoader: LoaderFunction = async () => {
const { id: sessionId, accountId } = await userSession.getOrCreate();
if (sessionId) {
// offline strategy: fetch or load from localStorage if thrown error, logout if expired
try {
const organizationsResult = await window.main.insomniaFetch<OrganizationsResponse | void>({
const organizationsResult = await window.main.insomniaFetch<OrganizationsResponse | { error: string } | void>({
method: 'GET',
path: '/v1/organizations',
sessionId,
});

const hasSessionExpired = organizationsResult && 'error' in organizationsResult && organizationsResult.error === 'UNAUTHORIZED';
if (hasSessionExpired) {
await session.logout();
return redirect('/auth/login');
}
const user = await window.main.insomniaFetch<UserProfileResponse | void>({
method: 'GET',
path: '/v1/user/profile',
Expand All @@ -149,7 +154,7 @@ export const indexLoader: LoaderFunction = async () => {
sessionId,
});

invariant(organizationsResult && organizationsResult.organizations, 'Failed to load organizations');
invariant(organizationsResult && 'organizations' in organizationsResult && organizationsResult.organizations, 'Failed to load organizations');
invariant(user && user.id, 'Failed to load user');
invariant(currentPlan && currentPlan.planId, 'Failed to load current plan');

Expand All @@ -165,45 +170,45 @@ export const indexLoader: LoaderFunction = async () => {
}

const organizations = JSON.parse(localStorage.getItem(`${accountId}:organizations`) || '[]') as Organization[];
invariant(organizations, 'Failed to fetch organizations');

const personalOrganization = organizations.filter(isPersonalOrganization)
.find(organization =>
isOwnerOfOrganization({
organization,
accountId,
}));
invariant(personalOrganization, 'Failed to find personal organization your account appears to be in an invalid state. Please contact support if this is a recurring issue.');
if (await shouldMigrateProjectUnderOrganization()) {
await migrateProjectsIntoOrganization({
personalOrganization,
.find(organization =>
isOwnerOfOrganization({
organization,
accountId,
}));
invariant(personalOrganization, 'Failed to find personal organization your account appears to be in an invalid state. Please contact support if this is a recurring issue.');
if (await shouldMigrateProjectUnderOrganization()) {
await migrateProjectsIntoOrganization({
personalOrganization,
});

const preferredProjectType = localStorage.getItem('prefers-project-type');
if (preferredProjectType === 'remote') {
const localProjects = await database.find<Project>('Project', {
parentId: personalOrganization.id,
remoteId: null,
});

const preferredProjectType = localStorage.getItem('prefers-project-type');
if (preferredProjectType === 'remote') {
const localProjects = await database.find<Project>('Project', {
parentId: personalOrganization.id,
remoteId: null,
// If any of those fail projects will still be under the organization as local projects
for (const project of localProjects) {
updateLocalProjectToRemote({
project,
organizationId: personalOrganization.id,
sessionId,
vcs: VCSInstance(),
});

// If any of those fail projects will still be under the organization as local projects
for (const project of localProjects) {
updateLocalProjectToRemote({
project,
organizationId: personalOrganization.id,
sessionId,
vcs: VCSInstance(),
});
}
}
}
}
if (personalOrganization) {
return redirect(`/organization/${personalOrganization.id}`);
}

if (personalOrganization) {
return redirect(`/organization/${personalOrganization.id}`);
}

if (organizations.length > 0) {
return redirect(`/organization/${organizations[0].id}`);
}
if (organizations.length > 0) {
return redirect(`/organization/${organizations[0].id}`);
}
}

await session.logout();
Expand All @@ -215,7 +220,6 @@ export const syncOrganizationsAction: ActionFunction = async () => {

if (sessionId) {
try {

const organizationsResult = await window.main.insomniaFetch<OrganizationsResponse | void>({
method: 'GET',
path: '/v1/organizations',
Expand Down Expand Up @@ -331,7 +335,6 @@ export const singleOrgLoader: LoaderFunction = async ({ params }): Promise<Organ
if (!organization) {
throw redirect('/organization');
}

try {
const response = await window.main.insomniaFetch<{ features: FeatureList; billing: Billing } | undefined>({
method: 'GET',
Expand Down