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";
24 import { HtmlTags } from "../common/html-tags";
25 import { Spinner } from "../common/icon";
29 loginLoading: boolean;
30 siteRes: GetSiteResponse;
33 export class Login extends Component<any, State> {
34 private isoData = setIsoData(this.context);
35 private subscription: Subscription;
38 loginForm: new LoginForm({
39 username_or_email: undefined,
43 siteRes: this.isoData.site_res,
46 constructor(props: any, context: any) {
47 super(props, context);
49 this.state = this.emptyState;
51 this.parseMessage = this.parseMessage.bind(this);
52 this.subscription = wsSubscribe(this.parseMessage);
55 WebSocketService.Instance.send(wsClient.getCaptcha());
60 // Navigate to home if already logged in
61 if (UserService.Instance.myUserInfo.isSome()) {
62 this.context.router.history.push("/");
66 componentWillUnmount() {
68 this.subscription.unsubscribe();
72 get documentTitle(): string {
73 return this.state.siteRes.site_view.match({
74 some: siteView => `${i18n.t("login")} - ${siteView.site.name}`,
79 get isLemmyMl(): boolean {
80 return isBrowser() && window.location.hostname == "lemmy.ml";
85 <div class="container">
87 title={this.documentTitle}
88 path={this.context.router.route.match.url}
93 <div class="col-12 col-lg-6 offset-lg-3">{this.loginForm()}</div>
102 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
103 <h5>{i18n.t("login")}</h5>
104 <div class="form-group row">
106 class="col-sm-2 col-form-label"
107 htmlFor="login-email-or-username"
109 {i18n.t("email_or_username")}
111 <div class="col-sm-10">
115 id="login-email-or-username"
116 value={this.state.loginForm.username_or_email}
117 onInput={linkEvent(this, this.handleLoginUsernameChange)}
124 <div class="form-group row">
125 <label class="col-sm-2 col-form-label" htmlFor="login-password">
128 <div class="col-sm-10">
132 value={this.state.loginForm.password}
133 onInput={linkEvent(this, this.handleLoginPasswordChange)}
135 autoComplete="current-password"
141 onClick={linkEvent(this, this.handlePasswordReset)}
142 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
143 disabled={!validEmail(this.state.loginForm.username_or_email)}
144 title={i18n.t("no_password_reset")}
146 {i18n.t("forgot_password")}
150 <div class="form-group row">
151 <div class="col-sm-10">
152 <button type="submit" class="btn btn-secondary">
153 {this.state.loginLoading ? <Spinner /> : i18n.t("login")}
162 handleLoginSubmit(i: Login, event: any) {
163 event.preventDefault();
164 i.state.loginLoading = true;
166 WebSocketService.Instance.send(wsClient.login(i.state.loginForm));
169 handleLoginUsernameChange(i: Login, event: any) {
170 i.state.loginForm.username_or_email = event.target.value;
174 handleLoginPasswordChange(i: Login, event: any) {
175 i.state.loginForm.password = event.target.value;
179 handlePasswordReset(i: Login, event: any) {
180 event.preventDefault();
181 let resetForm = new PasswordReset({
182 email: i.state.loginForm.username_or_email,
184 WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
187 parseMessage(msg: any) {
188 let op = wsUserOp(msg);
191 toast(i18n.t(msg.error), "danger");
192 this.state = this.emptyState;
193 this.setState(this.state);
196 if (op == UserOperation.Login) {
197 let data = wsJsonToRes<LoginResponse>(msg, LoginResponse);
198 this.state = this.emptyState;
199 this.setState(this.state);
200 UserService.Instance.login(data);
201 WebSocketService.Instance.send(
203 auth: auth().unwrap(),
206 toast(i18n.t("logged_in"));
207 this.props.history.push("/");
208 } else if (op == UserOperation.PasswordReset) {
209 toast(i18n.t("reset_password_mail_sent"));
210 } else if (op == UserOperation.GetSite) {
211 let data = wsJsonToRes<GetSiteResponse>(msg, GetSiteResponse);
212 this.state.siteRes = data;
213 this.setState(this.state);