1 import { myAuth, setIsoData } from "@utils/app";
2 import { isBrowser } from "@utils/browser";
3 import { validEmail } from "@utils/helpers";
4 import { Component, linkEvent } from "inferno";
5 import { GetSiteResponse, LoginResponse } from "lemmy-js-client";
6 import { i18n } from "../../i18next";
7 import { UserService } from "../../services";
8 import { HttpService, RequestState } from "../../services/HttpService";
9 import { toast } from "../../toast";
10 import { HtmlTags } from "../common/html-tags";
11 import { Spinner } from "../common/icon";
14 loginRes: RequestState<LoginResponse>;
16 username_or_email?: string;
18 totp_2fa_token?: string;
21 siteRes: GetSiteResponse;
24 export class Login extends Component<any, State> {
25 private isoData = setIsoData(this.context);
28 loginRes: { state: "empty" },
31 siteRes: this.isoData.site_res,
34 constructor(props: any, context: any) {
35 super(props, context);
39 // Navigate to home if already logged in
40 if (UserService.Instance.myUserInfo) {
41 this.context.router.history.push("/");
45 get documentTitle(): string {
46 return `${i18n.t("login")} - ${this.state.siteRes.site_view.site.name}`;
49 get isLemmyMl(): boolean {
50 return isBrowser() && window.location.hostname == "lemmy.ml";
55 <div className="login container-lg">
57 title={this.documentTitle}
58 path={this.context.router.route.match.url}
61 <div className="col-12 col-lg-6 offset-lg-3">{this.loginForm()}</div>
70 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
71 <h5>{i18n.t("login")}</h5>
72 <div className="mb-3 row">
74 className="col-sm-2 col-form-label"
75 htmlFor="login-email-or-username"
77 {i18n.t("email_or_username")}
79 <div className="col-sm-10">
82 className="form-control"
83 id="login-email-or-username"
84 value={this.state.form.username_or_email}
85 onInput={linkEvent(this, this.handleLoginUsernameChange)}
92 <div className="mb-3 row">
93 <label className="col-sm-2 col-form-label" htmlFor="login-password">
96 <div className="col-sm-10">
100 value={this.state.form.password}
101 onInput={linkEvent(this, this.handleLoginPasswordChange)}
102 className="form-control"
103 autoComplete="current-password"
109 onClick={linkEvent(this, this.handlePasswordReset)}
110 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
112 !!this.state.form.username_or_email &&
113 !validEmail(this.state.form.username_or_email)
115 title={i18n.t("no_password_reset")}
117 {i18n.t("forgot_password")}
121 {this.state.showTotp && (
122 <div className="mb-3 row">
124 className="col-sm-6 col-form-label"
125 htmlFor="login-totp-token"
127 {i18n.t("two_factor_token")}
129 <div className="col-sm-6">
133 className="form-control"
134 id="login-totp-token"
136 autoComplete="one-time-code"
137 value={this.state.form.totp_2fa_token}
138 onInput={linkEvent(this, this.handleLoginTotpChange)}
143 <div className="mb-3 row">
144 <div className="col-sm-10">
145 <button type="submit" className="btn btn-secondary">
146 {this.state.loginRes.state == "loading" ? (
159 async handleLoginSubmit(i: Login, event: any) {
160 event.preventDefault();
161 const { password, totp_2fa_token, username_or_email } = i.state.form;
163 if (username_or_email && password) {
164 i.setState({ loginRes: { state: "loading" } });
166 const loginRes = await HttpService.client.login({
171 switch (loginRes.state) {
173 if (loginRes.msg === "missing_totp_token") {
174 i.setState({ showTotp: true });
175 toast(i18n.t("enter_two_factor_code"), "info");
178 i.setState({ loginRes: { state: "failed", msg: loginRes.msg } });
183 UserService.Instance.login(loginRes.data);
184 const site = await HttpService.client.getSite({
188 if (site.state === "success") {
189 UserService.Instance.myUserInfo = site.data.my_user;
192 i.props.history.action === "PUSH"
193 ? i.props.history.back()
194 : i.props.history.replace("/");
202 handleLoginUsernameChange(i: Login, event: any) {
203 i.state.form.username_or_email = event.target.value.trim();
207 handleLoginTotpChange(i: Login, event: any) {
208 i.state.form.totp_2fa_token = event.target.value;
212 handleLoginPasswordChange(i: Login, event: any) {
213 i.state.form.password = event.target.value;
217 async handlePasswordReset(i: Login, event: any) {
218 event.preventDefault();
219 const email = i.state.form.username_or_email;
221 const res = await HttpService.client.passwordReset({ email });
222 if (res.state == "success") {
223 toast(i18n.t("reset_password_mail_sent"));