1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
11 WebSocketJsonResponse,
12 } from '../interfaces';
13 import { WebSocketService, UserService } from '../services';
14 import { wsJsonToRes, validEmail, toast } from '../utils';
15 import { i18n } from '../i18next';
19 registerForm: RegisterForm;
20 loginLoading: boolean;
21 registerLoading: boolean;
30 export class Login extends Component<any, State> {
31 private subscription: Subscription;
35 username_or_email: undefined,
41 password_verify: undefined,
46 registerLoading: false,
47 enable_nsfw: undefined,
49 a: Math.floor(Math.random() * 10) + 1,
50 b: Math.floor(Math.random() * 10) + 1,
55 constructor(props: any, context: any) {
56 super(props, context);
58 this.state = this.emptyState;
60 this.subscription = WebSocketService.Instance.subject
61 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
63 msg => this.parseMessage(msg),
64 err => console.error(err),
65 () => console.log('complete')
68 WebSocketService.Instance.getSite();
71 componentWillUnmount() {
72 this.subscription.unsubscribe();
77 <div class="container">
79 <div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
80 <div class="col-12 col-lg-6">{this.registerForm()}</div>
89 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
90 <h5>{i18n.t('login')}</h5>
91 <div class="form-group row">
93 class="col-sm-2 col-form-label"
94 htmlFor="login-email-or-username"
96 {i18n.t('email_or_username')}
98 <div class="col-sm-10">
102 id="login-email-or-username"
103 value={this.state.loginForm.username_or_email}
104 onInput={linkEvent(this, this.handleLoginUsernameChange)}
110 <div class="form-group row">
111 <label class="col-sm-2 col-form-label" htmlFor="login-password">
114 <div class="col-sm-10">
118 value={this.state.loginForm.password}
119 onInput={linkEvent(this, this.handleLoginPasswordChange)}
123 {validEmail(this.state.loginForm.username_or_email) && (
126 onClick={linkEvent(this, this.handlePasswordReset)}
127 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold"
129 {i18n.t('forgot_password')}
134 <div class="form-group row">
135 <div class="col-sm-10">
136 <button type="submit" class="btn btn-secondary">
137 {this.state.loginLoading ? (
138 <svg class="icon icon-spinner spin">
139 <use xlinkHref="#icon-spinner"></use>
153 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
154 <h5>{i18n.t('sign_up')}</h5>
156 <div class="form-group row">
157 <label class="col-sm-2 col-form-label" htmlFor="register-username">
161 <div class="col-sm-10">
164 id="register-username"
166 value={this.state.registerForm.username}
167 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
171 pattern="[a-zA-Z0-9_]+"
176 <div class="form-group row">
177 <label class="col-sm-2 col-form-label" htmlFor="register-email">
180 <div class="col-sm-10">
185 placeholder={i18n.t('optional')}
186 value={this.state.registerForm.email}
187 onInput={linkEvent(this, this.handleRegisterEmailChange)}
190 {!validEmail(this.state.registerForm.email) && (
191 <div class="mt-2 mb-0 alert alert-light" role="alert">
192 <svg class="icon icon-inline mr-2">
193 <use xlinkHref="#icon-alert-triangle"></use>
195 {i18n.t('no_password_reset')}
201 <div class="form-group row">
202 <label class="col-sm-2 col-form-label" htmlFor="register-password">
205 <div class="col-sm-10">
208 id="register-password"
209 value={this.state.registerForm.password}
210 autoComplete="new-password"
211 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
218 <div class="form-group row">
220 class="col-sm-2 col-form-label"
221 htmlFor="register-verify-password"
223 {i18n.t('verify_password')}
225 <div class="col-sm-10">
228 id="register-verify-password"
229 value={this.state.registerForm.password_verify}
230 autoComplete="new-password"
231 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
237 <div class="form-group row">
238 <label class="col-sm-10 col-form-label" htmlFor="register-math">
239 {i18n.t('what_is')}{' '}
240 {`${this.state.mathQuestion.a} + ${this.state.mathQuestion.b}?`}
243 <div class="col-sm-2">
248 value={this.state.mathQuestion.answer}
249 onInput={linkEvent(this, this.handleMathAnswerChange)}
254 {this.state.enable_nsfw && (
255 <div class="form-group row">
256 <div class="col-sm-10">
257 <div class="form-check">
259 class="form-check-input"
260 id="register-show-nsfw"
262 checked={this.state.registerForm.show_nsfw}
263 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
265 <label class="form-check-label" htmlFor="register-show-nsfw">
266 {i18n.t('show_nsfw')}
272 <div class="form-group row">
273 <div class="col-sm-10">
276 class="btn btn-secondary"
277 disabled={this.mathCheck}
279 {this.state.registerLoading ? (
280 <svg class="icon icon-spinner spin">
281 <use xlinkHref="#icon-spinner"></use>
293 handleLoginSubmit(i: Login, event: any) {
294 event.preventDefault();
295 i.state.loginLoading = true;
297 WebSocketService.Instance.login(i.state.loginForm);
300 handleLoginUsernameChange(i: Login, event: any) {
301 i.state.loginForm.username_or_email = event.target.value;
305 handleLoginPasswordChange(i: Login, event: any) {
306 i.state.loginForm.password = event.target.value;
310 handleRegisterSubmit(i: Login, event: any) {
311 event.preventDefault();
312 i.state.registerLoading = true;
316 WebSocketService.Instance.register(i.state.registerForm);
320 handleRegisterUsernameChange(i: Login, event: any) {
321 i.state.registerForm.username = event.target.value;
325 handleRegisterEmailChange(i: Login, event: any) {
326 i.state.registerForm.email = event.target.value;
327 if (i.state.registerForm.email == '') {
328 i.state.registerForm.email = undefined;
333 handleRegisterPasswordChange(i: Login, event: any) {
334 i.state.registerForm.password = event.target.value;
338 handleRegisterPasswordVerifyChange(i: Login, event: any) {
339 i.state.registerForm.password_verify = event.target.value;
343 handleRegisterShowNsfwChange(i: Login, event: any) {
344 i.state.registerForm.show_nsfw = event.target.checked;
348 handleMathAnswerChange(i: Login, event: any) {
349 i.state.mathQuestion.answer = event.target.value;
353 handlePasswordReset(i: Login) {
354 event.preventDefault();
355 let resetForm: PasswordResetForm = {
356 email: i.state.loginForm.username_or_email,
358 WebSocketService.Instance.passwordReset(resetForm);
361 get mathCheck(): boolean {
363 this.state.mathQuestion.answer !=
364 this.state.mathQuestion.a + this.state.mathQuestion.b
368 parseMessage(msg: WebSocketJsonResponse) {
369 let res = wsJsonToRes(msg);
371 toast(i18n.t(msg.error), 'danger');
372 this.state = this.emptyState;
373 this.setState(this.state);
376 if (res.op == UserOperation.Login) {
377 let data = res.data as LoginResponse;
378 this.state = this.emptyState;
379 this.setState(this.state);
380 UserService.Instance.login(data);
381 WebSocketService.Instance.userJoin();
382 toast(i18n.t('logged_in'));
383 this.props.history.push('/');
384 } else if (res.op == UserOperation.Register) {
385 let data = res.data as LoginResponse;
386 this.state = this.emptyState;
387 this.setState(this.state);
388 UserService.Instance.login(data);
389 WebSocketService.Instance.userJoin();
390 this.props.history.push('/communities');
391 } else if (res.op == UserOperation.PasswordReset) {
392 toast(i18n.t('reset_password_mail_sent'));
393 } else if (res.op == UserOperation.GetSite) {
394 let data = res.data as GetSiteResponse;
395 this.state.enable_nsfw = data.site.enable_nsfw;
396 this.setState(this.state);
397 document.title = `${i18n.t('login')} - ${data.site.name}`;