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;
143 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
144 <h5>{this.titleName(siteView)}</h5>
147 <div className="mb-3 row">
148 <div className="mt-2 mb-0 alert alert-warning" role="alert">
149 <T i18nKey="lemmy_ml_registration_message">
150 #<a href={joinLemmyUrl}>#</a>
156 <div className="mb-3 row">
158 className="col-sm-2 col-form-label"
159 htmlFor="register-username"
161 {I18NextService.i18n.t("username")}
164 <div className="col-sm-10">
167 id="register-username"
168 className="form-control"
169 value={this.state.form.username}
170 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
173 pattern="[a-zA-Z0-9_]+"
174 title={I18NextService.i18n.t("community_reqs")}
179 <div className="mb-3 row">
180 <label className="col-sm-2 col-form-label" htmlFor="register-email">
181 {I18NextService.i18n.t("email")}
183 <div className="col-sm-10">
187 className="form-control"
189 siteView.local_site.require_email_verification
190 ? I18NextService.i18n.t("required")
191 : I18NextService.i18n.t("optional")
193 value={this.state.form.email}
195 onInput={linkEvent(this, this.handleRegisterEmailChange)}
196 required={siteView.local_site.require_email_verification}
199 {!siteView.local_site.require_email_verification &&
200 this.state.form.email &&
201 !validEmail(this.state.form.email) && (
202 <div className="mt-2 mb-0 alert alert-warning" role="alert">
203 <Icon icon="alert-triangle" classes="icon-inline me-2" />
204 {I18NextService.i18n.t("no_password_reset")}
210 <div className="mb-3 row">
212 className="col-sm-2 col-form-label"
213 htmlFor="register-password"
215 {I18NextService.i18n.t("password")}
217 <div className="col-sm-10">
220 id="register-password"
221 value={this.state.form.password}
222 autoComplete="new-password"
223 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
226 className="form-control"
229 {this.state.form.password && (
230 <div className={this.passwordColorClass}>
231 {I18NextService.i18n.t(
232 this.passwordStrength as NoOptionI18nKeys
239 <div className="mb-3 row">
241 className="col-sm-2 col-form-label"
242 htmlFor="register-verify-password"
244 {I18NextService.i18n.t("verify_password")}
246 <div className="col-sm-10">
249 id="register-verify-password"
250 value={this.state.form.password_verify}
251 autoComplete="new-password"
252 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
254 className="form-control"
260 {siteView.local_site.registration_mode == "RequireApplication" && (
262 <div className="mb-3 row">
263 <div className="offset-sm-2 col-sm-10">
264 <div className="mt-2 alert alert-warning" role="alert">
265 <Icon icon="alert-triangle" classes="icon-inline me-2" />
266 {I18NextService.i18n.t("fill_out_application")}
268 {siteView.local_site.application_question && (
271 dangerouslySetInnerHTML={mdToHtml(
272 siteView.local_site.application_question
279 <div className="mb-3 row">
281 className="col-sm-2 col-form-label"
282 htmlFor="application_answer"
284 {I18NextService.i18n.t("answer")}
286 <div className="col-sm-10">
289 onContentChange={this.handleAnswerChange}
290 hideNavigationWarnings
298 {this.renderCaptcha()}
299 <div className="mb-3 row">
300 <div className="col-sm-10">
301 <div className="form-check">
303 className="form-check-input"
304 id="register-show-nsfw"
306 checked={this.state.form.show_nsfw}
307 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
309 <label className="form-check-label" htmlFor="register-show-nsfw">
310 {I18NextService.i18n.t("show_nsfw")}
320 className="form-control honeypot"
322 value={this.state.form.honeypot}
323 onInput={linkEvent(this, this.handleHoneyPotChange)}
325 <div className="mb-3 row">
326 <div className="col-sm-10">
327 <button type="submit" className="btn btn-secondary">
328 {this.state.registerRes.state == "loading" ? (
331 this.titleName(siteView)
341 switch (this.state.captchaRes.state) {
345 const res = this.state.captchaRes.data;
347 <div className="mb-3 row">
348 <label className="col-sm-2" htmlFor="register-captcha">
349 <span className="me-2">
350 {I18NextService.i18n.t("enter_code")}
354 className="btn btn-secondary"
355 onClick={linkEvent(this, this.handleRegenCaptcha)}
356 aria-label={I18NextService.i18n.t("captcha")}
358 <Icon icon="refresh-cw" classes="icon-refresh-cw" />
361 {this.showCaptcha(res)}
362 <div className="col-sm-6">
365 className="form-control"
366 id="register-captcha"
367 value={this.state.form.captcha_answer}
370 this.handleRegisterCaptchaAnswerChange
381 showCaptcha(res: GetCaptchaResponse) {
382 const captchaRes = res?.ok;
383 return captchaRes ? (
384 <div className="col-sm-4">
387 className="rounded-top img-fluid"
388 src={this.captchaPngSrc(captchaRes)}
389 style="border-bottom-right-radius: 0; border-bottom-left-radius: 0;"
390 alt={I18NextService.i18n.t("captcha")}
394 className="rounded-bottom btn btn-sm btn-secondary d-block"
395 style="border-top-right-radius: 0; border-top-left-radius: 0;"
396 title={I18NextService.i18n.t("play_captcha_audio")}
397 onClick={linkEvent(this, this.handleCaptchaPlay)}
399 disabled={this.state.captchaPlaying}
401 <Icon icon="play" classes="icon-play" />
411 get passwordStrength(): string | undefined {
412 const password = this.state.form.password;
414 ? passwordStrength(password, passwordStrengthOptions).value
418 get passwordColorClass(): string {
419 const strength = this.passwordStrength;
421 if (strength && ["weak", "medium"].includes(strength)) {
422 return "text-warning";
423 } else if (strength == "strong") {
424 return "text-success";
426 return "text-danger";
430 async handleRegisterSubmit(i: Signup, event: any) {
431 event.preventDefault();
443 if (username && password && password_verify) {
444 i.setState({ registerRes: { state: "loading" } });
446 const registerRes = await HttpService.client.register({
457 switch (registerRes.state) {
459 toast(registerRes.msg, "danger");
460 i.setState({ registerRes: { state: "empty" } });
465 const data = registerRes.data;
467 // Only log them in if a jwt was set
469 UserService.Instance.login(data);
471 const site = await HttpService.client.getSite({ auth: myAuth() });
473 if (site.state === "success") {
474 UserService.Instance.myUserInfo = site.data.my_user;
477 i.props.history.replace("/communities");
479 if (data.verify_email_sent) {
480 toast(I18NextService.i18n.t("verify_email_sent"));
482 if (data.registration_created) {
483 toast(I18NextService.i18n.t("registration_application_sent"));
485 i.props.history.push("/");
493 handleRegisterUsernameChange(i: Signup, event: any) {
494 i.state.form.username = event.target.value.trim();
498 handleRegisterEmailChange(i: Signup, event: any) {
499 i.state.form.email = event.target.value;
500 if (i.state.form.email == "") {
501 i.state.form.email = undefined;
506 handleRegisterPasswordChange(i: Signup, event: any) {
507 i.state.form.password = event.target.value;
511 handleRegisterPasswordVerifyChange(i: Signup, event: any) {
512 i.state.form.password_verify = event.target.value;
516 handleRegisterShowNsfwChange(i: Signup, event: any) {
517 i.state.form.show_nsfw = event.target.checked;
521 handleRegisterCaptchaAnswerChange(i: Signup, event: any) {
522 i.state.form.captcha_answer = event.target.value;
526 handleAnswerChange(val: string) {
527 this.setState(s => ((s.form.answer = val), s));
530 handleHoneyPotChange(i: Signup, event: any) {
531 i.state.form.honeypot = event.target.value;
535 async handleRegenCaptcha(i: Signup) {
537 i.setState({ captchaPlaying: false });
538 await i.fetchCaptcha();
541 handleCaptchaPlay(i: Signup) {
542 // This was a bad bug, it should only build the new audio on a new file.
543 // Replays would stop prematurely if this was rebuilt every time.
545 if (i.state.captchaRes.state == "success" && i.state.captchaRes.data.ok) {
546 const captchaRes = i.state.captchaRes.data.ok;
548 const base64 = `data:audio/wav;base64,${captchaRes.wav}`;
549 i.audio = new Audio(base64);
552 i.setState({ captchaPlaying: true });
554 i.audio.addEventListener("ended", () => {
556 i.audio.currentTime = 0;
557 i.setState({ captchaPlaying: false });
564 captchaPngSrc(captcha: CaptchaResponse) {
565 return `data:image/png;base64,${captcha.png}`;