1 import { myAuth, setIsoData } from "@utils/app";
2 import { isBrowser } from "@utils/browser";
3 import { Component, linkEvent } from "inferno";
4 import { NavLink } from "inferno-router";
5 import { GetSiteResponse, LoginResponse } from "lemmy-js-client";
6 import { I18NextService, UserService } from "../../services";
7 import { HttpService, RequestState } from "../../services/HttpService";
8 import { toast } from "../../toast";
9 import { HtmlTags } from "../common/html-tags";
10 import { Spinner } from "../common/icon";
13 loginRes: RequestState<LoginResponse>;
15 username_or_email?: string;
17 totp_2fa_token?: string;
20 siteRes: GetSiteResponse;
23 export class Login extends Component<any, State> {
24 private isoData = setIsoData(this.context);
27 loginRes: { state: "empty" },
30 siteRes: this.isoData.site_res,
33 constructor(props: any, context: any) {
34 super(props, context);
38 // Navigate to home if already logged in
39 if (UserService.Instance.myUserInfo) {
40 this.context.router.history.push("/");
44 get documentTitle(): string {
45 return `${I18NextService.i18n.t("login")} - ${
46 this.state.siteRes.site_view.site.name
50 get isLemmyMl(): boolean {
51 return isBrowser() && window.location.hostname == "lemmy.ml";
56 <div className="login container-lg">
58 title={this.documentTitle}
59 path={this.context.router.route.match.url}
62 <div className="col-12 col-lg-6 offset-lg-3">{this.loginForm()}</div>
71 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
72 <h1 className="h4 mb-4">{I18NextService.i18n.t("login")}</h1>
73 <div className="mb-3 row">
75 className="col-sm-2 col-form-label"
76 htmlFor="login-email-or-username"
78 {I18NextService.i18n.t("email_or_username")}
80 <div className="col-sm-10">
83 className="form-control"
84 id="login-email-or-username"
85 value={this.state.form.username_or_email}
86 onInput={linkEvent(this, this.handleLoginUsernameChange)}
93 <div className="mb-3 row">
94 <label className="col-sm-2 col-form-label" htmlFor="login-password">
95 {I18NextService.i18n.t("password")}
97 <div className="col-sm-10">
101 value={this.state.form.password}
102 onInput={linkEvent(this, this.handleLoginPasswordChange)}
103 className="form-control"
104 autoComplete="current-password"
109 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
112 {I18NextService.i18n.t("forgot_password")}
116 {this.state.showTotp && (
117 <div className="mb-3 row">
119 className="col-sm-6 col-form-label"
120 htmlFor="login-totp-token"
122 {I18NextService.i18n.t("two_factor_token")}
124 <div className="col-sm-6">
128 className="form-control"
129 id="login-totp-token"
131 autoComplete="one-time-code"
132 value={this.state.form.totp_2fa_token}
133 onInput={linkEvent(this, this.handleLoginTotpChange)}
138 <div className="mb-3 row">
139 <div className="col-sm-10">
140 <button type="submit" className="btn btn-secondary">
141 {this.state.loginRes.state == "loading" ? (
144 I18NextService.i18n.t("login")
154 async handleLoginSubmit(i: Login, event: any) {
155 event.preventDefault();
156 const { password, totp_2fa_token, username_or_email } = i.state.form;
158 if (username_or_email && password) {
159 i.setState({ loginRes: { state: "loading" } });
161 const loginRes = await HttpService.client.login({
166 switch (loginRes.state) {
168 if (loginRes.msg === "missing_totp_token") {
169 i.setState({ showTotp: true });
170 toast(I18NextService.i18n.t("enter_two_factor_code"), "info");
173 i.setState({ loginRes: { state: "failed", msg: loginRes.msg } });
178 UserService.Instance.login(loginRes.data);
179 const site = await HttpService.client.getSite({
183 if (site.state === "success") {
184 UserService.Instance.myUserInfo = site.data.my_user;
187 i.props.history.action === "PUSH"
188 ? i.props.history.back()
189 : i.props.history.replace("/");
197 handleLoginUsernameChange(i: Login, event: any) {
198 i.state.form.username_or_email = event.target.value.trim();
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;