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 `${i18n.t("login")} - ${this.state.siteRes.site_view.site.name}`;
75 get isLemmyMl(): boolean {
76 return isBrowser() && window.location.hostname == "lemmy.ml";
81 <div className="container-lg">
83 title={this.documentTitle}
84 path={this.context.router.route.match.url}
89 <div className="col-12 col-lg-6 offset-lg-3">{this.loginForm()}</div>
98 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
99 <h5>{i18n.t("login")}</h5>
100 <div className="form-group row">
102 className="col-sm-2 col-form-label"
103 htmlFor="login-email-or-username"
105 {i18n.t("email_or_username")}
107 <div className="col-sm-10">
110 className="form-control"
111 id="login-email-or-username"
112 value={this.state.loginForm.username_or_email}
113 onInput={linkEvent(this, this.handleLoginUsernameChange)}
120 <div className="form-group row">
121 <label className="col-sm-2 col-form-label" htmlFor="login-password">
124 <div className="col-sm-10">
128 value={this.state.loginForm.password}
129 onInput={linkEvent(this, this.handleLoginPasswordChange)}
130 className="form-control"
131 autoComplete="current-password"
137 onClick={linkEvent(this, this.handlePasswordReset)}
138 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
139 disabled={!validEmail(this.state.loginForm.username_or_email)}
140 title={i18n.t("no_password_reset")}
142 {i18n.t("forgot_password")}
146 <div className="form-group row">
147 <div className="col-sm-10">
148 <button type="submit" className="btn btn-secondary">
149 {this.state.loginLoading ? <Spinner /> : i18n.t("login")}
158 handleLoginSubmit(i: Login, event: any) {
159 event.preventDefault();
160 i.setState({ loginLoading: true });
161 WebSocketService.Instance.send(wsClient.login(i.state.loginForm));
164 handleLoginUsernameChange(i: Login, event: any) {
165 i.state.loginForm.username_or_email = event.target.value;
169 handleLoginPasswordChange(i: Login, event: any) {
170 i.state.loginForm.password = event.target.value;
174 handlePasswordReset(i: Login, event: any) {
175 event.preventDefault();
176 let resetForm = new PasswordReset({
177 email: i.state.loginForm.username_or_email,
179 WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
182 parseMessage(msg: any) {
183 let op = wsUserOp(msg);
186 toast(i18n.t(msg.error), "danger");
187 this.setState(this.emptyState);
190 if (op == UserOperation.Login) {
191 let data = wsJsonToRes<LoginResponse>(msg, LoginResponse);
192 this.setState(this.emptyState);
193 UserService.Instance.login(data);
194 this.props.history.push("/");
196 } else if (op == UserOperation.PasswordReset) {
197 toast(i18n.t("reset_password_mail_sent"));
198 } else if (op == UserOperation.GetSite) {
199 let data = wsJsonToRes<GetSiteResponse>(msg, GetSiteResponse);
200 this.setState({ siteRes: data });