2021-07-17 20:42:55 +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 { UserService } from "../../services";
|
|
|
|
import { HttpService, RequestState } from "../../services/HttpService";
|
|
|
|
import { isBrowser, myAuth, setIsoData, toast, validEmail } from "../../utils";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { HtmlTags } from "../common/html-tags";
|
2021-09-19 20:31:17 +00:00
|
|
|
import { Spinner } from "../common/icon";
|
2021-09-19 19:32:51 +00:00
|
|
|
|
2021-07-17 20:42:55 +00:00
|
|
|
interface State {
|
2023-06-14 12:20:40 +00:00
|
|
|
loginRes: RequestState<LoginResponse>;
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {
|
|
|
|
username_or_email?: string;
|
|
|
|
password?: string;
|
2023-03-02 23:30:38 +00:00
|
|
|
totp_2fa_token?: string;
|
2023-01-04 16:56:24 +00:00
|
|
|
};
|
2023-03-02 23:30:38 +00:00
|
|
|
showTotp: boolean;
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: GetSiteResponse;
|
2021-07-17 20:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Login extends Component<any, State> {
|
|
|
|
private isoData = setIsoData(this.context);
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
state: State = {
|
2023-06-14 12:20:40 +00:00
|
|
|
loginRes: { state: "empty" },
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {},
|
2023-03-02 23:30:38 +00:00
|
|
|
showTotp: false,
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2021-07-17 20:42:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
}
|
|
|
|
|
2021-09-19 20:28:44 +00:00
|
|
|
componentDidMount() {
|
|
|
|
// Navigate to home if already logged in
|
2023-01-04 16:56:24 +00:00
|
|
|
if (UserService.Instance.myUserInfo) {
|
2021-09-19 20:28:44 +00:00
|
|
|
this.context.router.history.push("/");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 20:42:55 +00:00
|
|
|
get documentTitle(): string {
|
2022-11-09 19:53:07 +00:00
|
|
|
return `${i18n.t("login")} - ${this.state.siteRes.site_view.site.name}`;
|
2021-07-17 20:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get isLemmyMl(): boolean {
|
|
|
|
return isBrowser() && window.location.hostname == "lemmy.ml";
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2022-10-03 18:16:36 +00:00
|
|
|
<div className="container-lg">
|
2021-07-17 20:42:55 +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">{this.loginForm()}</div>
|
2021-07-17 20:42:55 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
loginForm() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
|
|
|
|
<h5>{i18n.t("login")}</h5>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
2021-07-17 20:42:55 +00:00
|
|
|
<label
|
2022-09-22 15:03:35 +00:00
|
|
|
className="col-sm-2 col-form-label"
|
2021-07-17 20:42:55 +00:00
|
|
|
htmlFor="login-email-or-username"
|
|
|
|
>
|
|
|
|
{i18n.t("email_or_username")}
|
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2021-07-17 20:42:55 +00:00
|
|
|
<input
|
|
|
|
type="text"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control"
|
2021-07-17 20:42:55 +00:00
|
|
|
id="login-email-or-username"
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.form.username_or_email}
|
2021-07-17 20:42:55 +00:00
|
|
|
onInput={linkEvent(this, this.handleLoginUsernameChange)}
|
|
|
|
autoComplete="email"
|
|
|
|
required
|
|
|
|
minLength={3}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="login-password">
|
2021-07-17 20:42:55 +00:00
|
|
|
{i18n.t("password")}
|
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2021-07-17 20:42:55 +00:00
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
id="login-password"
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.form.password}
|
2021-07-17 20:42:55 +00:00
|
|
|
onInput={linkEvent(this, this.handleLoginPasswordChange)}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control"
|
2021-07-17 20:42:55 +00:00
|
|
|
autoComplete="current-password"
|
|
|
|
required
|
|
|
|
maxLength={60}
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={linkEvent(this, this.handlePasswordReset)}
|
|
|
|
className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
|
2023-01-04 16:56:24 +00:00
|
|
|
disabled={
|
|
|
|
!!this.state.form.username_or_email &&
|
|
|
|
!validEmail(this.state.form.username_or_email)
|
|
|
|
}
|
2021-07-17 20:42:55 +00:00
|
|
|
title={i18n.t("no_password_reset")}
|
|
|
|
>
|
|
|
|
{i18n.t("forgot_password")}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-03-02 23:30:38 +00:00
|
|
|
{this.state.showTotp && (
|
|
|
|
<div className="form-group row">
|
|
|
|
<label
|
|
|
|
className="col-sm-6 col-form-label"
|
|
|
|
htmlFor="login-totp-token"
|
|
|
|
>
|
|
|
|
{i18n.t("two_factor_token")}
|
|
|
|
</label>
|
|
|
|
<div className="col-sm-6">
|
|
|
|
<input
|
|
|
|
type="number"
|
|
|
|
inputMode="numeric"
|
|
|
|
className="form-control"
|
|
|
|
id="login-totp-token"
|
|
|
|
pattern="[0-9]*"
|
|
|
|
autoComplete="one-time-code"
|
|
|
|
value={this.state.form.totp_2fa_token}
|
|
|
|
onInput={linkEvent(this, this.handleLoginTotpChange)}
|
|
|
|
/>
|
|
|
|
</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.loginRes.state == "loading" ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
i18n.t("login")
|
|
|
|
)}
|
2021-07-17 20:42:55 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async handleLoginSubmit(i: Login, event: any) {
|
2021-07-17 20:42:55 +00:00
|
|
|
event.preventDefault();
|
2023-06-14 12:20:40 +00:00
|
|
|
const { password, totp_2fa_token, username_or_email } = i.state.form;
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
if (username_or_email && password) {
|
2023-06-14 12:20:40 +00:00
|
|
|
i.setState({ loginRes: { state: "loading" } });
|
|
|
|
|
|
|
|
const loginRes = await HttpService.client.login({
|
2023-01-04 16:56:24 +00:00
|
|
|
username_or_email,
|
|
|
|
password,
|
2023-03-02 23:30:38 +00:00
|
|
|
totp_2fa_token,
|
2023-06-14 12:20:40 +00:00
|
|
|
});
|
|
|
|
switch (loginRes.state) {
|
|
|
|
case "failed": {
|
|
|
|
if (loginRes.msg === "missing_totp_token") {
|
|
|
|
i.setState({ showTotp: true });
|
|
|
|
toast(i18n.t("enter_two_factor_code"), "info");
|
|
|
|
}
|
|
|
|
|
|
|
|
i.setState({ loginRes: { state: "empty" } });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "success": {
|
|
|
|
UserService.Instance.login(loginRes.data);
|
|
|
|
const site = await HttpService.client.getSite({
|
|
|
|
auth: myAuth(),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (site.state === "success") {
|
|
|
|
UserService.Instance.myUserInfo = site.data.my_user;
|
|
|
|
}
|
|
|
|
|
2023-06-15 16:29:24 +00:00
|
|
|
i.props.history.action === "PUSH"
|
|
|
|
? i.props.history.back()
|
2023-06-15 16:38:42 +00:00
|
|
|
: i.props.history.replace("/");
|
2023-06-14 12:20:40 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2021-07-17 20:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleLoginUsernameChange(i: Login, event: any) {
|
2023-06-14 13:47:33 +00:00
|
|
|
i.state.form.username_or_email = event.target.value.trim();
|
2021-07-17 20:42:55 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2023-03-02 23:30:38 +00:00
|
|
|
handleLoginTotpChange(i: Login, event: any) {
|
|
|
|
i.state.form.totp_2fa_token = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2021-07-17 20:42:55 +00:00
|
|
|
handleLoginPasswordChange(i: Login, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.state.form.password = event.target.value;
|
2021-07-17 20:42:55 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async handlePasswordReset(i: Login, event: any) {
|
2021-07-17 20:42:55 +00:00
|
|
|
event.preventDefault();
|
2023-06-05 21:31:12 +00:00
|
|
|
const email = i.state.form.username_or_email;
|
2023-01-04 16:56:24 +00:00
|
|
|
if (email) {
|
2023-06-14 12:20:40 +00:00
|
|
|
const res = await HttpService.client.passwordReset({ email });
|
|
|
|
if (res.state == "success") {
|
2021-07-17 20:42:55 +00:00
|
|
|
toast(i18n.t("reset_password_mail_sent"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|