Skip to content

Commit f23f72a

Browse files
Improve login and logout
1 parent 0b04cc6 commit f23f72a

File tree

12 files changed

+114
-39
lines changed

12 files changed

+114
-39
lines changed

packages/app/src/app/overmind/actions.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
convertTypeToStatus,
44
} from '@codesandbox/common/lib/utils/notifications';
55

6+
import { Page } from '@codesandbox/common/lib/types';
67
import { withLoadApp } from './factories';
78
import * as internalActions from './internalActions';
89
import { Action, AsyncAction } from '.';
@@ -13,15 +14,16 @@ export const appUnmounted: AsyncAction = async ({ effects, actions }) => {
1314
effects.connection.removeListener(actions.connectionChanged);
1415
};
1516

16-
export const sandboxPageMounted: AsyncAction = withLoadApp();
17+
export const sandboxPageMounted: AsyncAction = withLoadApp(Page.NEW_SANDBOX);
1718

18-
export const searchMounted: AsyncAction = withLoadApp();
19+
export const searchMounted: AsyncAction = withLoadApp(Page.SEARCH);
1920

20-
export const codesadboxMounted: AsyncAction = withLoadApp();
21+
export const codesadboxMounted: AsyncAction = withLoadApp(Page.SADBOX);
2122

22-
export const genericPageMounted: AsyncAction = withLoadApp();
23+
export const genericPageMounted: AsyncAction = withLoadApp(Page.GENERIC_PAGE);
2324

