Skip to content

Commit

Permalink
Fix some login problems (#6810)
Browse files Browse the repository at this point in the history
* ensure that active organization is set after logging in

* avoid unnecessary page refresh after logging in

* update changelog

* explain hard reload
  • Loading branch information
philippotto committed Feb 7, 2023
1 parent a0a255d commit d876df9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ For upgrade instructions, please check the [migration guide](MIGRATIONS.released
### Changed

### Fixed
- Fixed a benign error message which briefly appeared after logging in. [#6810](https://github.com/scalableminds/webknossos/pull/6810)

### Removed

Expand Down
7 changes: 5 additions & 2 deletions frontend/javascripts/admin/admin_rest_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,14 @@ export function sendFailedRequestAnalyticsEvent(
export async function loginUser(formValues: {
email: string;
password: string;
}): Promise<Record<string, any>> {
}): Promise<[APIUser, APIOrganization]> {
await Request.sendJSONReceiveJSON("/api/auth/login", {
data: formValues,
});
return getActiveUser();
const activeUser = await getActiveUser();
const organization = await getOrganization(activeUser.organization);

return [activeUser, organization];
}

export async function getUsers(): Promise<Array<APIUser>> {
Expand Down
5 changes: 3 additions & 2 deletions frontend/javascripts/admin/auth/login_form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { setActiveUserAction } from "oxalis/model/actions/user_actions";
import Store from "oxalis/store";
import messages from "messages";
import features from "features";
import { setActiveOrganizationAction } from "oxalis/model/actions/organization_actions";

const FormItem = Form.Item;
const { Password } = Input;
Expand All @@ -34,9 +35,9 @@ function LoginForm({ layout, onLoggedIn, hideFooter, style }: Props) {

// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'formValues' implicitly has an 'any' typ... Remove this comment to see the full error message
const onFinish = async (formValues) => {
const user = await loginUser(formValues);
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type 'Record<string, any>' is not assi... Remove this comment to see the full error message
const [user, organization] = await loginUser(formValues);
Store.dispatch(setActiveUserAction(user));
Store.dispatch(setActiveOrganizationAction(organization));

if (onLoggedIn) {
onLoggedIn();
Expand Down
5 changes: 3 additions & 2 deletions frontend/javascripts/admin/auth/registration_form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Request from "libs/request";
import Store from "oxalis/throttled_store";
import messages from "messages";
import { setHasOrganizationsAction } from "oxalis/model/actions/ui_actions";
import { setActiveOrganizationAction } from "oxalis/model/actions/organization_actions";
const FormItem = Form.Item;
const { Password } = Input;
type Props = {
Expand Down Expand Up @@ -38,12 +39,12 @@ function RegistrationForm(props: Props) {
const tryAutoLogin = props.tryAutoLogin || props.inviteToken != null || autoVerified;

if (tryAutoLogin) {
const user = await loginUser({
const [user, organization] = await loginUser({
email: formValues.email,
password: formValues.password.password1,
});
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type 'Record<string, any>' is not assi... Remove this comment to see the full error message
Store.dispatch(setActiveUserAction(user));
Store.dispatch(setActiveOrganizationAction(organization));
}

props.onRegistered(tryAutoLogin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { setActiveUserAction } from "oxalis/model/actions/user_actions";
import Request from "libs/request";
import Store from "oxalis/throttled_store";
import messages from "messages";
import { setActiveOrganizationAction } from "oxalis/model/actions/organization_actions";
const FormItem = Form.Item;
const { Password } = Input;
type Props = {
Expand Down Expand Up @@ -40,12 +41,12 @@ function SpotlightRegistrationForm(props: Props) {
organizationDisplayName: `${formValues.firstName} ${formValues.lastName} Lab`,
},
});
const user = await loginUser({
const [user, organization] = await loginUser({
email: formValues.email,
password: formValues.password.password1,
});
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type 'Record<string, any>' is not assi... Remove this comment to see the full error message
Store.dispatch(setActiveUserAction(user));
Store.dispatch(setActiveOrganizationAction(organization));
props.onRegistered(true);
}

Expand Down
9 changes: 7 additions & 2 deletions frontend/javascripts/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { Redirect, Route, Router, Switch } from "react-router-dom";
import { APICompoundTypeEnum, APIUser, TracingTypeEnum } from "types/api_flow_types";

import ErrorBoundary from "components/error_boundary";
import { Store } from "oxalis/singletons";

const { Content } = Layout;

Expand Down Expand Up @@ -264,11 +265,15 @@ class ReactRouter extends React.Component<Props> {
<RouteWithErrorBoundary
path="/dashboard"
render={() => {
if (isAuthenticated) {
// Imperatively access store state to avoid race condition when logging in.
// The `isAuthenticated` prop could be outdated for a short time frame which
// would lead to an unnecessary browser refresh.
const { activeUser } = Store.getState();
if (activeUser) {
return <DashboardView userId={null} isAdminView={false} initialTabKey={null} />;
}

// Hard navigate
// Hard navigate so that webknossos.org is shown for the demo instance.
window.location.href = "/";
return null;
}}
Expand Down

0 comments on commit d876df9

Please sign in to comment.