2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2023-06-14 12:20:40 +00:00
|
|
|
import { GetSiteResponse, LoginResponse } from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { i18n } from "../../i18next";
|
2023-06-14 12:20:40 +00:00
|
|
|
import { HttpService, UserService } from "../../services";
|
|
|
|
import { RequestState } from "../../services/HttpService";
|
|
|
|
import { capitalizeFirstLetter, myAuth, setIsoData } from "../../utils";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { HtmlTags } from "../common/html-tags";
|
|
|
|
import { Spinner } from "../common/icon";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface State {
|
2023-06-14 12:20:40 +00:00
|
|
|
passwordChangeRes: RequestState<LoginResponse>;
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {
|
|
|
|
token: string;
|
|
|
|
password?: string;
|
|
|
|
password_verify?: string;
|
|
|
|
};
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: GetSiteResponse;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PasswordChange extends Component<any, State> {
|
2020-09-09 03:13:26 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
state: State = {
|
2023-06-14 12:20:40 +00:00
|
|
|
passwordChangeRes: { state: "empty" },
|
|
|
|
siteRes: this.isoData.site_res,
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {
|
2020-09-06 16:15:25 +00:00
|
|
|
token: this.props.match.params.token,
|
2023-01-04 16:56:24 +00:00
|
|
|
},
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2022-11-09 19:53:07 +00:00
|
|
|
return `${i18n.t("password_change")} - ${
|
|
|
|
this.state.siteRes.site_view.site.name
|
|
|
|
}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2022-10-03 18:16:36 +00:00
|
|
|
<div className="container-lg">
|
2020-09-11 18:09:21 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
/>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="row">
|
|
|
|
<div className="col-12 col-lg-6 offset-lg-3 mb-4">
|
2021-02-22 02:39:04 +00:00
|
|
|
<h5>{i18n.t("password_change")}</h5>
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.passwordChangeForm()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
passwordChangeForm() {
|
|
|
|
return (
|
|
|
|
<form onSubmit={linkEvent(this, this.handlePasswordChangeSubmit)}>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="new-password">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("new_password")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2020-09-06 16:15:25 +00:00
|
|
|
<input
|
2021-02-06 20:20:41 +00:00
|
|
|
id="new-password"
|
2020-09-06 16:15:25 +00:00
|
|
|
type="password"
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.form.password}
|
2020-09-06 16:15:25 +00:00
|
|
|
onInput={linkEvent(this, this.handlePasswordChange)}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control"
|
2020-09-06 16:15:25 +00:00
|
|
|
required
|
2021-03-01 17:38:03 +00:00
|
|
|
maxLength={60}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="verify-password">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("verify_password")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2020-09-06 16:15:25 +00:00
|
|
|
<input
|
2021-02-06 20:20:41 +00:00
|
|
|
id="verify-password"
|
2020-09-06 16:15:25 +00:00
|
|
|
type="password"
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.form.password_verify}
|
2020-09-06 16:15:25 +00:00
|
|
|
onInput={linkEvent(this, this.handleVerifyPasswordChange)}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control"
|
2020-09-06 16:15:25 +00:00
|
|
|
required
|
2021-03-01 17:38:03 +00:00
|
|
|
maxLength={60}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<div className="col-sm-10">
|
|
|
|
<button type="submit" className="btn btn-secondary">
|
2023-06-14 12:20:40 +00:00
|
|
|
{this.state.passwordChangeRes.state == "loading" ? (
|
2021-02-11 20:35:27 +00:00
|
|
|
<Spinner />
|
2020-09-06 16:15:25 +00:00
|
|
|
) : (
|
2021-02-22 02:39:04 +00:00
|
|
|
capitalizeFirstLetter(i18n.t("save"))
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePasswordChange(i: PasswordChange, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.state.form.password = event.target.value;
|
2020-09-06 16:15:25 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleVerifyPasswordChange(i: PasswordChange, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.state.form.password_verify = event.target.value;
|
2020-09-06 16:15:25 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async handlePasswordChangeSubmit(i: PasswordChange, event: any) {
|
2020-09-06 16:15:25 +00:00
|
|
|
event.preventDefault();
|
2023-06-14 12:20:40 +00:00
|
|
|
i.setState({ passwordChangeRes: { state: "loading" } });
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-06-05 21:31:12 +00:00
|
|
|
const password = i.state.form.password;
|
|
|
|
const password_verify = i.state.form.password_verify;
|
2023-01-04 16:56:24 +00:00
|
|
|
|
|
|
|
if (password && password_verify) {
|
2023-06-14 12:20:40 +00:00
|
|
|
i.setState({
|
|
|
|
passwordChangeRes: await HttpService.client.passwordChangeAfterReset({
|
|
|
|
token: i.state.form.token,
|
|
|
|
password,
|
|
|
|
password_verify,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (i.state.passwordChangeRes.state === "success") {
|
|
|
|
const data = i.state.passwordChangeRes.data;
|
|
|
|
UserService.Instance.login(data);
|
|
|
|
|
|
|
|
const site = await HttpService.client.getSite({ auth: myAuth() });
|
|
|
|
if (site.state === "success") {
|
|
|
|
UserService.Instance.myUserInfo = site.data.my_user;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props.history.replace("/");
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|