1 import { isBrowser } from "@utils/browser";
2 import { Component, linkEvent } from "inferno";
3 import { GetSiteResponse, LoginResponse } from "lemmy-js-client";
4 import { i18n } from "../../i18next";
5 import { UserService } from "../../services";
6 import { HttpService, RequestState } from "../../services/HttpService";
7 import { myAuth, setIsoData, toast, validEmail } from "../../utils";
8 import { HtmlTags } from "../common/html-tags";
9 import { Spinner } from "../common/icon";
12 loginRes: RequestState<LoginResponse>;
14 username_or_email?: string;
16 totp_2fa_token?: string;
19 siteRes: GetSiteResponse;
22 export class Login extends Component<any, State> {
23 private isoData = setIsoData(this.context);
26 loginRes: { state: "empty" },
29 siteRes: this.isoData.site_res,
32 constructor(props: any, context: any) {
33 super(props, context);
37 // Navigate to home if already logged in
38 if (UserService.Instance.myUserInfo) {
39 this.context.router.history.push("/");
43 get documentTitle(): string {
44 return `${i18n.t("login")} - ${this.state.siteRes.site_view.site.name}`;
47 get isLemmyMl(): boolean {
48 return isBrowser() && window.location.hostname == "lemmy.ml";
53 <div className="container-lg">
55 title={this.documentTitle}
56 path={this.context.router.route.match.url}
59 <div className="col-12 col-lg-6 offset-lg-3">{this.loginForm()}</div>
68 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
69 <h5>{i18n.t("login")}</h5>
70 <div className="form-group row">
72 className="col-sm-2 col-form-label"
73 htmlFor="login-email-or-username"
75 {i18n.t("email_or_username")}
77 <div className="col-sm-10">
80 className="form-control"
81 id="login-email-or-username"
82 value={this.state.form.username_or_email}
83 onInput={linkEvent(this, this.handleLoginUsernameChange)}
90 <div className="form-group row">
91 <label className="col-sm-2 col-form-label" htmlFor="login-password">
94 <div className="col-sm-10">
98 value={this.state.form.password}
99 onInput={linkEvent(this, this.handleLoginPasswordChange)}
100 className="form-control"
101 autoComplete="current-password"
107 onClick={linkEvent(this, this.handlePasswordReset)}
108 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
110 !!this.state.form.username_or_email &&
111 !validEmail(this.state.form.username_or_email)
113 title={i18n.t("no_password_reset")}
115 {i18n.t("forgot_password")}
119 {this.state.showTotp && (
120 <div className="form-group row">
122 className="col-sm-6 col-form-label"
123 htmlFor="login-totp-token"
125 {i18n.t("two_factor_token")}
127 <div className="col-sm-6">
131 className="form-control"
132 id="login-totp-token"
134 autoComplete="one-time-code"
135 value={this.state.form.totp_2fa_token}
136 onInput={linkEvent(this, this.handleLoginTotpChange)}
141 <div className="form-group row">
142 <div className="col-sm-10">
143 <button type="submit" className="btn btn-secondary">
144 {this.state.loginRes.state == "loading" ? (
157 async handleLoginSubmit(i: Login, event: any) {
158 event.preventDefault();
159 const { password, totp_2fa_token, username_or_email } = i.state.form;
161 if (username_or_email && password) {
162 i.setState({ loginRes: { state: "loading" } });
164 const loginRes = await HttpService.client.login({
169 switch (loginRes.state) {
171 if (loginRes.msg === "missing_totp_token") {
172 i.setState({ showTotp: true });
173 toast(i18n.t("enter_two_factor_code"), "info");
176 i.setState({ loginRes: { state: "failed", msg: loginRes.msg } });
181 UserService.Instance.login(loginRes.data);
182 const site = await HttpService.client.getSite({
186 if (site.state === "success") {
187 UserService.Instance.myUserInfo = site.data.my_user;
190 i.props.history.action === "PUSH"
191 ? i.props.history.back()
192 : i.props.history.replace("/");
200 handleLoginUsernameChange(i: Login, event: any) {
201 i.state.form.username_or_email = event.target.value.trim();
205 handleLoginTotpChange(i: Login, event: any) {
206 i.state.form.totp_2fa_token = event.target.value;
210 handleLoginPasswordChange(i: Login, event: any) {
211 i.state.form.password = event.target.value;
215 async handlePasswordReset(i: Login, event: any) {
216 event.preventDefault();
217 const email = i.state.form.username_or_email;
219 const res = await HttpService.client.passwordReset({ email });
220 if (res.state == "success") {
221 toast(i18n.t("reset_password_mail_sent"));