2021-07-17 20:42:55 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
|
|
|
import {
|
|
|
|
GetSiteResponse,
|
2023-01-04 16:56:24 +00:00
|
|
|
Login as LoginI,
|
2021-07-17 20:42:55 +00:00
|
|
|
LoginResponse,
|
|
|
|
PasswordReset,
|
|
|
|
UserOperation,
|
2022-06-21 21:42:29 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsUserOp,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "lemmy-js-client";
|
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { UserService, WebSocketService } from "../../services";
|
|
|
|
import {
|
|
|
|
isBrowser,
|
|
|
|
setIsoData,
|
|
|
|
toast,
|
|
|
|
validEmail,
|
|
|
|
wsClient,
|
|
|
|
wsSubscribe,
|
|
|
|
} from "../../utils";
|
|
|
|
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-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
|
|
|
};
|
2021-07-17 20:42:55 +00:00
|
|
|
loginLoading: boolean;
|
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
|
|
|
private subscription?: Subscription;
|
2021-07-17 20:42:55 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
state: State = {
|
|
|
|
form: {},
|
2021-07-17 20:42:55 +00:00
|
|
|
loginLoading: false,
|
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);
|
|
|
|
|
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
|
|
|
|
|
|
|
if (isBrowser()) {
|
2023-05-11 18:32:32 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.getCaptcha({}));
|
2021-07-17 20:42:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
componentWillUnmount() {
|
|
|
|
if (isBrowser()) {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.subscription?.unsubscribe();
|
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">
|
2021-07-17 20:42:55 +00:00
|
|
|
{this.state.loginLoading ? <Spinner /> : i18n.t("login")}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleLoginSubmit(i: Login, event: any) {
|
|
|
|
event.preventDefault();
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ loginLoading: true });
|
2023-01-04 16:56:24 +00:00
|
|
|
let lForm = i.state.form;
|
|
|
|
let username_or_email = lForm.username_or_email;
|
|
|
|
let password = lForm.password;
|
2023-03-02 23:30:38 +00:00
|
|
|
let totp_2fa_token = lForm.totp_2fa_token;
|
2023-01-04 16:56:24 +00:00
|
|
|
if (username_or_email && password) {
|
|
|
|
let form: LoginI = {
|
|
|
|
username_or_email,
|
|
|
|
password,
|
2023-03-02 23:30:38 +00:00
|
|
|
totp_2fa_token,
|
2023-01-04 16:56:24 +00:00
|
|
|
};
|
|
|
|
WebSocketService.Instance.send(wsClient.login(form));
|
|
|
|
}
|
2021-07-17 20:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleLoginUsernameChange(i: Login, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.state.form.username_or_email = event.target.value;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePasswordReset(i: Login, event: any) {
|
|
|
|
event.preventDefault();
|
2023-01-04 16:56:24 +00:00
|
|
|
let email = i.state.form.username_or_email;
|
|
|
|
if (email) {
|
|
|
|
let resetForm: PasswordReset = { email };
|
|
|
|
WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
|
|
|
|
}
|
2021-07-17 20:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parseMessage(msg: any) {
|
|
|
|
let op = wsUserOp(msg);
|
|
|
|
console.log(msg);
|
|
|
|
if (msg.error) {
|
2023-03-02 23:30:38 +00:00
|
|
|
// If the error comes back that the token is missing, show the TOTP field
|
|
|
|
if (msg.error == "missing_totp_token") {
|
|
|
|
this.setState({ showTotp: true, loginLoading: false });
|
|
|
|
toast(i18n.t("enter_two_factor_code"));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
toast(i18n.t(msg.error), "danger");
|
|
|
|
this.setState({ form: {}, loginLoading: false });
|
|
|
|
return;
|
|
|
|
}
|
2021-07-17 20:42:55 +00:00
|
|
|
} else {
|
|
|
|
if (op == UserOperation.Login) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let data = wsJsonToRes<LoginResponse>(msg);
|
2021-07-17 20:42:55 +00:00
|
|
|
UserService.Instance.login(data);
|
2022-11-09 19:53:07 +00:00
|
|
|
this.props.history.push("/");
|
|
|
|
location.reload();
|
2021-07-17 20:42:55 +00:00
|
|
|
} else if (op == UserOperation.PasswordReset) {
|
|
|
|
toast(i18n.t("reset_password_mail_sent"));
|
|
|
|
} else if (op == UserOperation.GetSite) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let data = wsJsonToRes<GetSiteResponse>(msg);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ siteRes: data });
|
2021-07-17 20:42:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|