1 import { Component, linkEvent } from "inferno";
2 import { GetSiteResponse, LoginResponse } from "lemmy-js-client";
3 import { i18n } from "../../i18next";
4 import { UserService } from "../../services";
5 import { HttpService, RequestState } from "../../services/HttpService";
6 import { isBrowser, myAuth, setIsoData, toast, validEmail } from "../../utils";
7 import { HtmlTags } from "../common/html-tags";
8 import { Spinner } from "../common/icon";
11 loginRes: RequestState<LoginResponse>;
13 username_or_email?: string;
15 totp_2fa_token?: string;
18 siteRes: GetSiteResponse;
21 export class Login extends Component<any, State> {
22 private isoData = setIsoData(this.context);
25 loginRes: { state: "empty" },
28 siteRes: this.isoData.site_res,
31 constructor(props: any, context: any) {
32 super(props, context);
36 // Navigate to home if already logged in
37 if (UserService.Instance.myUserInfo) {
38 this.context.router.history.push("/");
42 get documentTitle(): string {
43 return `${i18n.t("login")} - ${this.state.siteRes.site_view.site.name}`;
46 get isLemmyMl(): boolean {
47 return isBrowser() && window.location.hostname == "lemmy.ml";
52 <div className="container-lg">
54 title={this.documentTitle}
55 path={this.context.router.route.match.url}
58 <div className="col-12 col-lg-6 offset-lg-3">{this.loginForm()}</div>
67 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
68 <h5>{i18n.t("login")}</h5>
69 <div className="form-group row">
71 className="col-sm-2 col-form-label"
72 htmlFor="login-email-or-username"
74 {i18n.t("email_or_username")}
76 <div className="col-sm-10">
79 className="form-control"
80 id="login-email-or-username"
81 value={this.state.form.username_or_email}
82 onInput={linkEvent(this, this.handleLoginUsernameChange)}
89 <div className="form-group row">
90 <label className="col-sm-2 col-form-label" htmlFor="login-password">
93 <div className="col-sm-10">
97 value={this.state.form.password}
98 onInput={linkEvent(this, this.handleLoginPasswordChange)}
99 className="form-control"
100 autoComplete="current-password"
106 onClick={linkEvent(this, this.handlePasswordReset)}
107 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
109 !!this.state.form.username_or_email &&
110 !validEmail(this.state.form.username_or_email)
112 title={i18n.t("no_password_reset")}
114 {i18n.t("forgot_password")}
118 {this.state.showTotp && (
119 <div className="form-group row">
121 className="col-sm-6 col-form-label"
122 htmlFor="login-totp-token"
124 {i18n.t("two_factor_token")}
126 <div className="col-sm-6">
130 className="form-control"
131 id="login-totp-token"
133 autoComplete="one-time-code"
134 value={this.state.form.totp_2fa_token}
135 onInput={linkEvent(this, this.handleLoginTotpChange)}
140 <div className="form-group row">
141 <div className="col-sm-10">
142 <button type="submit" className="btn btn-secondary">
143 {this.state.loginRes.state == "loading" ? (
156 async handleLoginSubmit(i: Login, event: any) {
157 event.preventDefault();
158 const { password, totp_2fa_token, username_or_email } = i.state.form;
160 if (username_or_email && password) {
161 i.setState({ loginRes: { state: "loading" } });
163 const loginRes = await HttpService.client.login({
168 switch (loginRes.state) {
170 if (loginRes.msg === "missing_totp_token") {
171 i.setState({ showTotp: true });
172 toast(i18n.t("enter_two_factor_code"), "info");
175 i.setState({ loginRes: { state: "empty" } });
180 UserService.Instance.login(loginRes.data);
181 const site = await HttpService.client.getSite({
185 if (site.state === "success") {
186 UserService.Instance.myUserInfo = site.data.my_user;
189 i.props.history.replace("/");
197 handleLoginUsernameChange(i: Login, event: any) {
198 i.state.form.username_or_email = event.target.value;
202 handleLoginTotpChange(i: Login, event: any) {
203 i.state.form.totp_2fa_token = event.target.value;
207 handleLoginPasswordChange(i: Login, event: any) {
208 i.state.form.password = event.target.value;
212 async handlePasswordReset(i: Login, event: any) {
213 event.preventDefault();
214 const email = i.state.form.username_or_email;
216 const res = await HttpService.client.passwordReset({ email });
217 if (res.state == "success") {
218 toast(i18n.t("reset_password_mail_sent"));