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;
30 loginLoading: boolean;
31 siteRes: GetSiteResponse;
34 export class Login extends Component<any, State> {
35 private isoData = setIsoData(this.context);
36 private subscription?: Subscription;
41 siteRes: this.isoData.site_res,
44 constructor(props: any, context: any) {
45 super(props, context);
47 this.parseMessage = this.parseMessage.bind(this);
48 this.subscription = wsSubscribe(this.parseMessage);
51 WebSocketService.Instance.send(wsClient.getCaptcha());
56 // Navigate to home if already logged in
57 if (UserService.Instance.myUserInfo) {
58 this.context.router.history.push("/");
62 componentWillUnmount() {
64 this.subscription?.unsubscribe();
68 get documentTitle(): string {
69 return `${i18n.t("login")} - ${this.state.siteRes.site_view.site.name}`;
72 get isLemmyMl(): boolean {
73 return isBrowser() && window.location.hostname == "lemmy.ml";
78 <div className="container-lg">
80 title={this.documentTitle}
81 path={this.context.router.route.match.url}
84 <div className="col-12 col-lg-6 offset-lg-3">{this.loginForm()}</div>
93 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
94 <h5>{i18n.t("login")}</h5>
95 <div className="form-group row">
97 className="col-sm-2 col-form-label"
98 htmlFor="login-email-or-username"
100 {i18n.t("email_or_username")}
102 <div className="col-sm-10">
105 className="form-control"
106 id="login-email-or-username"
107 value={this.state.form.username_or_email}
108 onInput={linkEvent(this, this.handleLoginUsernameChange)}
115 <div className="form-group row">
116 <label className="col-sm-2 col-form-label" htmlFor="login-password">
119 <div className="col-sm-10">
123 value={this.state.form.password}
124 onInput={linkEvent(this, this.handleLoginPasswordChange)}
125 className="form-control"
126 autoComplete="current-password"
132 onClick={linkEvent(this, this.handlePasswordReset)}
133 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
135 !!this.state.form.username_or_email &&
136 !validEmail(this.state.form.username_or_email)
138 title={i18n.t("no_password_reset")}
140 {i18n.t("forgot_password")}
144 <div className="form-group row">
145 <div className="col-sm-10">
146 <button type="submit" className="btn btn-secondary">
147 {this.state.loginLoading ? <Spinner /> : i18n.t("login")}
156 handleLoginSubmit(i: Login, event: any) {
157 event.preventDefault();
158 i.setState({ loginLoading: true });
159 let lForm = i.state.form;
160 let username_or_email = lForm.username_or_email;
161 let password = lForm.password;
162 if (username_or_email && password) {
167 WebSocketService.Instance.send(wsClient.login(form));
171 handleLoginUsernameChange(i: Login, event: any) {
172 i.state.form.username_or_email = event.target.value;
176 handleLoginPasswordChange(i: Login, event: any) {
177 i.state.form.password = event.target.value;
181 handlePasswordReset(i: Login, event: any) {
182 event.preventDefault();
183 let email = i.state.form.username_or_email;
185 let resetForm: PasswordReset = { email };
186 WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
190 parseMessage(msg: any) {
191 let op = wsUserOp(msg);
194 toast(i18n.t(msg.error), "danger");
195 this.setState({ form: {} });
198 if (op == UserOperation.Login) {
199 let data = wsJsonToRes<LoginResponse>(msg);
200 UserService.Instance.login(data);
201 this.props.history.push("/");
203 } else if (op == UserOperation.PasswordReset) {
204 toast(i18n.t("reset_password_mail_sent"));
205 } else if (op == UserOperation.GetSite) {
206 let data = wsJsonToRes<GetSiteResponse>(msg);
207 this.setState({ siteRes: data });