1 import { None } from "@sniptt/monads";
2 import { Component, linkEvent } from "inferno";
11 } from "lemmy-js-client";
12 import { Subscription } from "rxjs";
13 import { i18n } from "../../i18next";
14 import { UserService, WebSocketService } from "../../services";
23 import { HtmlTags } from "../common/html-tags";
24 import { Spinner } from "../common/icon";
28 loginLoading: boolean;
29 siteRes: GetSiteResponse;
32 export class Login extends Component<any, State> {
33 private isoData = setIsoData(this.context);
34 private subscription: Subscription;
37 loginForm: new LoginForm({
38 username_or_email: undefined,
42 siteRes: this.isoData.site_res,
45 constructor(props: any, context: any) {
46 super(props, context);
48 this.state = this.emptyState;
50 this.parseMessage = this.parseMessage.bind(this);
51 this.subscription = wsSubscribe(this.parseMessage);
54 WebSocketService.Instance.send(wsClient.getCaptcha());
59 // Navigate to home if already logged in
60 if (UserService.Instance.myUserInfo.isSome()) {
61 this.context.router.history.push("/");
65 componentWillUnmount() {
67 this.subscription.unsubscribe();
71 get documentTitle(): string {
72 return this.state.siteRes.site_view.match({
73 some: siteView => `${i18n.t("login")} - ${siteView.site.name}`,
78 get isLemmyMl(): boolean {
79 return isBrowser() && window.location.hostname == "lemmy.ml";
84 <div className="container-lg">
86 title={this.documentTitle}
87 path={this.context.router.route.match.url}
92 <div className="col-12 col-lg-6 offset-lg-3">{this.loginForm()}</div>
101 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
102 <h5>{i18n.t("login")}</h5>
103 <div className="form-group row">
105 className="col-sm-2 col-form-label"
106 htmlFor="login-email-or-username"
108 {i18n.t("email_or_username")}
110 <div className="col-sm-10">
113 className="form-control"
114 id="login-email-or-username"
115 value={this.state.loginForm.username_or_email}
116 onInput={linkEvent(this, this.handleLoginUsernameChange)}
123 <div className="form-group row">
124 <label className="col-sm-2 col-form-label" htmlFor="login-password">
127 <div className="col-sm-10">
131 value={this.state.loginForm.password}
132 onInput={linkEvent(this, this.handleLoginPasswordChange)}
133 className="form-control"
134 autoComplete="current-password"
140 onClick={linkEvent(this, this.handlePasswordReset)}
141 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
142 disabled={!validEmail(this.state.loginForm.username_or_email)}
143 title={i18n.t("no_password_reset")}
145 {i18n.t("forgot_password")}
149 <div className="form-group row">
150 <div className="col-sm-10">
151 <button type="submit" className="btn btn-secondary">
152 {this.state.loginLoading ? <Spinner /> : i18n.t("login")}
161 handleLoginSubmit(i: Login, event: any) {
162 event.preventDefault();
163 i.setState({ loginLoading: true });
164 WebSocketService.Instance.send(wsClient.login(i.state.loginForm));
167 handleLoginUsernameChange(i: Login, event: any) {
168 i.state.loginForm.username_or_email = event.target.value;
172 handleLoginPasswordChange(i: Login, event: any) {
173 i.state.loginForm.password = event.target.value;
177 handlePasswordReset(i: Login, event: any) {
178 event.preventDefault();
179 let resetForm = new PasswordReset({
180 email: i.state.loginForm.username_or_email,
182 WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
185 parseMessage(msg: any) {
186 let op = wsUserOp(msg);
189 toast(i18n.t(msg.error), "danger");
190 this.setState(this.emptyState);
193 if (op == UserOperation.Login) {
194 let data = wsJsonToRes<LoginResponse>(msg, LoginResponse);
195 this.setState(this.emptyState);
196 UserService.Instance.login(data);
197 } else if (op == UserOperation.PasswordReset) {
198 toast(i18n.t("reset_password_mail_sent"));
199 } else if (op == UserOperation.GetSite) {
200 let data = wsJsonToRes<GetSiteResponse>(msg, GetSiteResponse);
201 this.setState({ siteRes: data });