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";
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";
31 const passwordStrengthOptions: Options<string> = [
59 registerForm: Register;
60 registerLoading: boolean;
61 captcha: GetCaptchaResponse;
62 captchaPlaying: boolean;
66 export class Signup extends Component<any, State> {
67 private isoData = setIsoData(this.context);
68 private subscription: Subscription;
69 private audio: HTMLAudioElement;
75 password_verify: undefined,
77 captcha_uuid: undefined,
78 captcha_answer: undefined,
81 registerLoading: false,
83 captchaPlaying: false,
84 site_view: this.isoData.site_res.site_view,
87 constructor(props: any, context: any) {
88 super(props, context);
90 this.state = this.emptyState;
92 this.parseMessage = this.parseMessage.bind(this);
93 this.subscription = wsSubscribe(this.parseMessage);
96 WebSocketService.Instance.send(wsClient.getCaptcha());
100 componentWillUnmount() {
102 this.subscription.unsubscribe();
106 get documentTitle(): string {
107 return `${i18n.t("login")} - ${this.state.site_view.site.name}`;
110 get isLemmyMl(): boolean {
111 return isBrowser() && window.location.hostname == "lemmy.ml";
116 <div class="container">
118 title={this.documentTitle}
119 path={this.context.router.route.match.url}
122 <div class="col-12 col-lg-6 offset-lg-3">{this.registerForm()}</div>
130 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
131 <h5>{i18n.t("sign_up")}</h5>
133 <div class="form-group row">
134 <label class="col-sm-2 col-form-label" htmlFor="register-username">
138 <div class="col-sm-10">
141 id="register-username"
143 value={this.state.registerForm.username}
144 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
147 pattern="[a-zA-Z0-9_]+"
148 title={i18n.t("community_reqs")}
153 <div class="form-group row">
154 <label class="col-sm-2 col-form-label" htmlFor="register-email">
157 <div class="col-sm-10">
162 placeholder={i18n.t("optional")}
163 value={this.state.registerForm.email}
165 onInput={linkEvent(this, this.handleRegisterEmailChange)}
168 {!validEmail(this.state.registerForm.email) && (
169 <div class="mt-2 mb-0 alert alert-light" role="alert">
170 <Icon icon="alert-triangle" classes="icon-inline mr-2" />
171 {i18n.t("no_password_reset")}
177 <div class="form-group row">
178 <label class="col-sm-2 col-form-label" htmlFor="register-password">
181 <div class="col-sm-10">
184 id="register-password"
185 value={this.state.registerForm.password}
186 autoComplete="new-password"
187 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
193 {this.state.registerForm.password && (
194 <div class={this.passwordColorClass}>
195 {i18n.t(this.passwordStrength as I18nKeys)}
201 <div class="form-group row">
203 class="col-sm-2 col-form-label"
204 htmlFor="register-verify-password"
206 {i18n.t("verify_password")}
208 <div class="col-sm-10">
211 id="register-verify-password"
212 value={this.state.registerForm.password_verify}
213 autoComplete="new-password"
214 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
222 {this.state.captcha && (
223 <div class="form-group row">
224 <label class="col-sm-2" htmlFor="register-captcha">
225 <span class="mr-2">{i18n.t("enter_code")}</span>
228 class="btn btn-secondary"
229 onClick={linkEvent(this, this.handleRegenCaptcha)}
230 aria-label={i18n.t("captcha")}
232 <Icon icon="refresh-cw" classes="icon-refresh-cw" />
236 <div class="col-sm-6">
240 id="register-captcha"
241 value={this.state.registerForm.captcha_answer}
244 this.handleRegisterCaptchaAnswerChange
251 {this.state.site_view.site.enable_nsfw && (
252 <div class="form-group row">
253 <div class="col-sm-10">
254 <div class="form-check">
256 class="form-check-input"
257 id="register-show-nsfw"
259 checked={this.state.registerForm.show_nsfw}
260 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
262 <label class="form-check-label" htmlFor="register-show-nsfw">
263 {i18n.t("show_nsfw")}
270 <div class="mt-2 mb-0 alert alert-light" role="alert">
271 <T i18nKey="lemmy_ml_registration_message">
272 #<a href={joinLemmyUrl}>#</a>
281 class="form-control honeypot"
283 value={this.state.registerForm.honeypot}
284 onInput={linkEvent(this, this.handleHoneyPotChange)}
286 <div class="form-group row">
287 <div class="col-sm-10">
288 <button type="submit" class="btn btn-secondary">
289 {this.state.registerLoading ? <Spinner /> : i18n.t("sign_up")}
299 <div class="col-sm-4">
300 {this.state.captcha.ok && (
303 class="rounded-top img-fluid"
304 src={this.captchaPngSrc()}
305 style="border-bottom-right-radius: 0; border-bottom-left-radius: 0;"
306 alt={i18n.t("captcha")}
308 {this.state.captcha.ok.wav && (
310 class="rounded-bottom btn btn-sm btn-secondary btn-block"
311 style="border-top-right-radius: 0; border-top-left-radius: 0;"
312 title={i18n.t("play_captcha_audio")}
313 onClick={linkEvent(this, this.handleCaptchaPlay)}
315 disabled={this.state.captchaPlaying}
317 <Icon icon="play" classes="icon-play" />
326 get passwordStrength() {
327 return passwordStrength(
328 this.state.registerForm.password,
329 passwordStrengthOptions
333 get passwordColorClass(): string {
334 let strength = this.passwordStrength;
336 if (["weak", "medium"].includes(strength)) {
337 return "text-warning";
338 } else if (strength == "strong") {
339 return "text-success";
341 return "text-danger";
345 handleRegisterSubmit(i: Signup, event: any) {
346 event.preventDefault();
347 i.state.registerLoading = true;
349 WebSocketService.Instance.send(wsClient.register(i.state.registerForm));
352 handleRegisterUsernameChange(i: Signup, event: any) {
353 i.state.registerForm.username = event.target.value;
357 handleRegisterEmailChange(i: Signup, event: any) {
358 i.state.registerForm.email = event.target.value;
359 if (i.state.registerForm.email == "") {
360 i.state.registerForm.email = undefined;
365 handleRegisterPasswordChange(i: Signup, event: any) {
366 i.state.registerForm.password = event.target.value;
370 handleRegisterPasswordVerifyChange(i: Signup, event: any) {
371 i.state.registerForm.password_verify = event.target.value;
375 handleRegisterShowNsfwChange(i: Signup, event: any) {
376 i.state.registerForm.show_nsfw = event.target.checked;
380 handleRegisterCaptchaAnswerChange(i: Signup, event: any) {
381 i.state.registerForm.captcha_answer = event.target.value;
385 handleHoneyPotChange(i: Signup, event: any) {
386 i.state.registerForm.honeypot = event.target.value;
390 handleRegenCaptcha(i: Signup) {
392 i.state.captchaPlaying = false;
394 WebSocketService.Instance.send(wsClient.getCaptcha());
397 handleCaptchaPlay(i: Signup) {
398 // This was a bad bug, it should only build the new audio on a new file.
399 // Replays would stop prematurely if this was rebuilt every time.
400 if (i.audio == null) {
401 let base64 = `data:audio/wav;base64,${i.state.captcha.ok.wav}`;
402 i.audio = new Audio(base64);
407 i.state.captchaPlaying = true;
410 i.audio.addEventListener("ended", () => {
411 i.audio.currentTime = 0;
412 i.state.captchaPlaying = false;
418 return `data:image/png;base64,${this.state.captcha.ok.png}`;
421 parseMessage(msg: any) {
422 let op = wsUserOp(msg);
425 toast(i18n.t(msg.error), "danger");
426 this.state = this.emptyState;
427 this.state.registerForm.captcha_answer = undefined;
428 // Refetch another captcha
429 // WebSocketService.Instance.send(wsClient.getCaptcha());
430 this.setState(this.state);
433 if (op == UserOperation.Register) {
434 let data = wsJsonToRes<LoginResponse>(msg).data;
435 this.state = this.emptyState;
436 this.setState(this.state);
437 UserService.Instance.login(data);
438 WebSocketService.Instance.send(
443 this.props.history.push("/communities");
444 } else if (op == UserOperation.GetCaptcha) {
445 let data = wsJsonToRes<GetCaptchaResponse>(msg).data;
447 this.state.captcha = data;
448 this.state.registerForm.captcha_uuid = data.ok.uuid;
449 this.setState(this.state);
451 } else if (op == UserOperation.PasswordReset) {
452 toast(i18n.t("reset_password_mail_sent"));
453 } else if (op == UserOperation.GetSite) {
454 let data = wsJsonToRes<GetSiteResponse>(msg).data;
455 this.state.site_view = data.site_view;
456 this.setState(this.state);