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)}
125 <div class="form-group row">
126 <label class="col-sm-2 col-form-label" htmlFor="login-password">
129 <div class="col-sm-10">
133 value={this.state.loginForm.password}
134 onInput={linkEvent(this, this.handleLoginPasswordChange)}
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"
142 disabled={!validEmail(this.state.loginForm.username_or_email)}
143 title={i18n.t("no_password_reset")}
145 {i18n.t("forgot_password")}
149 <div class="form-group row">
150 <div class="col-sm-10">
151 <button type="submit" class="btn btn-secondary">
152 {this.state.loginLoading ? <Spinner /> : i18n.t("login")}
163 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
164 <h5>{i18n.t("sign_up")}</h5>
166 <div class="form-group row">
167 <label class="col-sm-2 col-form-label" htmlFor="register-username">
171 <div class="col-sm-10">
174 id="register-username"
176 value={this.state.registerForm.username}
177 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
181 pattern="[a-zA-Z0-9_]+"
186 <div class="form-group row">
187 <label class="col-sm-2 col-form-label" htmlFor="register-email">
190 <div class="col-sm-10">
195 placeholder={i18n.t("optional")}
196 value={this.state.registerForm.email}
197 onInput={linkEvent(this, this.handleRegisterEmailChange)}
200 {!validEmail(this.state.registerForm.email) && (
201 <div class="mt-2 mb-0 alert alert-light" role="alert">
202 <Icon icon="alert-triangle" classes="icon-inline mr-2" />
203 {i18n.t("no_password_reset")}
209 <div class="form-group row">
210 <label class="col-sm-2 col-form-label" htmlFor="register-password">
213 <div class="col-sm-10">
216 id="register-password"
217 value={this.state.registerForm.password}
218 autoComplete="new-password"
219 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
227 <div class="form-group row">
229 class="col-sm-2 col-form-label"
230 htmlFor="register-verify-password"
232 {i18n.t("verify_password")}
234 <div class="col-sm-10">
237 id="register-verify-password"
238 value={this.state.registerForm.password_verify}
239 autoComplete="new-password"
240 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
248 {this.state.captcha && (
249 <div class="form-group row">
250 <label class="col-sm-2" htmlFor="register-captcha">
251 <span class="mr-2">{i18n.t("enter_code")}</span>
254 class="btn btn-secondary"
255 onClick={linkEvent(this, this.handleRegenCaptcha)}
256 aria-label={i18n.t("captcha")}
258 <Icon icon="refresh-cw" classes="icon-refresh-cw" />
262 <div class="col-sm-6">
266 id="register-captcha"
267 value={this.state.registerForm.captcha_answer}
270 this.handleRegisterCaptchaAnswerChange
277 {this.state.site_view.site.enable_nsfw && (
278 <div class="form-group row">
279 <div class="col-sm-10">
280 <div class="form-check">
282 class="form-check-input"
283 id="register-show-nsfw"
285 checked={this.state.registerForm.show_nsfw}
286 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
288 <label class="form-check-label" htmlFor="register-show-nsfw">
289 {i18n.t("show_nsfw")}
295 <div class="form-group row">
296 <div class="col-sm-10">
297 <button type="submit" class="btn btn-secondary">
298 {this.state.registerLoading ? <Spinner /> : i18n.t("sign_up")}
308 <div class="col-sm-4">
309 {this.state.captcha.ok && (
312 class="rounded-top img-fluid"
313 src={this.captchaPngSrc()}
314 style="border-bottom-right-radius: 0; border-bottom-left-radius: 0;"
315 alt={i18n.t("captcha")}
317 {this.state.captcha.ok.wav && (
319 class="rounded-bottom btn btn-sm btn-secondary btn-block"
320 style="border-top-right-radius: 0; border-top-left-radius: 0;"
321 title={i18n.t("play_captcha_audio")}
322 onClick={linkEvent(this, this.handleCaptchaPlay)}
324 disabled={this.state.captchaPlaying}
326 <Icon icon="play" classes="icon-play" />
335 handleLoginSubmit(i: Login, event: any) {
336 event.preventDefault();
337 i.state.loginLoading = true;
339 WebSocketService.Instance.send(wsClient.login(i.state.loginForm));
342 handleLoginUsernameChange(i: Login, event: any) {
343 i.state.loginForm.username_or_email = event.target.value;
347 handleLoginPasswordChange(i: Login, event: any) {
348 i.state.loginForm.password = event.target.value;
352 handleRegisterSubmit(i: Login, event: any) {
353 event.preventDefault();
354 i.state.registerLoading = true;
356 WebSocketService.Instance.send(wsClient.register(i.state.registerForm));
359 handleRegisterUsernameChange(i: Login, event: any) {
360 i.state.registerForm.username = event.target.value;
364 handleRegisterEmailChange(i: Login, event: any) {
365 i.state.registerForm.email = event.target.value;
366 if (i.state.registerForm.email == "") {
367 i.state.registerForm.email = undefined;
372 handleRegisterPasswordChange(i: Login, event: any) {
373 i.state.registerForm.password = event.target.value;
377 handleRegisterPasswordVerifyChange(i: Login, event: any) {
378 i.state.registerForm.password_verify = event.target.value;
382 handleRegisterShowNsfwChange(i: Login, event: any) {
383 i.state.registerForm.show_nsfw = event.target.checked;
387 handleRegisterCaptchaAnswerChange(i: Login, event: any) {
388 i.state.registerForm.captcha_answer = event.target.value;
392 handleRegenCaptcha(_i: Login, event: any) {
393 event.preventDefault();
394 WebSocketService.Instance.send(wsClient.getCaptcha());
397 handlePasswordReset(i: Login, event: any) {
398 event.preventDefault();
399 let resetForm: PasswordReset = {
400 email: i.state.loginForm.username_or_email,
402 WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
405 handleCaptchaPlay(i: Login, event: any) {
406 event.preventDefault();
407 let snd = new Audio("data:audio/wav;base64," + i.state.captcha.ok.wav);
409 i.state.captchaPlaying = true;
411 snd.addEventListener("ended", () => {
413 i.state.captchaPlaying = false;
414 i.setState(this.state);
419 return `data:image/png;base64,${this.state.captcha.ok.png}`;
422 parseMessage(msg: any) {
423 let op = wsUserOp(msg);
425 toast(i18n.t(msg.error), "danger");
426 this.state = this.emptyState;
427 this.state.registerForm.captcha_answer = undefined;
428 // Refetch another captcha
429 WebSocketService.Instance.send(wsClient.getCaptcha());
430 this.setState(this.state);
433 if (op == UserOperation.Login) {
434 let data = wsJsonToRes<LoginResponse>(msg).data;
435 this.state = this.emptyState;
436 this.setState(this.state);
437 UserService.Instance.login(data);
438 WebSocketService.Instance.send(
443 toast(i18n.t("logged_in"));
444 this.props.history.push("/");
445 } else if (op == UserOperation.Register) {
446 let data = wsJsonToRes<LoginResponse>(msg).data;
447 this.state = this.emptyState;
448 this.setState(this.state);
449 UserService.Instance.login(data);
450 WebSocketService.Instance.send(
455 this.props.history.push("/communities");
456 } else if (op == UserOperation.GetCaptcha) {
457 let data = wsJsonToRes<GetCaptchaResponse>(msg).data;
459 this.state.captcha = data;
460 this.state.registerForm.captcha_uuid = data.ok.uuid;
461 this.setState(this.state);
463 } else if (op == UserOperation.PasswordReset) {
464 toast(i18n.t("reset_password_mail_sent"));
465 } else if (op == UserOperation.GetSite) {
466 let data = wsJsonToRes<GetSiteResponse>(msg).data;
467 this.state.site_view = data.site_view;
468 this.setState(this.state);