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

Move password reset form to separate route, view #1390

Merged
merged 17 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions src/server/handlers/robots-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default async ({ res }: { res: Response }) => {

res.send(`User-Agent: *
Disallow: /login
Disallow: /login_reset
Disallow: /settings
Disallow: /create_community
Disallow: /create_post
Expand Down
147 changes: 147 additions & 0 deletions src/shared/components/home/login-reset.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { Component, linkEvent } from "inferno";
import { GetSiteResponse } from "lemmy-js-client";
import { i18n } from "../../i18next";
import { UserService } from "../../services";
import { HttpService } from "../../services/HttpService";
import {
capitalizeFirstLetter,
isBrowser,
setIsoData,
toast,
validEmail,
} from "../../utils";
import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";

interface State {
form: {
email: string;
};
alectrocute marked this conversation as resolved.
Show resolved Hide resolved
loading: boolean;
siteRes: GetSiteResponse;
}

export class LoginReset extends Component<any, State> {
private isoData = setIsoData(this.context);

state: State = {
form: {
email: "",
},
loading: false,
siteRes: this.isoData.site_res,
};

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

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

get documentTitle(): string {
return `${capitalizeFirstLetter(i18n.t("forgot_password"))} - ${
this.state.siteRes.site_view.site.name
}`;
}

get isLemmyMl(): boolean {
return isBrowser() && window.location.hostname == "lemmy.ml";
}

render() {
return (
<div className="container-lg">
<HtmlTags
title={this.documentTitle}
path={this.context.router.route.match.url}
/>
<div className="row">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's only going to be one row, why not make the parent div have a className of container-lg row and get rid of this element?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primarily to make it match up with /login. I've refactored this in my upcoming changes.

<div className="col-12 col-lg-6 offset-lg-3">
{this.loginResetForm()}
</div>
</div>
</div>
);
}

loginResetForm() {
return (
<div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this div necessary?

<form onSubmit={linkEvent(this, this.handlePasswordReset)}>
<h5>{capitalizeFirstLetter(i18n.t("forgot_password"))}</h5>

<div className="form-group row">
<label className="col-form-label col-sm-10">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to be a form-group with a label as a child? I typically think of label elements being associated with input elements.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the bootstrap 5 stuff gets merged shortly, it'll have to conform to the way it does form anyway, and might need changes.

{i18n.t("no_password_reset")}
</label>
</div>

<div className="form-group row">
<label
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would this label look like if it was a child of the same div as the input its for?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as it follows the bootstrap conventions, it should be okay.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would look like this @SleeplessOne1917

Screenshot 2023-06-20 at 3 20 42 PM

className="col-sm-2 col-form-label"
htmlFor="login-email-or-username"
>
{i18n.t("email")}
</label>

<div className="col-sm-10">
<input
type="text"
className="form-control"
id="login-email-or-username"
value={this.state.form.email}
onInput={linkEvent(this, this.handleEmailInputChange)}
autoComplete="email"
required
minLength={3}
/>
</div>
</div>

<div className="form-group row">
<div className="col-sm-10">
<button
type="button"
onClick={linkEvent(this, this.handlePasswordReset)}
className="btn btn-secondary"
disabled={
!validEmail(this.state.form.email) || this.state.loading
}
>
{this.state.loading ? <Spinner /> : i18n.t("reset_password")}
</button>
</div>
</div>
</form>
</div>
);
}

handleEmailInputChange(i: LoginReset, event: any) {
i.state.form.email = event.target.value.trim();
i.setState(i.state);
SleeplessOne1917 marked this conversation as resolved.
Show resolved Hide resolved
}

async handlePasswordReset(i: LoginReset, event: any) {
event.preventDefault();

const email = i.state.form.email;

if (email && validEmail(email)) {
i.setState({ loading: true });

const res = await HttpService.client.passwordReset({ email });

if (res.state == "success") {
toast(i18n.t("reset_password_mail_sent"));
this.context.router.history.push("/login");
}

i.setState({ loading: false });
}
}
SleeplessOne1917 marked this conversation as resolved.
Show resolved Hide resolved
}
26 changes: 5 additions & 21 deletions src/shared/components/home/login.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component, linkEvent } from "inferno";
import { NavLink } from "inferno-router";
import { GetSiteResponse, LoginResponse } from "lemmy-js-client";
import { i18n } from "../../i18next";
import { UserService } from "../../services";
import { HttpService, RequestState } from "../../services/HttpService";
import { isBrowser, myAuth, setIsoData, toast, validEmail } from "../../utils";
import { isBrowser, myAuth, setIsoData, toast } from "../../utils";
import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";

Expand Down Expand Up @@ -101,18 +102,12 @@ export class Login extends Component<any, State> {
required
maxLength={60}
/>
<button
type="button"
onClick={linkEvent(this, this.handlePasswordReset)}
<NavLink
className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
disabled={
!!this.state.form.username_or_email &&
!validEmail(this.state.form.username_or_email)
}
title={i18n.t("no_password_reset")}
to="/login_reset"
>
{i18n.t("forgot_password")}
</button>
</NavLink>
</div>
</div>
{this.state.showTotp && (
Expand Down Expand Up @@ -210,15 +205,4 @@ export class Login extends Component<any, State> {
i.state.form.password = event.target.value;
i.setState(i.state);
}

async handlePasswordReset(i: Login, event: any) {
event.preventDefault();
const email = i.state.form.username_or_email;
if (email) {
const res = await HttpService.client.passwordReset({ email });
if (res.state == "success") {
toast(i18n.t("reset_password_mail_sent"));
}
}
}
}
5 changes: 5 additions & 0 deletions src/shared/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Home } from "./components/home/home";
import { Instances } from "./components/home/instances";
import { Legal } from "./components/home/legal";
import { Login } from "./components/home/login";
import { LoginReset } from "./components/home/login-reset";
import { Setup } from "./components/home/setup";
import { Signup } from "./components/home/signup";
import { Modlog } from "./components/modlog";
Expand Down Expand Up @@ -38,6 +39,10 @@ export const routes: IRoutePropsWithFetch<Record<string, any>>[] = [
path: `/login`,
component: Login,
},
{
path: `/login_reset`,
component: LoginReset,
},
{
path: `/signup`,
component: Signup,
Expand Down