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';
16 import { T } from 'inferno-i18next';
20 registerForm: RegisterForm;
21 loginLoading: boolean;
22 registerLoading: boolean;
26 export class Login extends Component<any, State> {
27 private subscription: Subscription;
31 username_or_email: undefined,
37 password_verify: undefined,
42 registerLoading: false,
43 enable_nsfw: undefined,
46 constructor(props: any, context: any) {
47 super(props, context);
49 this.state = this.emptyState;
51 this.subscription = WebSocketService.Instance.subject
52 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
54 msg => this.parseMessage(msg),
55 err => console.error(err),
56 () => console.log('complete')
59 WebSocketService.Instance.getSite();
62 componentWillUnmount() {
63 this.subscription.unsubscribe();
68 <div class="container">
70 <div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
71 <div class="col-12 col-lg-6">{this.registerForm()}</div>
80 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
81 <h5>{ i18n.t('login') }</h5>
82 <div class="form-group row">
83 <label class="col-sm-2 col-form-label" for="login-email-or-username">
84 { i18n.t('email_or_username') }
86 <div class="col-sm-10">
90 id="login-email-or-username"
91 value={this.state.loginForm.username_or_email}
92 onInput={linkEvent(this, this.handleLoginUsernameChange)}
98 <div class="form-group row">
99 <label class="col-sm-2 col-form-label" for="login-password">
100 { i18n.t('password') }
102 <div class="col-sm-10">
106 value={this.state.loginForm.password}
107 onInput={linkEvent(this, this.handleLoginPasswordChange)}
112 disabled={!validEmail(this.state.loginForm.username_or_email)}
113 onClick={linkEvent(this, this.handlePasswordReset)}
114 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold"
116 { i18n.t('forgot_password') }
120 <div class="form-group row">
121 <div class="col-sm-10">
122 <button type="submit" class="btn btn-secondary">
123 {this.state.loginLoading ? (
124 <svg class="icon icon-spinner spin">
125 <use xlinkHref="#icon-spinner"></use>
139 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
141 { i18n.t('sign_up') }
144 <div class="form-group row">
145 <label class="col-sm-2 col-form-label" for="register-username">
146 { i18n.t('username') }
149 <div class="col-sm-10">
152 id="register-username"
154 value={this.state.registerForm.username}
155 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
159 pattern="[a-zA-Z0-9_]+"
164 <div class="form-group row">
165 <label class="col-sm-2 col-form-label" for="register-email">
168 <div class="col-sm-10">
173 placeholder={i18n.t('optional')}
174 value={this.state.registerForm.email}
175 onInput={linkEvent(this, this.handleRegisterEmailChange)}
181 <div class="form-group row">
182 <label class="col-sm-2 col-form-label" for="register-password">
183 { i18n.t('password') }
185 <div class="col-sm-10">
188 id="register-password"
189 value={this.state.registerForm.password}
190 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
197 <div class="form-group row">
198 <label class="col-sm-2 col-form-label" for="register-verify-password">
199 { i18n.t('verify_password') }
201 <div class="col-sm-10">
204 id="register-verify-password"
205 value={this.state.registerForm.password_verify}
206 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
213 { this.state.enable_nsfw && (
214 <div class="form-group row">
215 <div class="col-sm-10">
216 <div class="form-check">
218 class="form-check-input"
219 id="register-show-nsfw"
221 checked={this.state.registerForm.show_nsfw}
222 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
224 <label class="form-check-label" for="register-show-nsfw">
225 { i18n.t('show_nsfw') }
231 <div class="form-group row">
232 <div class="col-sm-10">
233 <button type="submit" class="btn btn-secondary">
234 {this.state.registerLoading ? (
235 <svg class="icon icon-spinner spin">
236 <use xlinkHref="#icon-spinner"></use>
248 handleLoginSubmit(i: Login, event: any) {
249 event.preventDefault();
250 i.state.loginLoading = true;
252 WebSocketService.Instance.login(i.state.loginForm);
255 handleLoginUsernameChange(i: Login, event: any) {
256 i.state.loginForm.username_or_email = event.target.value;
260 handleLoginPasswordChange(i: Login, event: any) {
261 i.state.loginForm.password = event.target.value;
265 handleRegisterSubmit(i: Login, event: any) {
266 event.preventDefault();
267 i.state.registerLoading = true;
270 WebSocketService.Instance.register(i.state.registerForm);
273 handleRegisterUsernameChange(i: Login, event: any) {
274 i.state.registerForm.username = event.target.value;
278 handleRegisterEmailChange(i: Login, event: any) {
279 i.state.registerForm.email = event.target.value;
280 if (i.state.registerForm.email == '') {
281 i.state.registerForm.email = undefined;
286 handleRegisterPasswordChange(i: Login, event: any) {
287 i.state.registerForm.password = event.target.value;
291 handleRegisterPasswordVerifyChange(i: Login, event: any) {
292 i.state.registerForm.password_verify = event.target.value;
296 handleRegisterShowNsfwChange(i: Login, event: any) {
297 i.state.registerForm.show_nsfw = event.target.checked;
301 handlePasswordReset(i: Login) {
302 event.preventDefault();
303 let resetForm: PasswordResetForm = {
304 email: i.state.loginForm.username_or_email,
306 WebSocketService.Instance.passwordReset(resetForm);
309 parseMessage(msg: WebSocketJsonResponse) {
310 let res = wsJsonToRes(msg);
312 toast(i18n.t(msg.error), 'danger');
313 this.state = this.emptyState;
314 this.setState(this.state);
317 if (res.op == UserOperation.Login) {
318 let data = res.data as LoginResponse;
319 this.state = this.emptyState;
320 this.setState(this.state);
321 UserService.Instance.login(data);
322 toast(i18n.t('logged_in'));
323 this.props.history.push('/');
324 } else if (res.op == UserOperation.Register) {
325 let data = res.data as LoginResponse;
326 this.state = this.emptyState;
327 this.setState(this.state);
328 UserService.Instance.login(data);
329 this.props.history.push('/communities');
330 } else if (res.op == UserOperation.PasswordReset) {
331 toast(i18n.t('reset_password_mail_sent'));
332 } else if (res.op == UserOperation.GetSite) {
333 let data = res.data as GetSiteResponse;
334 this.state.enable_nsfw = data.site.enable_nsfw;
335 this.setState(this.state);
336 document.title = `${i18n.t('login')} - ${
337 WebSocketService.Instance.site.name