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)}
125 disabled={!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')}
133 <div class="form-group row">
134 <div class="col-sm-10">
135 <button type="submit" class="btn btn-secondary">
136 {this.state.loginLoading ? (
137 <svg class="icon icon-spinner spin">
138 <use xlinkHref="#icon-spinner"></use>
152 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
153 <h5>{i18n.t('sign_up')}</h5>
155 <div class="form-group row">
156 <label class="col-sm-2 col-form-label" htmlFor="register-username">
160 <div class="col-sm-10">
163 id="register-username"
165 value={this.state.registerForm.username}
166 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
170 pattern="[a-zA-Z0-9_]+"
175 <div class="form-group row">
176 <label class="col-sm-2 col-form-label" htmlFor="register-email">
179 <div class="col-sm-10">
184 placeholder={i18n.t('optional')}
185 value={this.state.registerForm.email}
186 onInput={linkEvent(this, this.handleRegisterEmailChange)}
192 <div class="form-group row">
193 <label class="col-sm-2 col-form-label" htmlFor="register-password">
196 <div class="col-sm-10">
199 id="register-password"
200 value={this.state.registerForm.password}
201 autoComplete="new-password"
202 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
209 <div class="form-group row">
211 class="col-sm-2 col-form-label"
212 htmlFor="register-verify-password"
214 {i18n.t('verify_password')}
216 <div class="col-sm-10">
219 id="register-verify-password"
220 value={this.state.registerForm.password_verify}
221 autoComplete="new-password"
222 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
228 <div class="form-group row">
229 <label class="col-sm-10 col-form-label" htmlFor="register-math">
230 {i18n.t('what_is')}{' '}
231 {`${this.state.mathQuestion.a} + ${this.state.mathQuestion.b}?`}
234 <div class="col-sm-2">
239 value={this.state.mathQuestion.answer}
240 onInput={linkEvent(this, this.handleMathAnswerChange)}
245 {this.state.enable_nsfw && (
246 <div class="form-group row">
247 <div class="col-sm-10">
248 <div class="form-check">
250 class="form-check-input"
251 id="register-show-nsfw"
253 checked={this.state.registerForm.show_nsfw}
254 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
256 <label class="form-check-label" htmlFor="register-show-nsfw">
257 {i18n.t('show_nsfw')}
263 <div class="form-group row">
264 <div class="col-sm-10">
267 class="btn btn-secondary"
268 disabled={this.mathCheck}
270 {this.state.registerLoading ? (
271 <svg class="icon icon-spinner spin">
272 <use xlinkHref="#icon-spinner"></use>
284 handleLoginSubmit(i: Login, event: any) {
285 event.preventDefault();
286 i.state.loginLoading = true;
288 WebSocketService.Instance.login(i.state.loginForm);
291 handleLoginUsernameChange(i: Login, event: any) {
292 i.state.loginForm.username_or_email = event.target.value;
296 handleLoginPasswordChange(i: Login, event: any) {
297 i.state.loginForm.password = event.target.value;
301 handleRegisterSubmit(i: Login, event: any) {
302 event.preventDefault();
303 i.state.registerLoading = true;
307 WebSocketService.Instance.register(i.state.registerForm);
311 handleRegisterUsernameChange(i: Login, event: any) {
312 i.state.registerForm.username = event.target.value;
316 handleRegisterEmailChange(i: Login, event: any) {
317 i.state.registerForm.email = event.target.value;
318 if (i.state.registerForm.email == '') {
319 i.state.registerForm.email = undefined;
324 handleRegisterPasswordChange(i: Login, event: any) {
325 i.state.registerForm.password = event.target.value;
329 handleRegisterPasswordVerifyChange(i: Login, event: any) {
330 i.state.registerForm.password_verify = event.target.value;
334 handleRegisterShowNsfwChange(i: Login, event: any) {
335 i.state.registerForm.show_nsfw = event.target.checked;
339 handleMathAnswerChange(i: Login, event: any) {
340 i.state.mathQuestion.answer = event.target.value;
344 handlePasswordReset(i: Login) {
345 event.preventDefault();
346 let resetForm: PasswordResetForm = {
347 email: i.state.loginForm.username_or_email,
349 WebSocketService.Instance.passwordReset(resetForm);
352 get mathCheck(): boolean {
354 this.state.mathQuestion.answer !=
355 this.state.mathQuestion.a + this.state.mathQuestion.b
359 parseMessage(msg: WebSocketJsonResponse) {
360 let res = wsJsonToRes(msg);
362 toast(i18n.t(msg.error), 'danger');
363 this.state = this.emptyState;
364 this.setState(this.state);
367 if (res.op == UserOperation.Login) {
368 let data = res.data as LoginResponse;
369 this.state = this.emptyState;
370 this.setState(this.state);
371 UserService.Instance.login(data);
372 WebSocketService.Instance.userJoin();
373 toast(i18n.t('logged_in'));
374 this.props.history.push('/');
375 } else if (res.op == UserOperation.Register) {
376 let data = res.data as LoginResponse;
377 this.state = this.emptyState;
378 this.setState(this.state);
379 UserService.Instance.login(data);
380 WebSocketService.Instance.userJoin();
381 this.props.history.push('/communities');
382 } else if (res.op == UserOperation.PasswordReset) {
383 toast(i18n.t('reset_password_mail_sent'));
384 } else if (res.op == UserOperation.GetSite) {
385 let data = res.data as GetSiteResponse;
386 this.state.enable_nsfw = data.site.enable_nsfw;
387 this.setState(this.state);
388 document.title = `${i18n.t('login')} - ${data.site.name}`;