1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
12 } from 'lemmy-js-client';
13 import { WebSocketService, UserService } from '../services';
25 import { i18n } from '../i18next';
26 import { HtmlTags } from './html-tags';
30 registerForm: Register;
31 loginLoading: boolean;
32 registerLoading: boolean;
33 captcha: GetCaptchaResponse;
34 captchaPlaying: boolean;
38 export class Login extends Component<any, State> {
39 private isoData = setIsoData(this.context);
40 private subscription: Subscription;
44 username_or_email: undefined,
50 password_verify: undefined,
53 captcha_uuid: undefined,
54 captcha_answer: undefined,
57 registerLoading: false,
59 captchaPlaying: false,
60 site_view: this.isoData.site_res.site_view,
63 constructor(props: any, context: any) {
64 super(props, context);
66 this.state = this.emptyState;
68 this.parseMessage = this.parseMessage.bind(this);
69 this.subscription = wsSubscribe(this.parseMessage);
72 WebSocketService.Instance.send(wsClient.getCaptcha());
76 componentWillUnmount() {
78 this.subscription.unsubscribe();
82 get documentTitle(): string {
83 return `${i18n.t('login')} - ${this.state.site_view.site.name}`;
88 <div class="container">
90 title={this.documentTitle}
91 path={this.context.router.route.match.url}
94 <div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
95 <div class="col-12 col-lg-6">{this.registerForm()}</div>
104 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
105 <h5>{i18n.t('login')}</h5>
106 <div class="form-group row">
108 class="col-sm-2 col-form-label"
109 htmlFor="login-email-or-username"
111 {i18n.t('email_or_username')}
113 <div class="col-sm-10">
117 id="login-email-or-username"
118 value={this.state.loginForm.username_or_email}
119 onInput={linkEvent(this, this.handleLoginUsernameChange)}
125 <div class="form-group row">
126 <label class="col-sm-2 col-form-label" htmlFor="login-password">
129 <div class="col-sm-10">
133 value={this.state.loginForm.password}
134 onInput={linkEvent(this, this.handleLoginPasswordChange)}
138 {validEmail(this.state.loginForm.username_or_email) && (
141 onClick={linkEvent(this, this.handlePasswordReset)}
142 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold"
144 {i18n.t('forgot_password')}
149 <div class="form-group row">
150 <div class="col-sm-10">
151 <button type="submit" class="btn btn-secondary">
152 {this.state.loginLoading ? (
153 <svg class="icon icon-spinner spin">
154 <use xlinkHref="#icon-spinner"></use>
169 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
170 <h5>{i18n.t('sign_up')}</h5>
172 <div class="form-group row">
173 <label class="col-sm-2 col-form-label" htmlFor="register-username">
177 <div class="col-sm-10">
180 id="register-username"
182 value={this.state.registerForm.username}
183 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
187 pattern="[a-zA-Z0-9_]+"
192 <div class="form-group row">
193 <label class="col-sm-2 col-form-label" htmlFor="register-email">
196 <div class="col-sm-10">
201 placeholder={i18n.t('optional')}
202 value={this.state.registerForm.email}
203 onInput={linkEvent(this, this.handleRegisterEmailChange)}
206 {!validEmail(this.state.registerForm.email) && (
207 <div class="mt-2 mb-0 alert alert-light" role="alert">
208 <svg class="icon icon-inline mr-2">
209 <use xlinkHref="#icon-alert-triangle"></use>
211 {i18n.t('no_password_reset')}
217 <div class="form-group row">
218 <label class="col-sm-2 col-form-label" htmlFor="register-password">
221 <div class="col-sm-10">
224 id="register-password"
225 value={this.state.registerForm.password}
226 autoComplete="new-password"
227 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
235 <div class="form-group row">
237 class="col-sm-2 col-form-label"
238 htmlFor="register-verify-password"
240 {i18n.t('verify_password')}
242 <div class="col-sm-10">
245 id="register-verify-password"
246 value={this.state.registerForm.password_verify}
247 autoComplete="new-password"
248 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
255 {this.state.captcha && (
256 <div class="form-group row">
257 <label class="col-sm-2" htmlFor="register-captcha">
258 <span class="mr-2">{i18n.t('enter_code')}</span>
261 class="btn btn-secondary"
262 onClick={linkEvent(this, this.handleRegenCaptcha)}
264 <svg class="icon icon-refresh-cw">
265 <use xlinkHref="#icon-refresh-cw"></use>
270 <div class="col-sm-6">
274 id="register-captcha"
275 value={this.state.registerForm.captcha_answer}
278 this.handleRegisterCaptchaAnswerChange
285 {this.state.site_view.site.enable_nsfw && (
286 <div class="form-group row">
287 <div class="col-sm-10">
288 <div class="form-check">
290 class="form-check-input"
291 id="register-show-nsfw"
293 checked={this.state.registerForm.show_nsfw}
294 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
296 <label class="form-check-label" htmlFor="register-show-nsfw">
297 {i18n.t('show_nsfw')}
303 <div class="form-group row">
304 <div class="col-sm-10">
305 <button type="submit" class="btn btn-secondary">
306 {this.state.registerLoading ? (
307 <svg class="icon icon-spinner spin">
308 <use xlinkHref="#icon-spinner"></use>
322 <div class="col-sm-4">
323 {this.state.captcha.ok && (
326 class="rounded-top img-fluid"
327 src={this.captchaPngSrc()}
328 style="border-bottom-right-radius: 0; border-bottom-left-radius: 0;"
330 {this.state.captcha.ok.wav && (
332 class="rounded-bottom btn btn-sm btn-secondary btn-block"
333 style="border-top-right-radius: 0; border-top-left-radius: 0;"
334 title={i18n.t('play_captcha_audio')}
335 onClick={linkEvent(this, this.handleCaptchaPlay)}
337 disabled={this.state.captchaPlaying}
339 <svg class="icon icon-play">
340 <use xlinkHref="#icon-play"></use>
350 handleLoginSubmit(i: Login, event: any) {
351 event.preventDefault();
352 i.state.loginLoading = true;
354 WebSocketService.Instance.send(wsClient.login(i.state.loginForm));
357 handleLoginUsernameChange(i: Login, event: any) {
358 i.state.loginForm.username_or_email = event.target.value;
362 handleLoginPasswordChange(i: Login, event: any) {
363 i.state.loginForm.password = event.target.value;
367 handleRegisterSubmit(i: Login, event: any) {
368 event.preventDefault();
369 i.state.registerLoading = true;
371 WebSocketService.Instance.send(wsClient.register(i.state.registerForm));
374 handleRegisterUsernameChange(i: Login, event: any) {
375 i.state.registerForm.username = event.target.value;
379 handleRegisterEmailChange(i: Login, event: any) {
380 i.state.registerForm.email = event.target.value;
381 if (i.state.registerForm.email == '') {
382 i.state.registerForm.email = undefined;
387 handleRegisterPasswordChange(i: Login, event: any) {
388 i.state.registerForm.password = event.target.value;
392 handleRegisterPasswordVerifyChange(i: Login, event: any) {
393 i.state.registerForm.password_verify = event.target.value;
397 handleRegisterShowNsfwChange(i: Login, event: any) {
398 i.state.registerForm.show_nsfw = event.target.checked;
402 handleRegisterCaptchaAnswerChange(i: Login, event: any) {
403 i.state.registerForm.captcha_answer = event.target.value;
407 handleRegenCaptcha(_i: Login, event: any) {
408 event.preventDefault();
409 WebSocketService.Instance.send(wsClient.getCaptcha());
412 handlePasswordReset(i: Login, event: any) {
413 event.preventDefault();
414 let resetForm: PasswordReset = {
415 email: i.state.loginForm.username_or_email,
417 WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
420 handleCaptchaPlay(i: Login, event: any) {
421 event.preventDefault();
422 let snd = new Audio('data:audio/wav;base64,' + i.state.captcha.ok.wav);
424 i.state.captchaPlaying = true;
426 snd.addEventListener('ended', () => {
428 i.state.captchaPlaying = false;
429 i.setState(this.state);
434 return `data:image/png;base64,${this.state.captcha.ok.png}`;
437 parseMessage(msg: any) {
438 let op = wsUserOp(msg);
440 toast(i18n.t(msg.error), 'danger');
441 this.state = this.emptyState;
442 this.state.registerForm.captcha_answer = undefined;
443 // Refetch another captcha
444 WebSocketService.Instance.send(wsClient.getCaptcha());
445 this.setState(this.state);
448 if (op == UserOperation.Login) {
449 let data = wsJsonToRes<LoginResponse>(msg).data;
450 this.state = this.emptyState;
451 this.setState(this.state);
452 UserService.Instance.login(data);
453 WebSocketService.Instance.send(
458 toast(i18n.t('logged_in'));
459 this.props.history.push('/');
460 } else if (op == UserOperation.Register) {
461 let data = wsJsonToRes<LoginResponse>(msg).data;
462 this.state = this.emptyState;
463 this.setState(this.state);
464 UserService.Instance.login(data);
465 WebSocketService.Instance.send(
470 this.props.history.push('/communities');
471 } else if (op == UserOperation.GetCaptcha) {
472 let data = wsJsonToRes<GetCaptchaResponse>(msg).data;
474 this.state.captcha = data;
475 this.state.registerForm.captcha_uuid = data.ok.uuid;
476 this.setState(this.state);
478 } else if (op == UserOperation.PasswordReset) {
479 toast(i18n.t('reset_password_mail_sent'));
480 } else if (op == UserOperation.GetSite) {
481 let data = wsJsonToRes<GetSiteResponse>(msg).data;
482 this.state.site_view = data.site_view;
483 this.setState(this.state);