1 import { Component, linkEvent } from "inferno";
9 } from "lemmy-js-client";
10 import { Subscription } from "rxjs";
11 import { i18n } from "../../i18next";
12 import { UserService, WebSocketService } from "../../services";
24 import { HtmlTags } from "../common/html-tags";
25 import { Spinner } from "../common/icon";
29 loginLoading: boolean;
33 export class Login extends Component<any, State> {
34 private isoData = setIsoData(this.context);
35 private subscription: Subscription;
39 username_or_email: undefined,
43 site_view: this.isoData.site_res.site_view,
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) {
62 this.context.router.history.push("/");
66 componentWillUnmount() {
68 this.subscription.unsubscribe();
72 get documentTitle(): string {
73 return `${i18n.t("login")} - ${this.state.site_view.site.name}`;
76 get isLemmyMl(): boolean {
77 return isBrowser() && window.location.hostname == "lemmy.ml";
82 <div class="container">
84 title={this.documentTitle}
85 path={this.context.router.route.match.url}
88 <div class="col-12 col-lg-6 offset-lg-3">{this.loginForm()}</div>
97 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
98 <h5>{i18n.t("login")}</h5>
99 <div class="form-group row">
101 class="col-sm-2 col-form-label"
102 htmlFor="login-email-or-username"
104 {i18n.t("email_or_username")}
106 <div class="col-sm-10">
110 id="login-email-or-username"
111 value={this.state.loginForm.username_or_email}
112 onInput={linkEvent(this, this.handleLoginUsernameChange)}
119 <div class="form-group row">
120 <label class="col-sm-2 col-form-label" htmlFor="login-password">
123 <div class="col-sm-10">
127 value={this.state.loginForm.password}
128 onInput={linkEvent(this, this.handleLoginPasswordChange)}
130 autoComplete="current-password"
136 onClick={linkEvent(this, this.handlePasswordReset)}
137 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
138 disabled={!validEmail(this.state.loginForm.username_or_email)}
139 title={i18n.t("no_password_reset")}
141 {i18n.t("forgot_password")}
145 <div class="form-group row">
146 <div class="col-sm-10">
147 <button type="submit" class="btn btn-secondary">
148 {this.state.loginLoading ? <Spinner /> : i18n.t("login")}
157 handleLoginSubmit(i: Login, event: any) {
158 event.preventDefault();
159 i.state.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: 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.state = this.emptyState;
188 // Refetch another captcha
189 WebSocketService.Instance.send(wsClient.getCaptcha());
190 this.setState(this.state);
193 if (op == UserOperation.Login) {
194 let data = wsJsonToRes<LoginResponse>(msg).data;
195 this.state = this.emptyState;
196 this.setState(this.state);
197 UserService.Instance.login(data);
198 WebSocketService.Instance.send(
203 toast(i18n.t("logged_in"));
204 this.props.history.push("/");
205 } else if (op == UserOperation.PasswordReset) {
206 toast(i18n.t("reset_password_mail_sent"));
207 } else if (op == UserOperation.GetSite) {
208 let data = wsJsonToRes<GetSiteResponse>(msg).data;
209 this.state.site_view = data.site_view;
210 this.setState(this.state);