1 import { Component, linkEvent } from "inferno";
2 import { Subscription } from "rxjs";
12 } from "lemmy-js-client";
13 import { WebSocketService, UserService } from "../services";
25 import { i18n } from "../i18next";
26 import { HtmlTags } from "./html-tags";
27 import { Icon, Spinner } from "./icon";
31 registerForm: Register;
32 loginLoading: boolean;
33 registerLoading: boolean;
34 captcha: GetCaptchaResponse;
35 captchaPlaying: boolean;
39 export class Login extends Component<any, State> {
40 private isoData = setIsoData(this.context);
41 private subscription: Subscription;
45 username_or_email: undefined,
51 password_verify: undefined,
53 captcha_uuid: undefined,
54 captcha_answer: undefined,
57 registerLoading: false,
59 captchaPlaying: false,
60 site_view: this.isoData.site_res.site_view,
63 constructor(props: any, context: any) {
64 super(props, context);
66 this.state = this.emptyState;
68 this.parseMessage = this.parseMessage.bind(this);
69 this.subscription = wsSubscribe(this.parseMessage);
72 WebSocketService.Instance.send(wsClient.getCaptcha());
76 componentWillUnmount() {
78 this.subscription.unsubscribe();
82 get documentTitle(): string {
83 return `${i18n.t("login")} - ${this.state.site_view.site.name}`;
88 <div class="container">
90 title={this.documentTitle}
91 path={this.context.router.route.match.url}
94 <div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
95 <div class="col-12 col-lg-6">{this.registerForm()}</div>
104 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
105 <h5>{i18n.t("login")}</h5>
106 <div class="form-group row">
108 class="col-sm-2 col-form-label"
109 htmlFor="login-email-or-username"
111 {i18n.t("email_or_username")}
113 <div class="col-sm-10">
117 id="login-email-or-username"
118 value={this.state.loginForm.username_or_email}
119 onInput={linkEvent(this, this.handleLoginUsernameChange)}
126 <div class="form-group row">
127 <label class="col-sm-2 col-form-label" htmlFor="login-password">
130 <div class="col-sm-10">
134 value={this.state.loginForm.password}
135 onInput={linkEvent(this, this.handleLoginPasswordChange)}
137 autoComplete="current-password"
143 onClick={linkEvent(this, this.handlePasswordReset)}
144 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
145 disabled={!validEmail(this.state.loginForm.username_or_email)}
146 title={i18n.t("no_password_reset")}
148 {i18n.t("forgot_password")}
152 <div class="form-group row">
153 <div class="col-sm-10">
154 <button type="submit" class="btn btn-secondary">
155 {this.state.loginLoading ? <Spinner /> : i18n.t("login")}
166 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
167 <h5>{i18n.t("sign_up")}</h5>
169 <div class="form-group row">
170 <label class="col-sm-2 col-form-label" htmlFor="register-username">
174 <div class="col-sm-10">
177 id="register-username"
179 value={this.state.registerForm.username}
180 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
184 pattern="[a-zA-Z0-9_]+"
189 <div class="form-group row">
190 <label class="col-sm-2 col-form-label" htmlFor="register-email">
193 <div class="col-sm-10">
198 placeholder={i18n.t("optional")}
199 value={this.state.registerForm.email}
201 onInput={linkEvent(this, this.handleRegisterEmailChange)}
204 {!validEmail(this.state.registerForm.email) && (
205 <div class="mt-2 mb-0 alert alert-light" role="alert">
206 <Icon icon="alert-triangle" classes="icon-inline mr-2" />
207 {i18n.t("no_password_reset")}
213 <div class="form-group row">
214 <label class="col-sm-2 col-form-label" htmlFor="register-password">
217 <div class="col-sm-10">
220 id="register-password"
221 value={this.state.registerForm.password}
222 autoComplete="new-password"
223 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
231 <div class="form-group row">
233 class="col-sm-2 col-form-label"
234 htmlFor="register-verify-password"
236 {i18n.t("verify_password")}
238 <div class="col-sm-10">
241 id="register-verify-password"
242 value={this.state.registerForm.password_verify}
243 autoComplete="new-password"
244 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
252 {this.state.captcha && (
253 <div class="form-group row">
254 <label class="col-sm-2" htmlFor="register-captcha">
255 <span class="mr-2">{i18n.t("enter_code")}</span>
258 class="btn btn-secondary"
259 onClick={linkEvent(this, this.handleRegenCaptcha)}
260 aria-label={i18n.t("captcha")}
262 <Icon icon="refresh-cw" classes="icon-refresh-cw" />
266 <div class="col-sm-6">
270 id="register-captcha"
271 value={this.state.registerForm.captcha_answer}
274 this.handleRegisterCaptchaAnswerChange
281 {this.state.site_view.site.enable_nsfw && (
282 <div class="form-group row">
283 <div class="col-sm-10">
284 <div class="form-check">
286 class="form-check-input"
287 id="register-show-nsfw"
289 checked={this.state.registerForm.show_nsfw}
290 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
292 <label class="form-check-label" htmlFor="register-show-nsfw">
293 {i18n.t("show_nsfw")}
299 <div class="form-group row">
300 <div class="col-sm-10">
301 <button type="submit" class="btn btn-secondary">
302 {this.state.registerLoading ? <Spinner /> : i18n.t("sign_up")}
312 <div class="col-sm-4">
313 {this.state.captcha.ok && (
316 class="rounded-top img-fluid"
317 src={this.captchaPngSrc()}
318 style="border-bottom-right-radius: 0; border-bottom-left-radius: 0;"
319 alt={i18n.t("captcha")}
321 {this.state.captcha.ok.wav && (
323 class="rounded-bottom btn btn-sm btn-secondary btn-block"
324 style="border-top-right-radius: 0; border-top-left-radius: 0;"
325 title={i18n.t("play_captcha_audio")}
326 onClick={linkEvent(this, this.handleCaptchaPlay)}
328 disabled={this.state.captchaPlaying}
330 <Icon icon="play" classes="icon-play" />
339 handleLoginSubmit(i: Login, event: any) {
340 event.preventDefault();
341 i.state.loginLoading = true;
343 WebSocketService.Instance.send(wsClient.login(i.state.loginForm));
346 handleLoginUsernameChange(i: Login, event: any) {
347 i.state.loginForm.username_or_email = event.target.value;
351 handleLoginPasswordChange(i: Login, event: any) {
352 i.state.loginForm.password = event.target.value;
356 handleRegisterSubmit(i: Login, event: any) {
357 event.preventDefault();
358 i.state.registerLoading = true;
360 WebSocketService.Instance.send(wsClient.register(i.state.registerForm));
363 handleRegisterUsernameChange(i: Login, event: any) {
364 i.state.registerForm.username = event.target.value;
368 handleRegisterEmailChange(i: Login, event: any) {
369 i.state.registerForm.email = event.target.value;
370 if (i.state.registerForm.email == "") {
371 i.state.registerForm.email = undefined;
376 handleRegisterPasswordChange(i: Login, event: any) {
377 i.state.registerForm.password = event.target.value;
381 handleRegisterPasswordVerifyChange(i: Login, event: any) {
382 i.state.registerForm.password_verify = event.target.value;
386 handleRegisterShowNsfwChange(i: Login, event: any) {
387 i.state.registerForm.show_nsfw = event.target.checked;
391 handleRegisterCaptchaAnswerChange(i: Login, event: any) {
392 i.state.registerForm.captcha_answer = event.target.value;
396 handleRegenCaptcha(_i: Login, event: any) {
397 event.preventDefault();
398 WebSocketService.Instance.send(wsClient.getCaptcha());
401 handlePasswordReset(i: Login, event: any) {
402 event.preventDefault();
403 let resetForm: PasswordReset = {
404 email: i.state.loginForm.username_or_email,
406 WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
409 handleCaptchaPlay(i: Login, event: any) {
410 event.preventDefault();
411 let snd = new Audio("data:audio/wav;base64," + i.state.captcha.ok.wav);
413 i.state.captchaPlaying = true;
415 snd.addEventListener("ended", () => {
417 i.state.captchaPlaying = false;
418 i.setState(this.state);
423 return `data:image/png;base64,${this.state.captcha.ok.png}`;
426 parseMessage(msg: any) {
427 let op = wsUserOp(msg);
430 toast(i18n.t(msg.error), "danger");
431 this.state = this.emptyState;
432 this.state.registerForm.captcha_answer = undefined;
433 // Refetch another captcha
434 WebSocketService.Instance.send(wsClient.getCaptcha());
435 this.setState(this.state);
438 if (op == UserOperation.Login) {
439 let data = wsJsonToRes<LoginResponse>(msg).data;
440 this.state = this.emptyState;
441 this.setState(this.state);
442 UserService.Instance.login(data);
443 WebSocketService.Instance.send(
448 toast(i18n.t("logged_in"));
449 this.props.history.push("/");
450 } else if (op == UserOperation.Register) {
451 let data = wsJsonToRes<LoginResponse>(msg).data;
452 this.state = this.emptyState;
453 this.setState(this.state);
454 UserService.Instance.login(data);
455 WebSocketService.Instance.send(
460 this.props.history.push("/communities");
461 } else if (op == UserOperation.GetCaptcha) {
462 let data = wsJsonToRes<GetCaptchaResponse>(msg).data;
464 this.state.captcha = data;
465 this.state.registerForm.captcha_uuid = data.ok.uuid;
466 this.setState(this.state);
468 } else if (op == UserOperation.PasswordReset) {
469 toast(i18n.t("reset_password_mail_sent"));
470 } else if (op == UserOperation.GetSite) {
471 let data = wsJsonToRes<GetSiteResponse>(msg).data;
472 this.state.site_view = data.site_view;
473 this.setState(this.state);