1 import { None, Option, Some } from "@sniptt/monads";
2 import { Options, passwordStrength } from "check-password-strength";
3 import { I18nKeys } from "i18next";
4 import { Component, linkEvent } from "inferno";
5 import { T } from "inferno-i18next-dess";
17 } from "lemmy-js-client";
18 import { Subscription } from "rxjs";
19 import { i18n } from "../../i18next";
20 import { UserService, WebSocketService } from "../../services";
31 import { HtmlTags } from "../common/html-tags";
32 import { Icon, Spinner } from "../common/icon";
33 import { MarkdownTextArea } from "../common/markdown-textarea";
35 const passwordStrengthOptions: Options<string> = [
63 registerForm: Register;
64 registerLoading: boolean;
65 captcha: Option<GetCaptchaResponse>;
66 captchaPlaying: boolean;
67 siteRes: GetSiteResponse;
70 export class Signup extends Component<any, State> {
71 private isoData = setIsoData(this.context);
72 private subscription: Subscription;
73 private audio: HTMLAudioElement;
76 registerForm: new Register({
79 password_verify: undefined,
87 registerLoading: false,
89 captchaPlaying: false,
90 siteRes: this.isoData.site_res,
93 constructor(props: any, context: any) {
94 super(props, context);
96 this.state = this.emptyState;
97 this.handleAnswerChange = this.handleAnswerChange.bind(this);
99 this.parseMessage = this.parseMessage.bind(this);
100 this.subscription = wsSubscribe(this.parseMessage);
103 WebSocketService.Instance.send(wsClient.getCaptcha());
107 componentWillUnmount() {
109 this.subscription.unsubscribe();
113 get documentTitle(): string {
114 return this.state.siteRes.site_view.match({
115 some: siteView => `${this.titleName(siteView)} - ${siteView.site.name}`,
120 titleName(siteView: SiteView): string {
121 return i18n.t(siteView.site.private_instance ? "apply_to_join" : "sign_up");
124 get isLemmyMl(): boolean {
125 return isBrowser() && window.location.hostname == "lemmy.ml";
130 <div className="container">
132 title={this.documentTitle}
133 path={this.context.router.route.match.url}
137 <div className="row">
138 <div className="col-12 col-lg-6 offset-lg-3">
139 {this.registerForm()}
147 return this.state.siteRes.site_view.match({
149 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
150 <h5>{this.titleName(siteView)}</h5>
153 <div className="form-group row">
154 <div className="mt-2 mb-0 alert alert-warning" role="alert">
155 <T i18nKey="lemmy_ml_registration_message">
156 #<a href={joinLemmyUrl}>#</a>
162 <div className="form-group row">
164 className="col-sm-2 col-form-label"
165 htmlFor="register-username"
170 <div className="col-sm-10">
173 id="register-username"
174 className="form-control"
175 value={this.state.registerForm.username}
176 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
179 pattern="[a-zA-Z0-9_]+"
180 title={i18n.t("community_reqs")}
185 <div className="form-group row">
186 <label className="col-sm-2 col-form-label" htmlFor="register-email">
189 <div className="col-sm-10">
193 className="form-control"
195 siteView.site.require_email_verification
199 value={toUndefined(this.state.registerForm.email)}
201 onInput={linkEvent(this, this.handleRegisterEmailChange)}
202 required={siteView.site.require_email_verification}
205 {!siteView.site.require_email_verification &&
206 !this.state.registerForm.email
209 <div className="mt-2 mb-0 alert alert-warning" role="alert">
210 <Icon icon="alert-triangle" classes="icon-inline mr-2" />
211 {i18n.t("no_password_reset")}
217 <div className="form-group row">
219 className="col-sm-2 col-form-label"
220 htmlFor="register-password"
224 <div className="col-sm-10">
227 id="register-password"
228 value={this.state.registerForm.password}
229 autoComplete="new-password"
230 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
233 className="form-control"
236 {this.state.registerForm.password && (
237 <div className={this.passwordColorClass}>
238 {i18n.t(this.passwordStrength as I18nKeys)}
244 <div className="form-group row">
246 className="col-sm-2 col-form-label"
247 htmlFor="register-verify-password"
249 {i18n.t("verify_password")}
251 <div className="col-sm-10">
254 id="register-verify-password"
255 value={this.state.registerForm.password_verify}
256 autoComplete="new-password"
259 this.handleRegisterPasswordVerifyChange
262 className="form-control"
268 {siteView.site.require_application && (
270 <div className="form-group row">
271 <div className="offset-sm-2 col-sm-10">
272 <div className="mt-2 alert alert-warning" role="alert">
273 <Icon icon="alert-triangle" classes="icon-inline mr-2" />
274 {i18n.t("fill_out_application")}
276 {siteView.site.application_question.match({
280 dangerouslySetInnerHTML={mdToHtml(question)}
288 <div className="form-group row">
290 className="col-sm-2 col-form-label"
291 htmlFor="application_answer"
295 <div className="col-sm-10">
297 initialContent={None}
301 onContentChange={this.handleAnswerChange}
302 hideNavigationWarnings
309 {this.state.captcha.isSome() && (
310 <div className="form-group row">
311 <label className="col-sm-2" htmlFor="register-captcha">
312 <span className="mr-2">{i18n.t("enter_code")}</span>
315 className="btn btn-secondary"
316 onClick={linkEvent(this, this.handleRegenCaptcha)}
317 aria-label={i18n.t("captcha")}
319 <Icon icon="refresh-cw" classes="icon-refresh-cw" />
323 <div className="col-sm-6">
326 className="form-control"
327 id="register-captcha"
328 value={toUndefined(this.state.registerForm.captcha_answer)}
331 this.handleRegisterCaptchaAnswerChange
338 {siteView.site.enable_nsfw && (
339 <div className="form-group row">
340 <div className="col-sm-10">
341 <div className="form-check">
343 className="form-check-input"
344 id="register-show-nsfw"
346 checked={this.state.registerForm.show_nsfw}
349 this.handleRegisterShowNsfwChange
353 className="form-check-label"
354 htmlFor="register-show-nsfw"
356 {i18n.t("show_nsfw")}
367 className="form-control honeypot"
369 value={toUndefined(this.state.registerForm.honeypot)}
370 onInput={linkEvent(this, this.handleHoneyPotChange)}
372 <div className="form-group row">
373 <div className="col-sm-10">
374 <button type="submit" className="btn btn-secondary">
375 {this.state.registerLoading ? (
378 this.titleName(siteView)
390 return this.state.captcha.match({
392 <div className="col-sm-4">
397 className="rounded-top img-fluid"
398 src={this.captchaPngSrc(res)}
399 style="border-bottom-right-radius: 0; border-bottom-left-radius: 0;"
400 alt={i18n.t("captcha")}
402 {res.wav.isSome() && (
404 className="rounded-bottom btn btn-sm btn-secondary btn-block"
405 style="border-top-right-radius: 0; border-top-left-radius: 0;"
406 title={i18n.t("play_captcha_audio")}
407 onClick={linkEvent(this, this.handleCaptchaPlay)}
409 disabled={this.state.captchaPlaying}
411 <Icon icon="play" classes="icon-play" />
424 get passwordStrength() {
425 return passwordStrength(
426 this.state.registerForm.password,
427 passwordStrengthOptions
431 get passwordColorClass(): string {
432 let strength = this.passwordStrength;
434 if (["weak", "medium"].includes(strength)) {
435 return "text-warning";
436 } else if (strength == "strong") {
437 return "text-success";
439 return "text-danger";
443 handleRegisterSubmit(i: Signup, event: any) {
444 event.preventDefault();
445 i.setState({ registerLoading: true });
446 WebSocketService.Instance.send(wsClient.register(i.state.registerForm));
449 handleRegisterUsernameChange(i: Signup, event: any) {
450 i.state.registerForm.username = event.target.value;
454 handleRegisterEmailChange(i: Signup, event: any) {
455 i.state.registerForm.email = Some(event.target.value);
456 if (i.state.registerForm.email.unwrap() == "") {
457 i.state.registerForm.email = None;
462 handleRegisterPasswordChange(i: Signup, event: any) {
463 i.state.registerForm.password = event.target.value;
467 handleRegisterPasswordVerifyChange(i: Signup, event: any) {
468 i.state.registerForm.password_verify = event.target.value;
472 handleRegisterShowNsfwChange(i: Signup, event: any) {
473 i.state.registerForm.show_nsfw = event.target.checked;
477 handleRegisterCaptchaAnswerChange(i: Signup, event: any) {
478 i.state.registerForm.captcha_answer = Some(event.target.value);
482 handleAnswerChange(val: string) {
483 this.setState(s => ((s.registerForm.answer = Some(val)), s));
486 handleHoneyPotChange(i: Signup, event: any) {
487 i.state.registerForm.honeypot = Some(event.target.value);
491 handleRegenCaptcha(i: Signup) {
493 i.setState({ captchaPlaying: false });
494 WebSocketService.Instance.send(wsClient.getCaptcha());
497 handleCaptchaPlay(i: Signup) {
498 // This was a bad bug, it should only build the new audio on a new file.
499 // Replays would stop prematurely if this was rebuilt every time.
500 i.state.captcha.match({
504 if (i.audio == null) {
505 let base64 = `data:audio/wav;base64,${res.wav}`;
506 i.audio = new Audio(base64);
511 i.setState({ captchaPlaying: true });
513 i.audio.addEventListener("ended", () => {
514 i.audio.currentTime = 0;
515 i.setState({ captchaPlaying: false });
524 captchaPngSrc(captcha: CaptchaResponse) {
525 return `data:image/png;base64,${captcha.png}`;
528 parseMessage(msg: any) {
529 let op = wsUserOp(msg);
532 toast(i18n.t(msg.error), "danger");
533 this.setState(this.emptyState);
534 this.setState(s => ((s.registerForm.captcha_answer = undefined), s));
535 // Refetch another captcha
536 // WebSocketService.Instance.send(wsClient.getCaptcha());
539 if (op == UserOperation.Register) {
540 let data = wsJsonToRes<LoginResponse>(msg, LoginResponse);
541 this.setState(this.emptyState);
542 // Only log them in if a jwt was set
543 if (data.jwt.isSome()) {
544 UserService.Instance.login(data);
545 this.props.history.push("/communities");
547 if (data.verify_email_sent) {
548 toast(i18n.t("verify_email_sent"));
550 if (data.registration_created) {
551 toast(i18n.t("registration_application_sent"));
553 this.props.history.push("/");
555 } else if (op == UserOperation.GetCaptcha) {
556 let data = wsJsonToRes<GetCaptchaResponse>(msg, GetCaptchaResponse);
559 this.setState({ captcha: Some(data) });
561 s => ((s.registerForm.captcha_uuid = Some(res.uuid)), s)
566 } else if (op == UserOperation.PasswordReset) {
567 toast(i18n.t("reset_password_mail_sent"));
568 } else if (op == UserOperation.GetSite) {
569 let data = wsJsonToRes<GetSiteResponse>(msg, GetSiteResponse);
570 this.setState({ siteRes: data });