1 import { Options, passwordStrength } from "check-password-strength";
2 import { I18nKeys } from "i18next";
3 import { Component, linkEvent } from "inferno";
4 import { T } from "inferno-i18next-dess";
14 } from "lemmy-js-client";
15 import { Subscription } from "rxjs";
16 import { i18n } from "../../i18next";
17 import { UserService, WebSocketService } from "../../services";
30 import { HtmlTags } from "../common/html-tags";
31 import { Icon, Spinner } from "../common/icon";
33 const passwordStrengthOptions: Options<string> = [
62 registerForm: Register;
63 loginLoading: boolean;
64 registerLoading: boolean;
65 captcha: GetCaptchaResponse;
66 captchaPlaying: boolean;
70 export class Login extends Component<any, State> {
71 private isoData = setIsoData(this.context);
72 private subscription: Subscription;
73 private audio: HTMLAudioElement;
77 username_or_email: undefined,
83 password_verify: undefined,
85 captcha_uuid: undefined,
86 captcha_answer: undefined,
89 registerLoading: false,
91 captchaPlaying: false,
92 site_view: this.isoData.site_res.site_view,
95 constructor(props: any, context: any) {
96 super(props, context);
98 this.state = this.emptyState;
100 this.parseMessage = this.parseMessage.bind(this);
101 this.subscription = wsSubscribe(this.parseMessage);
104 WebSocketService.Instance.send(wsClient.getCaptcha());
108 componentDidMount() {
109 // Navigate to home if already logged in
110 if (UserService.Instance.myUserInfo) {
111 this.context.router.history.push("/");
115 componentWillUnmount() {
117 this.subscription.unsubscribe();
121 get documentTitle(): string {
122 return `${i18n.t("login")} - ${this.state.site_view.site.name}`;
125 get isLemmyMl(): boolean {
126 return isBrowser() && window.location.hostname == "lemmy.ml";
131 <div class="container">
133 title={this.documentTitle}
134 path={this.context.router.route.match.url}
137 <div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
138 <div class="col-12 col-lg-6">{this.registerForm()}</div>
147 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
148 <h5>{i18n.t("login")}</h5>
149 <div class="form-group row">
151 class="col-sm-2 col-form-label"
152 htmlFor="login-email-or-username"
154 {i18n.t("email_or_username")}
156 <div class="col-sm-10">
160 id="login-email-or-username"
161 value={this.state.loginForm.username_or_email}
162 onInput={linkEvent(this, this.handleLoginUsernameChange)}
169 <div class="form-group row">
170 <label class="col-sm-2 col-form-label" htmlFor="login-password">
173 <div class="col-sm-10">
177 value={this.state.loginForm.password}
178 onInput={linkEvent(this, this.handleLoginPasswordChange)}
180 autoComplete="current-password"
186 onClick={linkEvent(this, this.handlePasswordReset)}
187 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold pointer-events not-allowed"
188 disabled={!validEmail(this.state.loginForm.username_or_email)}
189 title={i18n.t("no_password_reset")}
191 {i18n.t("forgot_password")}
195 <div class="form-group row">
196 <div class="col-sm-10">
197 <button type="submit" class="btn btn-secondary">
198 {this.state.loginLoading ? <Spinner /> : i18n.t("login")}
209 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
210 <h5>{i18n.t("sign_up")}</h5>
212 <div class="form-group row">
213 <label class="col-sm-2 col-form-label" htmlFor="register-username">
217 <div class="col-sm-10">
220 id="register-username"
222 value={this.state.registerForm.username}
223 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
226 pattern="[a-zA-Z0-9_]+"
227 title={i18n.t("community_reqs")}
232 <div class="form-group row">
233 <label class="col-sm-2 col-form-label" htmlFor="register-email">
236 <div class="col-sm-10">
241 placeholder={i18n.t("optional")}
242 value={this.state.registerForm.email}
244 onInput={linkEvent(this, this.handleRegisterEmailChange)}
247 {!validEmail(this.state.registerForm.email) && (
248 <div class="mt-2 mb-0 alert alert-light" role="alert">
249 <Icon icon="alert-triangle" classes="icon-inline mr-2" />
250 {i18n.t("no_password_reset")}
256 <div class="form-group row">
257 <label class="col-sm-2 col-form-label" htmlFor="register-password">
260 <div class="col-sm-10">
263 id="register-password"
264 value={this.state.registerForm.password}
265 autoComplete="new-password"
266 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
272 {this.state.registerForm.password && (
273 <div class={this.passwordColorClass}>
274 {i18n.t(this.passwordStrength as I18nKeys)}
280 <div class="form-group row">
282 class="col-sm-2 col-form-label"
283 htmlFor="register-verify-password"
285 {i18n.t("verify_password")}
287 <div class="col-sm-10">
290 id="register-verify-password"
291 value={this.state.registerForm.password_verify}
292 autoComplete="new-password"
293 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
301 {this.state.captcha && (
302 <div class="form-group row">
303 <label class="col-sm-2" htmlFor="register-captcha">
304 <span class="mr-2">{i18n.t("enter_code")}</span>
307 class="btn btn-secondary"
308 onClick={linkEvent(this, this.handleRegenCaptcha)}
309 aria-label={i18n.t("captcha")}
311 <Icon icon="refresh-cw" classes="icon-refresh-cw" />
315 <div class="col-sm-6">
319 id="register-captcha"
320 value={this.state.registerForm.captcha_answer}
323 this.handleRegisterCaptchaAnswerChange
330 {this.state.site_view.site.enable_nsfw && (
331 <div class="form-group row">
332 <div class="col-sm-10">
333 <div class="form-check">
335 class="form-check-input"
336 id="register-show-nsfw"
338 checked={this.state.registerForm.show_nsfw}
339 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
341 <label class="form-check-label" htmlFor="register-show-nsfw">
342 {i18n.t("show_nsfw")}
349 <div class="mt-2 mb-0 alert alert-light" role="alert">
350 <T i18nKey="lemmy_ml_registration_message">
351 #<a href={joinLemmyUrl}>#</a>
355 <div class="form-group row">
356 <div class="col-sm-10">
357 <button type="submit" class="btn btn-secondary">
358 {this.state.registerLoading ? <Spinner /> : i18n.t("sign_up")}
368 <div class="col-sm-4">
369 {this.state.captcha.ok && (
372 class="rounded-top img-fluid"
373 src={this.captchaPngSrc()}
374 style="border-bottom-right-radius: 0; border-bottom-left-radius: 0;"
375 alt={i18n.t("captcha")}
377 {this.state.captcha.ok.wav && (
379 class="rounded-bottom btn btn-sm btn-secondary btn-block"
380 style="border-top-right-radius: 0; border-top-left-radius: 0;"
381 title={i18n.t("play_captcha_audio")}
382 onClick={linkEvent(this, this.handleCaptchaPlay)}
384 disabled={this.state.captchaPlaying}
386 <Icon icon="play" classes="icon-play" />
395 get passwordStrength() {
396 return passwordStrength(
397 this.state.registerForm.password,
398 passwordStrengthOptions
402 get passwordColorClass(): string {
403 let strength = this.passwordStrength;
405 if (["weak", "medium"].includes(strength)) {
406 return "text-warning";
407 } else if (strength == "strong") {
408 return "text-success";
410 return "text-danger";
414 handleLoginSubmit(i: Login, event: any) {
415 event.preventDefault();
416 i.state.loginLoading = true;
418 WebSocketService.Instance.send(wsClient.login(i.state.loginForm));
421 handleLoginUsernameChange(i: Login, event: any) {
422 i.state.loginForm.username_or_email = event.target.value;
426 handleLoginPasswordChange(i: Login, event: any) {
427 i.state.loginForm.password = event.target.value;
431 handleRegisterSubmit(i: Login, event: any) {
432 event.preventDefault();
433 i.state.registerLoading = true;
435 WebSocketService.Instance.send(wsClient.register(i.state.registerForm));
438 handleRegisterUsernameChange(i: Login, event: any) {
439 i.state.registerForm.username = event.target.value;
443 handleRegisterEmailChange(i: Login, event: any) {
444 i.state.registerForm.email = event.target.value;
445 if (i.state.registerForm.email == "") {
446 i.state.registerForm.email = undefined;
451 handleRegisterPasswordChange(i: Login, event: any) {
452 i.state.registerForm.password = event.target.value;
456 handleRegisterPasswordVerifyChange(i: Login, event: any) {
457 i.state.registerForm.password_verify = event.target.value;
461 handleRegisterShowNsfwChange(i: Login, event: any) {
462 i.state.registerForm.show_nsfw = event.target.checked;
466 handleRegisterCaptchaAnswerChange(i: Login, event: any) {
467 i.state.registerForm.captcha_answer = event.target.value;
471 handleRegenCaptcha(i: Login) {
473 i.state.captchaPlaying = false;
475 WebSocketService.Instance.send(wsClient.getCaptcha());
478 handlePasswordReset(i: Login, event: any) {
479 event.preventDefault();
480 let resetForm: PasswordReset = {
481 email: i.state.loginForm.username_or_email,
483 WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
486 handleCaptchaPlay(i: Login) {
487 // This was a bad bug, it should only build the new audio on a new file.
488 // Replays would stop prematurely if this was rebuilt every time.
489 if (i.audio == null) {
490 let base64 = `data:audio/wav;base64,${i.state.captcha.ok.wav}`;
491 i.audio = new Audio(base64);
496 i.state.captchaPlaying = true;
499 i.audio.addEventListener("ended", () => {
500 i.audio.currentTime = 0;
501 i.state.captchaPlaying = false;
507 return `data:image/png;base64,${this.state.captcha.ok.png}`;
510 parseMessage(msg: any) {
511 let op = wsUserOp(msg);
514 toast(i18n.t(msg.error), "danger");
515 this.state = this.emptyState;
516 this.state.registerForm.captcha_answer = undefined;
517 // Refetch another captcha
518 WebSocketService.Instance.send(wsClient.getCaptcha());
519 this.setState(this.state);
522 if (op == UserOperation.Login) {
523 let data = wsJsonToRes<LoginResponse>(msg).data;
524 this.state = this.emptyState;
525 this.setState(this.state);
526 UserService.Instance.login(data);
527 WebSocketService.Instance.send(
532 toast(i18n.t("logged_in"));
533 this.props.history.push("/");
534 } else if (op == UserOperation.Register) {
535 let data = wsJsonToRes<LoginResponse>(msg).data;
536 this.state = this.emptyState;
537 this.setState(this.state);
538 UserService.Instance.login(data);
539 WebSocketService.Instance.send(
544 this.props.history.push("/communities");
545 } else if (op == UserOperation.GetCaptcha) {
546 let data = wsJsonToRes<GetCaptchaResponse>(msg).data;
548 this.state.captcha = data;
549 this.state.registerForm.captcha_uuid = data.ok.uuid;
550 this.setState(this.state);
552 } else if (op == UserOperation.PasswordReset) {
553 toast(i18n.t("reset_password_mail_sent"));
554 } else if (op == UserOperation.GetSite) {
555 let data = wsJsonToRes<GetSiteResponse>(msg).data;
556 this.state.site_view = data.site_view;
557 this.setState(this.state);