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

fix: Prevent login screen from redirecting to signup #2170

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion src/shared/components/app/app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isAuthPath, setIsoData } from "@utils/app";
import { isAnonymousPath, isAuthPath, setIsoData } from "@utils/app";
import { dataBsTheme } from "@utils/browser";
import { Component, RefObject, createRef, linkEvent } from "inferno";
import { Provider } from "inferno-i18next-dess";
Expand All @@ -14,6 +14,7 @@ import { Footer } from "./footer";
import { Navbar } from "./navbar";
import "./styles.scss";
import { Theme } from "./theme";
import AnonymousGuard from "../common/anonymous-guard";

interface AppProps {
user?: MyUserInfo;
Expand Down Expand Up @@ -78,6 +79,10 @@ export class App extends Component<AppProps, any> {
<AuthGuard {...routeProps}>
<RouteComponent {...routeProps} />
</AuthGuard>
) : isAnonymousPath(path ?? "") ? (
<AnonymousGuard>
<RouteComponent {...routeProps} />
</AnonymousGuard>
) : (
<RouteComponent {...routeProps} />
))}
Expand Down
31 changes: 31 additions & 0 deletions src/shared/components/common/anonymous-guard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Component } from "inferno";
import { UserService } from "../../services";
import { Spinner } from "./icon";

interface AnonymousGuardState {
hasRedirected: boolean;
}

class AnonymousGuard extends Component<any, AnonymousGuardState> {
state = {
hasRedirected: false,
} as AnonymousGuardState;

constructor(props: any, context: any) {
super(props, context);
}

componentDidMount() {
if (UserService.Instance.myUserInfo) {
this.context.router.history.replace(`/`);
} else {
this.setState({ hasRedirected: true });
}
}

render() {
return this.state.hasRedirected ? this.props.children : <Spinner />;
}
}

export default AnonymousGuard;
2 changes: 2 additions & 0 deletions src/shared/components/common/subscribe-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ class RemoteFetchModal extends Component<
value={this.state.instanceText}
onInput={linkEvent(this, handleInput)}
required
enterKeyHint="go"
inputMode="url"
/>
</form>
<footer className="modal-footer">
Expand Down
1 change: 1 addition & 0 deletions src/shared/components/common/totp-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export default class TotpModal extends Component<
ref={element => {
this.inputRefs[i] = element;
}}
enterKeyHint="done"
/>
))}
</div>
Expand Down
8 changes: 1 addition & 7 deletions src/shared/components/home/login-reset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { setIsoData } from "@utils/app";
import { capitalizeFirstLetter, validEmail } from "@utils/helpers";
import { Component, linkEvent } from "inferno";
import { GetSiteResponse } from "lemmy-js-client";
import { HttpService, I18NextService, UserService } from "../../services";
import { HttpService, I18NextService } from "../../services";
import { toast } from "../../toast";
import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";
Expand Down Expand Up @@ -30,12 +30,6 @@ export class LoginReset extends Component<any, State> {
super(props, context);
}

componentDidMount() {
if (UserService.Instance.myUserInfo) {
this.context.router.history.push("/");
}
}

get documentTitle(): string {
return `${capitalizeFirstLetter(
I18NextService.i18n.t("forgot_password"),
Expand Down
7 changes: 0 additions & 7 deletions src/shared/components/home/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,6 @@ export class Login extends Component<
this.handleSubmitTotp = this.handleSubmitTotp.bind(this);
}

componentDidMount() {
// Navigate to home if already logged in
if (UserService.Instance.myUserInfo) {
this.context.router.history.push("/");
}
}

get documentTitle(): string {
return `${I18NextService.i18n.t("login")} - ${
this.state.siteRes.site_view.site.name
Expand Down
2 changes: 2 additions & 0 deletions src/shared/utils/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import updateCommunityBlock from "./update-community-block";
import updatePersonBlock from "./update-person-block";
import instanceToChoice from "./instance-to-choice";
import updateInstanceBlock from "./update-instance-block";
import isAnonymousPath from "./is-anonymous-path";

export {
buildCommentsTree,
Expand Down Expand Up @@ -112,4 +113,5 @@ export {
updatePersonBlock,
instanceToChoice,
updateInstanceBlock,
isAnonymousPath,
};
5 changes: 5 additions & 0 deletions src/shared/utils/app/is-anonymous-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function isAnonymousPath(pathname: string) {
return /^\/(login.*|signup|password_change.*|verify_email.*)\b/g.test(
pathname,
);
}