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;
47 username_or_email: undefined,
53 password_verify: undefined,
55 captcha_uuid: undefined,
56 captcha_answer: undefined,
59 registerLoading: false,
61 captchaPlaying: false,
62 site_view: this.isoData.site_res.site_view,
65 constructor(props: any, context: any) {
66 super(props, context);
68 this.state = this.emptyState;
70 this.parseMessage = this.parseMessage.bind(this);
71 this.subscription = wsSubscribe(this.parseMessage);
74 WebSocketService.Instance.send(wsClient.getCaptcha());
78 componentWillUnmount() {
80 this.subscription.unsubscribe();
84 get documentTitle(): string {
85 return `${i18n.t("login")} - ${this.state.site_view.site.name}`;
88 get isLemmyMl(): boolean {
89 return isBrowser() && window.location.hostname == "lemmy.ml";
94 <div class="container">
96 title={this.documentTitle}
97 path={this.context.router.route.match.url}
100 <div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
101 <div class="col-12 col-lg-6">{this.registerForm()}</div>
110 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
111 <h5>{i18n.t("login")}</h5>
112 <div class="form-group row">
114 class="col-sm-2 col-form-label"
115 htmlFor="login-email-or-username"
117 {i18n.t("email_or_username")}
119 <div class="col-sm-10">
123 id="login-email-or-username"
124 value={this.state.loginForm.username_or_email}
125 onInput={linkEvent(this, this.handleLoginUsernameChange)}
132 <div class="form-group row">
133 <label class="col-sm-2 col-form-label" htmlFor="login-password">
136 <div class="col-sm-10">
140 value={this.state.loginForm.password}
141 onInput={linkEvent(this, this.handleLoginPasswordChange)}
143 autoComplete="current-password"
149 onClick={linkEvent(this, this.handlePasswordReset)}
150 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
151 disabled={!validEmail(this.state.loginForm.username_or_email)}
152 title={i18n.t("no_password_reset")}
154 {i18n.t("forgot_password")}
158 <div class="form-group row">
159 <div class="col-sm-10">
160 <button type="submit" class="btn btn-secondary">
161 {this.state.loginLoading ? <Spinner /> : i18n.t("login")}
172 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
173 <h5>{i18n.t("sign_up")}</h5>
175 <div class="form-group row">
176 <label class="col-sm-2 col-form-label" htmlFor="register-username">
180 <div class="col-sm-10">
183 id="register-username"
185 value={this.state.registerForm.username}
186 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, event: any) {
410 event.preventDefault();
411 WebSocketService.Instance.send(wsClient.getCaptcha());
414 handlePasswordReset(i: Login, event: any) {
415 event.preventDefault();
416 let resetForm: PasswordReset = {
417 email: i.state.loginForm.username_or_email,
419 WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
422 handleCaptchaPlay(i: Login, event: any) {
423 event.preventDefault();
424 let snd = new Audio("data:audio/wav;base64," + i.state.captcha.ok.wav);
426 i.state.captchaPlaying = true;
428 snd.addEventListener("ended", () => {
430 i.state.captchaPlaying = false;
431 i.setState(this.state);
436 return `data:image/png;base64,${this.state.captcha.ok.png}`;
439 parseMessage(msg: any) {
440 let op = wsUserOp(msg);
443 toast(i18n.t(msg.error), "danger");
444 this.state = this.emptyState;
445 this.state.registerForm.captcha_answer = undefined;
446 // Refetch another captcha
447 WebSocketService.Instance.send(wsClient.getCaptcha());
448 this.setState(this.state);
451 if (op == UserOperation.Login) {
452 let data = wsJsonToRes<LoginResponse>(msg).data;
453 this.state = this.emptyState;
454 this.setState(this.state);
455 UserService.Instance.login(data);
456 WebSocketService.Instance.send(
461 toast(i18n.t("logged_in"));
462 this.props.history.push("/");
463 } else if (op == UserOperation.Register) {
464 let data = wsJsonToRes<LoginResponse>(msg).data;
465 this.state = this.emptyState;
466 this.setState(this.state);
467 UserService.Instance.login(data);
468 WebSocketService.Instance.send(
473 this.props.history.push("/communities");
474 } else if (op == UserOperation.GetCaptcha) {
475 let data = wsJsonToRes<GetCaptchaResponse>(msg).data;
477 this.state.captcha = data;
478 this.state.registerForm.captcha_uuid = data.ok.uuid;
479 this.setState(this.state);
481 } else if (op == UserOperation.PasswordReset) {
482 toast(i18n.t("reset_password_mail_sent"));
483 } else if (op == UserOperation.GetSite) {
484 let data = wsJsonToRes<GetSiteResponse>(msg).data;
485 this.state.site_view = data.site_view;
486 this.setState(this.state);