2425
export const cliMounted: AsyncAction = withLoadApp(
26+
Page.CLI,
2527
async ({ state, actions }) => {
2628
if (state.user) {
2729
await actions.internal.authorize();
@@ -52,9 +54,11 @@ export const notificationRemoved: Action<{
5254
state.notifications.splice(notificationToRemoveIndex, 1);
5355
};
5456

55-
export const cliInstructionsMounted: AsyncAction = withLoadApp();
57+
export const cliInstructionsMounted: AsyncAction = withLoadApp(
58+
Page.CLI_INSTRUCTIONS
59+
);
5660

57-
export const githubPageMounted: AsyncAction = withLoadApp();
61+
export const githubPageMounted: AsyncAction = withLoadApp(Page.GITHUB);
5862

5963
export const connectionChanged: Action<boolean> = ({ state }, connected) => {
6064
state.connected = connected;
@@ -179,19 +183,10 @@ export const signInGithubClicked: AsyncAction = async ({ state, actions }) => {
179183
state.isLoadingGithub = false;
180184
};
181185

182-
export const signOutClicked: AsyncAction = async ({
183-
state,
184-
effects,
185-
actions,
186-
}) => {
186+
export const signOutClicked: AsyncAction = async ({ effects }) => {
187187
effects.analytics.track('Sign Out', {});
188-
state.workspace.openedWorkspaceItem = 'files';
189-
if (state.live.isLive) {
190-
actions.live.internal.disconnect();
191-
}
192188
await effects.api.signout();
193189
effects.jwt.reset();
194-
state.user = null;
195190
effects.browser.reload();
196191
};
197192

packages/app/src/app/overmind/effects/router.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,21 @@ export default new (class RouterEffect {
6666
history.replace(url.replace(origin, ''));
6767
}
6868

69+
reload() {
70+
const current = new URL(location.href);
71+
history.replace(
72+
`${current.pathname}${
73+
current.search ? `?${current.search}&login` : '?login'
74+
}`
75+
);
76+
}
77+
6978
getParameter(key: string): string | null {
7079
const currentUrl = new URL(location.href);
7180
return currentUrl.searchParams.get(key);
7281
}
82+
83+
getPathParts() {
84+
return new URL(location.href).pathname.split('/');
85+
}
7386
})();

packages/app/src/app/overmind/factories.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { Contributor, PermissionType } from '@codesandbox/common/lib/types';
1+
import {
2+
Contributor,
3+
PermissionType,
4+
Page,
5+
} from '@codesandbox/common/lib/types';
26
import { hasPermission } from '@codesandbox/common/lib/utils/permission';
37
import { IDerive, IState } from 'overmind';
48

@@ -9,10 +13,11 @@ import { AsyncAction } from '.';
913
and settings
1014
*/
1115
export const withLoadApp = <T>(
16+
page: Page,
1217
continueAction?: AsyncAction<T>
1318
): AsyncAction<T> => async (context, value) => {
1419
const { effects, state, actions } = context;
15-
20+
state.currentPage = page;
1621
if (state.hasLoadedApp && continueAction) {
1722
await continueAction(context, value);
1823
return;

packages/app/src/app/overmind/internalActions.ts

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ServerContainerStatus,
99
ServerStatus,
1010
TabType,
11+
Page,
1112
} from '@codesandbox/common/lib/types';
1213
import { patronUrl } from '@codesandbox/common/lib/utils/url-generator';
1314
import { NotificationStatus } from '@codesandbox/notifications';
@@ -27,16 +28,49 @@ export const signIn: AsyncAction<{ useExtraScopes?: boolean }> = async (
2728
try {
2829
const jwt = await actions.internal.signInGithub(options);
2930
actions.internal.setJwt(jwt);
30-
state.user = await effects.api.getCurrentUser();
31-
actions.internal.setPatronPrice();
32-
actions.internal.setSignedInCookie();
33-
effects.analytics.identify('signed_in', true);
34-
effects.analytics.setUserId(state.user.id);
35-
actions.internal.setStoredSettings();
36-
effects.live.connect();
37-
actions.userNotifications.internal.initialize(); // Seemed a bit different originally?
38-
actions.refetchSandboxInfo();
39-
state.isAuthenticating = false;
31+
state.hasLoadedApp = false;
32+
switch (state.currentPage) {
33+
case Page.GITHUB:
34+
actions.githubPageMounted();
35+
break;
36+
case Page.CLI_INSTRUCTIONS:
37+
actions.cliInstructionsMounted();
38+
break;
39+
case Page.NEW_SANDBOX:
40+
actions.sandboxPageMounted();
41+
break;
42+
case Page.CURATOR:
43+
actions.explore.popularSandboxesMounted(null);
44+
break;
45+
case Page.SANDBOX:
46+
actions.editor.sandboxChanged({
47+
id: state.editor.currentSandbox.id,
48+
});
49+
break;
50+
case Page.DASHBOARD:
51+
actions.dashboard.dashboardMounted();
52+
break;
53+
case Page.LIVE:
54+
actions.live.roomJoined({
55+
roomId: effects.router.getPathParts().pop(),
56+
});
57+
break;
58+
case Page.PROFILE:
59+
actions.profile.profileMounted(effects.router.getPathParts().pop());
60+
break;
61+
case Page.SEARCH:
62+
actions.searchMounted();
63+
break;
64+
case Page.PATRON:
65+
actions.patron.patronMounted();
66+
break;
67+
case Page.PRO:
68+
actions.patron.patronMounted();
69+
break;
70+
case Page.CLI:
71+
actions.cliMounted();
72+
break;
73+
}
4074
} catch (error) {
4175
actions.internal.handleError({
4276
message: 'Could not authenticate with Github',

packages/app/src/app/overmind/namespaces/dashboard/actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Action, AsyncAction } from 'app/overmind';
22
import { withLoadApp } from 'app/overmind/factories';
3+
import { Page } from '@codesandbox/common/lib/types';
34
import { OrderBy } from './state';
45

5-
export const dashboardMounted: AsyncAction = withLoadApp();
6+
export const dashboardMounted: AsyncAction = withLoadApp(Page.DASHBOARD);
67

78
export const sandboxesSelected: Action<{
89
sandboxIds: string[];

packages/app/src/app/overmind/namespaces/editor/actions.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
WindowOrientation,
1010
Module,
1111
UserSelection,
12+
Page,
1213
} from '@codesandbox/common/lib/types';
1314
import { logBreadcrumb } from '@codesandbox/common/lib/utils/analytics/sentry';
1415
import { getTextOperation } from '@codesandbox/common/lib/utils/diff';
@@ -91,9 +92,11 @@ export const loadCursorFromUrl: AsyncAction = async ({
9192

9293
await actions.editor.moduleSelected({ id: module.id });
9394

94-
const [parsedHead, parsedAnchor] = selection.split('-').map(Number);
95-
if (!isNaN(parsedHead) && !isNaN(parsedAnchor)) {
96-
effects.vscode.setSelection(parsedHead, parsedAnchor);
95+
if (selection) {
96+
const [parsedHead, parsedAnchor] = selection.split('-').map(Number);
97+
if (!isNaN(parsedHead) && !isNaN(parsedAnchor)) {
98+
effects.vscode.setSelection(parsedHead, parsedAnchor);
99+
}
97100
}
98101
};
99102

@@ -134,7 +137,7 @@ export const npmDependencyRemoved: AsyncAction<string> = withOwnedSandbox(
134137

135138
export const sandboxChanged: AsyncAction<{ id: string }> = withLoadApp<{
136139
id: string;
137-
}>(async ({ state, actions, effects }, { id }) => {
140+
}>(Page.SANDBOX, async ({ state, actions, effects }, { id }) => {
138141
// This happens when we fork. This can be avoided with state first routing
139142
if (state.editor.isForkingSandbox && state.editor.currentSandbox) {
140143
effects.vscode.openModule(state.editor.currentModule);

packages/app/src/app/overmind/namespaces/explore/actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { PickedSandboxDetails } from '@codesandbox/common/lib/types';
1+
import { PickedSandboxDetails, Page } from '@codesandbox/common/lib/types';
22
import { Action, AsyncAction } from 'app/overmind';
33
import { withLoadApp } from 'app/overmind/factories';
44

55
export const popularSandboxesMounted: AsyncAction<string> = withLoadApp(
6+
Page.CURATOR,
67
async ({ state, actions, effects }, date) => {
78
try {
89
state.explore.popularSandboxes = await effects.api.getPopularSandboxes(

packages/app/src/app/overmind/namespaces/live/actions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
LiveMessageEvent,
44
UserViewRange,
55
UserSelection,
6+
Page,
67
} from '@codesandbox/common/lib/types';
78
import { Action, AsyncAction, Operator } from 'app/overmind';
89
import { withLoadApp } from 'app/overmind/factories';
@@ -17,7 +18,7 @@ export const internal = internalActions;
1718

1819
export const signInToRoom: AsyncAction<{
1920
roomId: string;
20-
}> = withLoadApp(async ({ state, effects, actions }, { roomId }) => {
21+
}> = withLoadApp(Page.LIVE, async ({ state, effects, actions }, { roomId }) => {
2122
await actions.internal.signIn({});
2223

2324
if (state.isLoggedIn) {
@@ -57,7 +58,7 @@ export const onOperationError: Action<{
5758

5859
export const roomJoined: AsyncAction<{
5960
roomId: string;
60-
}> = withLoadApp(async ({ state, effects, actions }, { roomId }) => {
61+
}> = withLoadApp(Page.LIVE, async ({ state, effects, actions }, { roomId }) => {
6162
if (!state.isLoggedIn) {
6263
return;
6364
}

packages/app/src/app/overmind/namespaces/patron/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { AsyncAction, Action } from 'app/overmind';
22
import { withLoadApp } from 'app/overmind/factories';
3-
import { StripeErrorCode } from '@codesandbox/common/lib/types';
3+
import { StripeErrorCode, Page } from '@codesandbox/common/lib/types';
44

5-
export const patronMounted: AsyncAction = withLoadApp();
5+
export const patronMounted: AsyncAction = withLoadApp(Page.PATRON);
66

77
export const priceChanged: Action<{ price: number }> = (
88
{ state },

packages/app/src/app/overmind/namespaces/profile/actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Sandbox } from '@codesandbox/common/lib/types';
1+
import { Sandbox, Page } from '@codesandbox/common/lib/types';
22
import { Action, AsyncAction } from 'app/overmind';
33
import { withLoadApp } from 'app/overmind/factories';
44

55
export const profileMounted: AsyncAction<string> = withLoadApp(
6+
Page.PROFILE,
67
async ({ effects, state }, username) => {
78
state.profile.isLoadingProfile = true;
89
state.profile.notFound = false;

0 commit comments

Comments
 (0)