1 import { Component, linkEvent } from "inferno";
10 } from "lemmy-js-client";
11 import { Subscription } from "rxjs";
12 import { i18n } from "../../i18next";
13 import { UserService, WebSocketService } from "../../services";
22 import { HtmlTags } from "../common/html-tags";
23 import { Spinner } from "../common/icon";
27 username_or_email?: string;
29 totp_2fa_token?: string;
31 loginLoading: boolean;
33 siteRes: GetSiteResponse;
36 export class Login extends Component<any, State> {
37 private isoData = setIsoData(this.context);
38 private subscription?: Subscription;
44 siteRes: this.isoData.site_res,
47 constructor(props: any, context: any) {
48 super(props, context);
50 this.parseMessage = this.parseMessage.bind(this);
51 this.subscription = wsSubscribe(this.parseMessage);
54 WebSocketService.Instance.send(wsClient.getCaptcha({}));
59 // Navigate to home if already logged in
60 if (UserService.Instance.myUserInfo) {
61 this.context.router.history.push("/");
65 componentWillUnmount() {
67 this.subscription?.unsubscribe();
71 get documentTitle(): string {
72 return `${i18n.t("login")} - ${this.state.siteRes.site_view.site.name}`;
75 get isLemmyMl(): boolean {
76 return isBrowser() && window.location.hostname == "lemmy.ml";
81 <div className="container-lg">
83 title={this.documentTitle}
84 path={this.context.router.route.match.url}
87 <div className="col-12 col-lg-6 offset-lg-3">{this.loginForm()}</div>
96 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
97 <h5>{i18n.t("login")}</h5>
98 <div className="form-group row">
100 className="col-sm-2 col-form-label"
101 htmlFor="login-email-or-username"
103 {i18n.t("email_or_username")}
105 <div className="col-sm-10">
108 className="form-control"
109 id="login-email-or-username"
110 value={this.state.form.username_or_email}
111 onInput={linkEvent(this, this.handleLoginUsernameChange)}
118 <div className="form-group row">
119 <label className="col-sm-2 col-form-label" htmlFor="login-password">
122 <div className="col-sm-10">
126 value={this.state.form.password}
127 onInput={linkEvent(this, this.handleLoginPasswordChange)}
128 className="form-control"
129 autoComplete="current-password"
135 onClick={linkEvent(this, this.handlePasswordReset)}
136 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
138 !!this.state.form.username_or_email &&
139 !validEmail(this.state.form.username_or_email)
141 title={i18n.t("no_password_reset")}
143 {i18n.t("forgot_password")}
147 {this.state.showTotp && (
148 <div className="form-group row">
150 className="col-sm-6 col-form-label"
151 htmlFor="login-totp-token"
153 {i18n.t("two_factor_token")}
155 <div className="col-sm-6">
159 className="form-control"
160 id="login-totp-token"
162 autoComplete="one-time-code"
163 value={this.state.form.totp_2fa_token}
164 onInput={linkEvent(this, this.handleLoginTotpChange)}
169 <div className="form-group row">
170 <div className="col-sm-10">
171 <button type="submit" className="btn btn-secondary">
172 {this.state.loginLoading ? <Spinner /> : i18n.t("login")}
181 handleLoginSubmit(i: Login, event: any) {
182 event.preventDefault();
183 i.setState({ loginLoading: true });
184 let lForm = i.state.form;
185 let username_or_email = lForm.username_or_email;
186 let password = lForm.password;
187 let totp_2fa_token = lForm.totp_2fa_token;
188 if (username_or_email && password) {
194 WebSocketService.Instance.send(wsClient.login(form));
198 handleLoginUsernameChange(i: Login, event: any) {
199 i.state.form.username_or_email = event.target.value;
203 handleLoginTotpChange(i: Login, event: any) {
204 i.state.form.totp_2fa_token = event.target.value;
208 handleLoginPasswordChange(i: Login, event: any) {
209 i.state.form.password = event.target.value;
213 handlePasswordReset(i: Login, event: any) {
214 event.preventDefault();
215 let email = i.state.form.username_or_email;
217 let resetForm: PasswordReset = { email };
218 WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
222 parseMessage(msg: any) {
223 let op = wsUserOp(msg);
226 // If the error comes back that the token is missing, show the TOTP field
227 if (msg.error == "missing_totp_token") {
228 this.setState({ showTotp: true, loginLoading: false });
229 toast(i18n.t("enter_two_factor_code"));
232 toast(i18n.t(msg.error), "danger");
233 this.setState({ form: {}, loginLoading: false });
237 if (op == UserOperation.Login) {
238 let data = wsJsonToRes<LoginResponse>(msg);
239 UserService.Instance.login(data);
240 this.props.history.push("/");
242 } else if (op == UserOperation.PasswordReset) {
243 toast(i18n.t("reset_password_mail_sent"));
244 } else if (op == UserOperation.GetSite) {
245 let data = wsJsonToRes<GetSiteResponse>(msg);
246 this.setState({ siteRes: data });