1 import { Component, linkEvent } from "inferno";
2 import { T } from "inferno-i18next";
12 } from "lemmy-js-client";
13 import { Subscription } from "rxjs";
14 import { i18n } from "../../i18next";
15 import { UserService, WebSocketService } from "../../services";
28 import { HtmlTags } from "../common/html-tags";
29 import { Icon, Spinner } from "../common/icon";
33 registerForm: Register;
34 loginLoading: boolean;
35 registerLoading: boolean;
36 captcha: GetCaptchaResponse;
37 captchaPlaying: boolean;
41 export class Login extends Component<any, State> {
42 private isoData = setIsoData(this.context);
43 private subscription: Subscription;
44 private audio: HTMLAudioElement;
48 username_or_email: undefined,
54 password_verify: undefined,
56 captcha_uuid: undefined,
57 captcha_answer: undefined,
60 registerLoading: false,
62 captchaPlaying: false,
63 site_view: this.isoData.site_res.site_view,
66 constructor(props: any, context: any) {
67 super(props, context);
69 this.state = this.emptyState;
71 this.parseMessage = this.parseMessage.bind(this);
72 this.subscription = wsSubscribe(this.parseMessage);
75 WebSocketService.Instance.send(wsClient.getCaptcha());
79 componentWillUnmount() {
81 this.subscription.unsubscribe();
85 get documentTitle(): string {
86 return `${i18n.t("login")} - ${this.state.site_view.site.name}`;
89 get isLemmyMl(): boolean {
90 return isBrowser() && window.location.hostname == "lemmy.ml";
95 <div class="container">
97 title={this.documentTitle}
98 path={this.context.router.route.match.url}
101 <div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
102 <div class="col-12 col-lg-6">{this.registerForm()}</div>
111 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
112 <h5>{i18n.t("login")}</h5>
113 <div class="form-group row">
115 class="col-sm-2 col-form-label"
116 htmlFor="login-email-or-username"
118 {i18n.t("email_or_username")}
120 <div class="col-sm-10">
124 id="login-email-or-username"
125 value={this.state.loginForm.username_or_email}
126 onInput={linkEvent(this, this.handleLoginUsernameChange)}
133 <div class="form-group row">
134 <label class="col-sm-2 col-form-label" htmlFor="login-password">
137 <div class="col-sm-10">
141 value={this.state.loginForm.password}
142 onInput={linkEvent(this, this.handleLoginPasswordChange)}
144 autoComplete="current-password"
150 onClick={linkEvent(this, this.handlePasswordReset)}
151 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
152 disabled={!validEmail(this.state.loginForm.username_or_email)}
153 title={i18n.t("no_password_reset")}
155 {i18n.t("forgot_password")}
159 <div class="form-group row">
160 <div class="col-sm-10">
161 <button type="submit" class="btn btn-secondary">
162 {this.state.loginLoading ? <Spinner /> : i18n.t("login")}
173 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
174 <h5>{i18n.t("sign_up")}</h5>
176 <div class="form-group row">
177 <label class="col-sm-2 col-form-label" htmlFor="register-username">
181 <div class="col-sm-10">
184 id="register-username"
186 value={this.state.registerForm.username}
187 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
190 pattern="[a-zA-Z0-9_]+"
195 <div class="form-group row">
196 <label class="col-sm-2 col-form-label" htmlFor="register-email">
199 <div class="col-sm-10">
204 placeholder={i18n.t("optional")}
205 value={this.state.registerForm.email}
207 onInput={linkEvent(this, this.handleRegisterEmailChange)}
210 {!validEmail(this.state.registerForm.email) && (
211 <div class="mt-2 mb-0 alert alert-light" role="alert">
212 <Icon icon="alert-triangle" classes="icon-inline mr-2" />
213 {i18n.t("no_password_reset")}
219 <div class="form-group row">
220 <label class="col-sm-2 col-form-label" htmlFor="register-password">
223 <div class="col-sm-10">
226 id="register-password"
227 value={this.state.registerForm.password}
228 autoComplete="new-password"
229 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
237 <div class="form-group row">
239 class="col-sm-2 col-form-label"
240 htmlFor="register-verify-password"
242 {i18n.t("verify_password")}
244 <div class="col-sm-10">
247 id="register-verify-password"
248 value={this.state.registerForm.password_verify}
249 autoComplete="new-password"
250 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
258 {this.state.captcha && (
259 <div class="form-group row">
260 <label class="col-sm-2" htmlFor="register-captcha">
261 <span class="mr-2">{i18n.t("enter_code")}</span>
264 class="btn btn-secondary"
265 onClick={linkEvent(this, this.handleRegenCaptcha)}
266 aria-label={i18n.t("captcha")}
268 <Icon icon="refresh-cw" classes="icon-refresh-cw" />
272 <div class="col-sm-6">
276 id="register-captcha"
277 value={this.state.registerForm.captcha_answer}
280 this.handleRegisterCaptchaAnswerChange
287 {this.state.site_view.site.enable_nsfw && (
288 <div class="form-group row">
289 <div class="col-sm-10">
290 <div class="form-check">
292 class="form-check-input"
293 id="register-show-nsfw"
295 checked={this.state.registerForm.show_nsfw}
296 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
298 <label class="form-check-label" htmlFor="register-show-nsfw">
299 {i18n.t("show_nsfw")}
306 <div class="mt-2 mb-0 alert alert-light" role="alert">
307 <T i18nKey="lemmy_ml_registration_message">
308 #<a href={joinLemmyUrl}>#</a>
312 <div class="form-group row">
313 <div class="col-sm-10">
314 <button type="submit" class="btn btn-secondary">
315 {this.state.registerLoading ? <Spinner /> : i18n.t("sign_up")}
325 <div class="col-sm-4">
326 {this.state.captcha.ok && (
329 class="rounded-top img-fluid"
330 src={this.captchaPngSrc()}
331 style="border-bottom-right-radius: 0; border-bottom-left-radius: 0;"
332 alt={i18n.t("captcha")}
334 {this.state.captcha.ok.wav && (
336 class="rounded-bottom btn btn-sm btn-secondary btn-block"
337 style="border-top-right-radius: 0; border-top-left-radius: 0;"
338 title={i18n.t("play_captcha_audio")}
339 onClick={linkEvent(this, this.handleCaptchaPlay)}
341 disabled={this.state.captchaPlaying}
343 <Icon icon="play" classes="icon-play" />
352 handleLoginSubmit(i: Login, event: any) {
353 event.preventDefault();
354 i.state.loginLoading = true;
356 WebSocketService.Instance.send(wsClient.login(i.state.loginForm));
359 handleLoginUsernameChange(i: Login, event: any) {
360 i.state.loginForm.username_or_email = event.target.value;
364 handleLoginPasswordChange(i: Login, event: any) {
365 i.state.loginForm.password = event.target.value;
369 handleRegisterSubmit(i: Login, event: any) {
370 event.preventDefault();
371 i.state.registerLoading = true;
373 WebSocketService.Instance.send(wsClient.register(i.state.registerForm));
376 handleRegisterUsernameChange(i: Login, event: any) {
377 i.state.registerForm.username = event.target.value;
381 handleRegisterEmailChange(i: Login, event: any) {
382 i.state.registerForm.email = event.target.value;
383 if (i.state.registerForm.email == "") {
384 i.state.registerForm.email = undefined;
389 handleRegisterPasswordChange(i: Login, event: any) {
390 i.state.registerForm.password = event.target.value;
394 handleRegisterPasswordVerifyChange(i: Login, event: any) {
395 i.state.registerForm.password_verify = event.target.value;
399 handleRegisterShowNsfwChange(i: Login, event: any) {
400 i.state.registerForm.show_nsfw = event.target.checked;
404 handleRegisterCaptchaAnswerChange(i: Login, event: any) {
405 i.state.registerForm.captcha_answer = event.target.value;
409 handleRegenCaptcha(i: Login) {
411 i.state.captchaPlaying = false;
413 WebSocketService.Instance.send(wsClient.getCaptcha());
416 handlePasswordReset(i: Login, event: any) {
417 event.preventDefault();
418 let resetForm: PasswordReset = {
419 email: i.state.loginForm.username_or_email,
421 WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
424 handleCaptchaPlay(i: Login) {
425 // This was a bad bug, it should only build the new audio on a new file.
426 // Replays would stop prematurely if this was rebuilt every time.
427 if (i.audio == null) {
428 let base64 = `data:audio/wav;base64,${i.state.captcha.ok.wav}`;
429 i.audio = new Audio(base64);
434 i.state.captchaPlaying = true;
437 i.audio.addEventListener("ended", () => {
438 i.audio.currentTime = 0;
439 i.state.captchaPlaying = false;
445 return `data:image/png;base64,${this.state.captcha.ok.png}`;
448 parseMessage(msg: any) {
449 let op = wsUserOp(msg);
452 toast(i18n.t(msg.error), "danger");
453 this.state = this.emptyState;
454 this.state.registerForm.captcha_answer = undefined;
455 // Refetch another captcha
456 WebSocketService.Instance.send(wsClient.getCaptcha());
457 this.setState(this.state);
460 if (op == UserOperation.Login) {
461 let data = wsJsonToRes<LoginResponse>(msg).data;
462 this.state = this.emptyState;
463 this.setState(this.state);
464 UserService.Instance.login(data);
465 WebSocketService.Instance.send(
470 toast(i18n.t("logged_in"));
471 this.props.history.push("/");
472 } else if (op == UserOperation.Register) {
473 let data = wsJsonToRes<LoginResponse>(msg).data;
474 this.state = this.emptyState;
475 this.setState(this.state);
476 UserService.Instance.login(data);
477 WebSocketService.Instance.send(
482 this.props.history.push("/communities");
483 } else if (op == UserOperation.GetCaptcha) {
484 let data = wsJsonToRes<GetCaptchaResponse>(msg).data;
486 this.state.captcha = data;
487 this.state.registerForm.captcha_uuid = data.ok.uuid;
488 this.setState(this.state);
490 } else if (op == UserOperation.PasswordReset) {
491 toast(i18n.t("reset_password_mail_sent"));
492 } else if (op == UserOperation.GetSite) {
493 let data = wsJsonToRes<GetSiteResponse>(msg).data;
494 this.state.site_view = data.site_view;
495 this.setState(this.state);