1 import { myAuth, setIsoData } from "@utils/app";
2 import { isBrowser } from "@utils/browser";
3 import { validEmail } from "@utils/helpers";
4 import { Options, passwordStrength } from "check-password-strength";
5 import { NoOptionI18nKeys } from "i18next";
6 import { Component, linkEvent } from "inferno";
7 import { T } from "inferno-i18next-dess";
14 } from "lemmy-js-client";
15 import { joinLemmyUrl } from "../../config";
16 import { mdToHtml } from "../../markdown";
17 import { I18NextService, UserService } from "../../services";
18 import { HttpService, RequestState } from "../../services/HttpService";
19 import { toast } from "../../toast";
20 import { HtmlTags } from "../common/html-tags";
21 import { Icon, Spinner } from "../common/icon";
22 import { MarkdownTextArea } from "../common/markdown-textarea";
24 const passwordStrengthOptions: Options<string> = [
52 registerRes: RequestState<LoginResponse>;
53 captchaRes: RequestState<GetCaptchaResponse>;
58 password_verify?: string;
60 captcha_uuid?: string;
61 captcha_answer?: string;
65 captchaPlaying: boolean;
66 siteRes: GetSiteResponse;
69 export class Signup extends Component<any, State> {
70 private isoData = setIsoData(this.context);
71 private audio?: HTMLAudioElement;
74 registerRes: { state: "empty" },
75 captchaRes: { state: "empty" },
79 captchaPlaying: false,
80 siteRes: this.isoData.site_res,
83 constructor(props: any, context: any) {
84 super(props, context);
86 this.handleAnswerChange = this.handleAnswerChange.bind(this);
89 async componentDidMount() {
90 if (this.state.siteRes.site_view.local_site.captcha_enabled) {
91 await this.fetchCaptcha();
95 async fetchCaptcha() {
96 this.setState({ captchaRes: { state: "loading" } });
98 captchaRes: await HttpService.client.getCaptcha({}),
102 if (s.captchaRes.state == "success") {
103 s.form.captcha_uuid = s.captchaRes.data.ok?.uuid;
109 get documentTitle(): string {
110 const siteView = this.state.siteRes.site_view;
111 return `${this.titleName(siteView)} - ${siteView.site.name}`;
114 titleName(siteView: SiteView): string {
115 return I18NextService.i18n.t(
116 siteView.local_site.private_instance ? "apply_to_join" : "sign_up"
120 get isLemmyMl(): boolean {
121 return isBrowser() && window.location.hostname == "lemmy.ml";
126 <div className="home-signup container-lg">
128 title={this.documentTitle}
129 path={this.context.router.route.match.url}
131 <div className="row">
132 <div className="col-12 col-lg-6 offset-lg-3">
133 {this.registerForm()}
141 const siteView = this.state.siteRes.site_view;
144 className="was-validated"
145 onSubmit={linkEvent(this, this.handleRegisterSubmit)}
147 <h1 className="h4 mb-4">{this.titleName(siteView)}</h1>
150 <div className="mb-3 row">
151 <div className="mt-2 mb-0 alert alert-warning" role="alert">
152 <T i18nKey="lemmy_ml_registration_message">
153 #<a href={joinLemmyUrl}>#</a>
159 <div className="mb-3 row">
161 className="col-sm-2 col-form-label"
162 htmlFor="register-username"
164 {I18NextService.i18n.t("username")}
167 <div className="col-sm-10">
170 id="register-username"
171 className="form-control"
172 value={this.state.form.username}
173 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
176 pattern="[a-zA-Z0-9_]+"
177 title={I18NextService.i18n.t("community_reqs")}
182 <div className="mb-3 row">
183 <label className="col-sm-2 col-form-label" htmlFor="register-email">
184 {I18NextService.i18n.t("email")}
186 <div className="col-sm-10">
190 className="form-control"
192 siteView.local_site.require_email_verification
193 ? I18NextService.i18n.t("required")
194 : I18NextService.i18n.t("optional")
196 value={this.state.form.email}
198 onInput={linkEvent(this, this.handleRegisterEmailChange)}
199 required={siteView.local_site.require_email_verification}
202 {!siteView.local_site.require_email_verification &&
203 this.state.form.email &&
204 !validEmail(this.state.form.email) && (
205 <div className="mt-2 mb-0 alert alert-warning" role="alert">
206 <Icon icon="alert-triangle" classes="icon-inline me-2" />
207 {I18NextService.i18n.t("no_password_reset")}
213 <div className="mb-3 row">
215 className="col-sm-2 col-form-label"
216 htmlFor="register-password"
218 {I18NextService.i18n.t("password")}
220 <div className="col-sm-10">
223 id="register-password"
224 value={this.state.form.password}
225 autoComplete="new-password"
226 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
229 className="form-control"
232 {this.state.form.password && (
233 <div className={this.passwordColorClass}>
234 {I18NextService.i18n.t(
235 this.passwordStrength as NoOptionI18nKeys
242 <div className="mb-3 row">
244 className="col-sm-2 col-form-label"
245 htmlFor="register-verify-password"
247 {I18NextService.i18n.t("verify_password")}
249 <div className="col-sm-10">
252 id="register-verify-password"
253 value={this.state.form.password_verify}
254 autoComplete="new-password"
255 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
257 className="form-control"
263 {siteView.local_site.registration_mode == "RequireApplication" && (
265 <div className="mb-3 row">
266 <div className="offset-sm-2 col-sm-10">
267 <div className="mt-2 alert alert-warning" role="alert">
268 <Icon icon="alert-triangle" classes="icon-inline me-2" />
269 {I18NextService.i18n.t("fill_out_application")}
271 {siteView.local_site.application_question && (
274 dangerouslySetInnerHTML={mdToHtml(
275 siteView.local_site.application_question
282 <div className="mb-3 row">
284 className="col-sm-2 col-form-label"
285 htmlFor="application_answer"
287 {I18NextService.i18n.t("answer")}
289 <div className="col-sm-10">
292 onContentChange={this.handleAnswerChange}
293 hideNavigationWarnings
301 {this.renderCaptcha()}
302 <div className="mb-3 row">
303 <div className="col-sm-10">
304 <div className="form-check">
306 className="form-check-input"
307 id="register-show-nsfw"
309 checked={this.state.form.show_nsfw}
310 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
312 <label className="form-check-label" htmlFor="register-show-nsfw">
313 {I18NextService.i18n.t("show_nsfw")}
323 className="form-control honeypot"
325 value={this.state.form.honeypot}
326 onInput={linkEvent(this, this.handleHoneyPotChange)}
328 <div className="mb-3 row">
329 <div className="col-sm-10">
330 <button type="submit" className="btn btn-secondary">
331 {this.state.registerRes.state == "loading" ? (
334 this.titleName(siteView)
344 switch (this.state.captchaRes.state) {
348 const res = this.state.captchaRes.data;
350 <div className="mb-3 row">
351 <label className="col-sm-2" htmlFor="register-captcha">
352 <span className="me-2">
353 {I18NextService.i18n.t("enter_code")}
357 className="btn btn-secondary"
358 onClick={linkEvent(this, this.handleRegenCaptcha)}
359 aria-label={I18NextService.i18n.t("captcha")}
361 <Icon icon="refresh-cw" classes="icon-refresh-cw" />
364 {this.showCaptcha(res)}
365 <div className="col-sm-6">
368 className="form-control"
369 id="register-captcha"
370 value={this.state.form.captcha_answer}
373 this.handleRegisterCaptchaAnswerChange
384 showCaptcha(res: GetCaptchaResponse) {
385 const captchaRes = res?.ok;
386 return captchaRes ? (
387 <div className="col-sm-4">
390 className="rounded-top img-fluid"
391 src={this.captchaPngSrc(captchaRes)}
392 style="border-bottom-right-radius: 0; border-bottom-left-radius: 0;"
393 alt={I18NextService.i18n.t("captcha")}
397 className="rounded-bottom btn btn-sm btn-secondary d-block"
398 style="border-top-right-radius: 0; border-top-left-radius: 0;"
399 title={I18NextService.i18n.t("play_captcha_audio")}
400 onClick={linkEvent(this, this.handleCaptchaPlay)}
402 disabled={this.state.captchaPlaying}
404 <Icon icon="play" classes="icon-play" />
414 get passwordStrength(): string | undefined {
415 const password = this.state.form.password;
417 ? passwordStrength(password, passwordStrengthOptions).value
421 get passwordColorClass(): string {
422 const strength = this.passwordStrength;
424 if (strength && ["weak", "medium"].includes(strength)) {
425 return "text-warning";
426 } else if (strength == "strong") {
427 return "text-success";
429 return "text-danger";
433 async handleRegisterSubmit(i: Signup, event: any) {
434 event.preventDefault();
446 if (username && password && password_verify) {
447 i.setState({ registerRes: { state: "loading" } });
449 const registerRes = await HttpService.client.register({
460 switch (registerRes.state) {
462 toast(registerRes.msg, "danger");
463 i.setState({ registerRes: { state: "empty" } });
468 const data = registerRes.data;
470 // Only log them in if a jwt was set
472 UserService.Instance.login({
476 const site = await HttpService.client.getSite({ auth: myAuth() });
478 if (site.state === "success") {
479 UserService.Instance.myUserInfo = site.data.my_user;
482 i.props.history.replace("/communities");
484 if (data.verify_email_sent) {
485 toast(I18NextService.i18n.t("verify_email_sent"));
487 if (data.registration_created) {
488 toast(I18NextService.i18n.t("registration_application_sent"));
490 i.props.history.push("/");
498 handleRegisterUsernameChange(i: Signup, event: any) {
499 i.state.form.username = event.target.value.trim();
503 handleRegisterEmailChange(i: Signup, event: any) {
504 i.state.form.email = event.target.value;
505 if (i.state.form.email == "") {
506 i.state.form.email = undefined;
511 handleRegisterPasswordChange(i: Signup, event: any) {
512 i.state.form.password = event.target.value;
516 handleRegisterPasswordVerifyChange(i: Signup, event: any) {
517 i.state.form.password_verify = event.target.value;
521 handleRegisterShowNsfwChange(i: Signup, event: any) {
522 i.state.form.show_nsfw = event.target.checked;
526 handleRegisterCaptchaAnswerChange(i: Signup, event: any) {
527 i.state.form.captcha_answer = event.target.value;
531 handleAnswerChange(val: string) {
532 this.setState(s => ((s.form.answer = val), s));
535 handleHoneyPotChange(i: Signup, event: any) {
536 i.state.form.honeypot = event.target.value;
540 async handleRegenCaptcha(i: Signup) {
542 i.setState({ captchaPlaying: false });
543 await i.fetchCaptcha();
546 handleCaptchaPlay(i: Signup) {
547 // This was a bad bug, it should only build the new audio on a new file.
548 // Replays would stop prematurely if this was rebuilt every time.
550 if (i.state.captchaRes.state == "success" && i.state.captchaRes.data.ok) {
551 const captchaRes = i.state.captchaRes.data.ok;
553 const base64 = `data:audio/wav;base64,${captchaRes.wav}`;
554 i.audio = new Audio(base64);
557 i.setState({ captchaPlaying: true });
559 i.audio.addEventListener("ended", () => {
561 i.audio.currentTime = 0;
562 i.setState({ captchaPlaying: false });
569 captchaPngSrc(captcha: CaptchaResponse) {
570 return `data:image/png;base64,${captcha.png}`;