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,
52 captcha_uuid: undefined,
53 captcha_answer: undefined,
56 registerLoading: false,
58 captchaPlaying: false,
59 site_view: this.isoData.site_res.site_view,
62 constructor(props: any, context: any) {
63 super(props, context);
65 this.state = this.emptyState;
67 this.parseMessage = this.parseMessage.bind(this);
68 this.subscription = wsSubscribe(this.parseMessage);
71 WebSocketService.Instance.send(wsClient.getCaptcha());
75 componentWillUnmount() {
77 this.subscription.unsubscribe();
81 get documentTitle(): string {
82 return `${i18n.t('login')} - ${this.state.site_view.site.name}`;
87 <div class="container">
89 title={this.documentTitle}
90 path={this.context.router.route.match.url}
93 <div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
94 <div class="col-12 col-lg-6">{this.registerForm()}</div>
103 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
104 <h5>{i18n.t('login')}</h5>
105 <div class="form-group row">
107 class="col-sm-2 col-form-label"
108 htmlFor="login-email-or-username"
110 {i18n.t('email_or_username')}
112 <div class="col-sm-10">
116 id="login-email-or-username"
117 value={this.state.loginForm.username_or_email}
118 onInput={linkEvent(this, this.handleLoginUsernameChange)}
124 <div class="form-group row">
125 <label class="col-sm-2 col-form-label" htmlFor="login-password">
128 <div class="col-sm-10">
132 value={this.state.loginForm.password}
133 onInput={linkEvent(this, this.handleLoginPasswordChange)}
137 {validEmail(this.state.loginForm.username_or_email) && (
140 onClick={linkEvent(this, this.handlePasswordReset)}
141 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold"
143 {i18n.t('forgot_password')}
148 <div class="form-group row">
149 <div class="col-sm-10">
150 <button type="submit" class="btn btn-secondary">
151 {this.state.loginLoading ? (
152 <svg class="icon icon-spinner spin">
153 <use xlinkHref="#icon-spinner"></use>
168 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
169 <h5>{i18n.t('sign_up')}</h5>
171 <div class="form-group row">
172 <label class="col-sm-2 col-form-label" htmlFor="register-username">
176 <div class="col-sm-10">
179 id="register-username"
181 value={this.state.registerForm.username}
182 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
186 pattern="[a-zA-Z0-9_]+"
191 <div class="form-group row">
192 <label class="col-sm-2 col-form-label" htmlFor="register-email">
195 <div class="col-sm-10">
200 placeholder={i18n.t('optional')}
201 value={this.state.registerForm.email}
202 onInput={linkEvent(this, this.handleRegisterEmailChange)}
205 {!validEmail(this.state.registerForm.email) && (
206 <div class="mt-2 mb-0 alert alert-light" role="alert">
207 <svg class="icon icon-inline mr-2">
208 <use xlinkHref="#icon-alert-triangle"></use>
210 {i18n.t('no_password_reset')}
216 <div class="form-group row">
217 <label class="col-sm-2 col-form-label" htmlFor="register-password">
220 <div class="col-sm-10">
223 id="register-password"
224 value={this.state.registerForm.password}
225 autoComplete="new-password"
226 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
234 <div class="form-group row">
236 class="col-sm-2 col-form-label"
237 htmlFor="register-verify-password"
239 {i18n.t('verify_password')}
241 <div class="col-sm-10">
244 id="register-verify-password"
245 value={this.state.registerForm.password_verify}
246 autoComplete="new-password"
247 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
254 {this.state.captcha && (
255 <div class="form-group row">
256 <label class="col-sm-2" htmlFor="register-captcha">
257 <span class="mr-2">{i18n.t('enter_code')}</span>
260 class="btn btn-secondary"
261 onClick={linkEvent(this, this.handleRegenCaptcha)}
263 <svg class="icon icon-refresh-cw">
264 <use xlinkHref="#icon-refresh-cw"></use>
269 <div class="col-sm-6">
273 id="register-captcha"
274 value={this.state.registerForm.captcha_answer}
277 this.handleRegisterCaptchaAnswerChange
284 {this.state.site_view.site.enable_nsfw && (
285 <div class="form-group row">
286 <div class="col-sm-10">
287 <div class="form-check">
289 class="form-check-input"
290 id="register-show-nsfw"
292 checked={this.state.registerForm.show_nsfw}
293 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
295 <label class="form-check-label" htmlFor="register-show-nsfw">
296 {i18n.t('show_nsfw')}
302 <div class="form-group row">
303 <div class="col-sm-10">
304 <button type="submit" class="btn btn-secondary">
305 {this.state.registerLoading ? (
306 <svg class="icon icon-spinner spin">
307 <use xlinkHref="#icon-spinner"></use>
321 <div class="col-sm-4">
322 {this.state.captcha.ok && (
325 class="rounded-top img-fluid"
326 src={this.captchaPngSrc()}
327 style="border-bottom-right-radius: 0; border-bottom-left-radius: 0;"
329 {this.state.captcha.ok.wav && (
331 class="rounded-bottom btn btn-sm btn-secondary btn-block"
332 style="border-top-right-radius: 0; border-top-left-radius: 0;"
333 title={i18n.t('play_captcha_audio')}
334 onClick={linkEvent(this, this.handleCaptchaPlay)}
336 disabled={this.state.captchaPlaying}
338 <svg class="icon icon-play">
339 <use xlinkHref="#icon-play"></use>
349 handleLoginSubmit(i: Login, event: any) {
350 event.preventDefault();
351 i.state.loginLoading = true;
353 WebSocketService.Instance.send(wsClient.login(i.state.loginForm));
356 handleLoginUsernameChange(i: Login, event: any) {
357 i.state.loginForm.username_or_email = event.target.value;
361 handleLoginPasswordChange(i: Login, event: any) {
362 i.state.loginForm.password = event.target.value;
366 handleRegisterSubmit(i: Login, event: any) {
367 event.preventDefault();
368 i.state.registerLoading = true;
370 WebSocketService.Instance.send(wsClient.register(i.state.registerForm));
373 handleRegisterUsernameChange(i: Login, event: any) {
374 i.state.registerForm.username = event.target.value;
378 handleRegisterEmailChange(i: Login, event: any) {
379 i.state.registerForm.email = event.target.value;
380 if (i.state.registerForm.email == '') {
381 i.state.registerForm.email = undefined;
386 handleRegisterPasswordChange(i: Login, event: any) {
387 i.state.registerForm.password = event.target.value;
391 handleRegisterPasswordVerifyChange(i: Login, event: any) {
392 i.state.registerForm.password_verify = event.target.value;
396 handleRegisterShowNsfwChange(i: Login, event: any) {
397 i.state.registerForm.show_nsfw = event.target.checked;
401 handleRegisterCaptchaAnswerChange(i: Login, event: any) {
402 i.state.registerForm.captcha_answer = event.target.value;
406 handleRegenCaptcha(_i: Login, event: any) {
407 event.preventDefault();
408 WebSocketService.Instance.send(wsClient.getCaptcha());
411 handlePasswordReset(i: Login, event: any) {
412 event.preventDefault();
413 let resetForm: PasswordReset = {
414 email: i.state.loginForm.username_or_email,
416 WebSocketService.Instance.send(wsClient.passwordReset(resetForm));
419 handleCaptchaPlay(i: Login, event: any) {
420 event.preventDefault();
421 let snd = new Audio('data:audio/wav;base64,' + i.state.captcha.ok.wav);
423 i.state.captchaPlaying = true;
425 snd.addEventListener('ended', () => {
427 i.state.captchaPlaying = false;
428 i.setState(this.state);
433 return `data:image/png;base64,${this.state.captcha.ok.png}`;
436 parseMessage(msg: any) {
437 let op = wsUserOp(msg);
439 toast(i18n.t(msg.error), 'danger');
440 this.state = this.emptyState;
441 this.state.registerForm.captcha_answer = undefined;
442 // Refetch another captcha
443 WebSocketService.Instance.send(wsClient.getCaptcha());
444 this.setState(this.state);
447 if (op == UserOperation.Login) {
448 let data = wsJsonToRes<LoginResponse>(msg).data;
449 this.state = this.emptyState;
450 this.setState(this.state);
451 UserService.Instance.login(data);
452 WebSocketService.Instance.send(
457 toast(i18n.t('logged_in'));
458 this.props.history.push('/');
459 } else if (op == UserOperation.Register) {
460 let data = wsJsonToRes<LoginResponse>(msg).data;
461 this.state = this.emptyState;
462 this.setState(this.state);
463 UserService.Instance.login(data);
464 WebSocketService.Instance.send(
469 this.props.history.push('/communities');
470 } else if (op == UserOperation.GetCaptcha) {
471 let data = wsJsonToRes<GetCaptchaResponse>(msg).data;
473 this.state.captcha = data;
474 this.state.registerForm.captcha_uuid = data.ok.uuid;
475 this.setState(this.state);
477 } else if (op == UserOperation.PasswordReset) {
478 toast(i18n.t('reset_password_mail_sent'));
479 } else if (op == UserOperation.GetSite) {
480 let data = wsJsonToRes<GetSiteResponse>(msg).data;
481 this.state.site_view = data.site_view;
482 this.setState(this.state);