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;
25 export class Login extends Component<any, State> {
26 private subscription: Subscription;
30 username_or_email: undefined,
36 password_verify: undefined,
41 registerLoading: false,
42 enable_nsfw: undefined,
45 constructor(props: any, context: any) {
46 super(props, context);
48 this.state = this.emptyState;
50 this.subscription = WebSocketService.Instance.subject
51 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
53 msg => this.parseMessage(msg),
54 err => console.error(err),
55 () => console.log('complete')
58 WebSocketService.Instance.getSite();
61 componentWillUnmount() {
62 this.subscription.unsubscribe();
67 <div class="container">
69 <div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
70 <div class="col-12 col-lg-6">{this.registerForm()}</div>
79 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
80 <h5>{i18n.t('login')}</h5>
81 <div class="form-group row">
83 class="col-sm-2 col-form-label"
84 htmlFor="login-email-or-username"
86 {i18n.t('email_or_username')}
88 <div class="col-sm-10">
92 id="login-email-or-username"
93 value={this.state.loginForm.username_or_email}
94 onInput={linkEvent(this, this.handleLoginUsernameChange)}
100 <div class="form-group row">
101 <label class="col-sm-2 col-form-label" htmlFor="login-password">
104 <div class="col-sm-10">
108 value={this.state.loginForm.password}
109 onInput={linkEvent(this, this.handleLoginPasswordChange)}
114 disabled={!validEmail(this.state.loginForm.username_or_email)}
115 onClick={linkEvent(this, this.handlePasswordReset)}
116 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold"
118 {i18n.t('forgot_password')}
122 <div class="form-group row">
123 <div class="col-sm-10">
124 <button type="submit" class="btn btn-secondary">
125 {this.state.loginLoading ? (
126 <svg class="icon icon-spinner spin">
127 <use xlinkHref="#icon-spinner"></use>
141 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
142 <h5>{i18n.t('sign_up')}</h5>
144 <div class="form-group row">
145 <label class="col-sm-2 col-form-label" htmlFor="register-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" htmlFor="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" htmlFor="register-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">
199 class="col-sm-2 col-form-label"
200 htmlFor="register-verify-password"
202 {i18n.t('verify_password')}
204 <div class="col-sm-10">
207 id="register-verify-password"
208 value={this.state.registerForm.password_verify}
209 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
216 {this.state.enable_nsfw && (
217 <div class="form-group row">
218 <div class="col-sm-10">
219 <div class="form-check">
221 class="form-check-input"
222 id="register-show-nsfw"
224 checked={this.state.registerForm.show_nsfw}
225 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
227 <label class="form-check-label" htmlFor="register-show-nsfw">
228 {i18n.t('show_nsfw')}
234 <div class="form-group row">
235 <div class="col-sm-10">
236 <button type="submit" class="btn btn-secondary">
237 {this.state.registerLoading ? (
238 <svg class="icon icon-spinner spin">
239 <use xlinkHref="#icon-spinner"></use>
251 handleLoginSubmit(i: Login, event: any) {
252 event.preventDefault();
253 i.state.loginLoading = true;
255 WebSocketService.Instance.login(i.state.loginForm);
258 handleLoginUsernameChange(i: Login, event: any) {
259 i.state.loginForm.username_or_email = event.target.value;
263 handleLoginPasswordChange(i: Login, event: any) {
264 i.state.loginForm.password = event.target.value;
268 handleRegisterSubmit(i: Login, event: any) {
269 event.preventDefault();
270 i.state.registerLoading = true;
273 WebSocketService.Instance.register(i.state.registerForm);
276 handleRegisterUsernameChange(i: Login, event: any) {
277 i.state.registerForm.username = event.target.value;
281 handleRegisterEmailChange(i: Login, event: any) {
282 i.state.registerForm.email = event.target.value;
283 if (i.state.registerForm.email == '') {
284 i.state.registerForm.email = undefined;
289 handleRegisterPasswordChange(i: Login, event: any) {
290 i.state.registerForm.password = event.target.value;
294 handleRegisterPasswordVerifyChange(i: Login, event: any) {
295 i.state.registerForm.password_verify = event.target.value;
299 handleRegisterShowNsfwChange(i: Login, event: any) {
300 i.state.registerForm.show_nsfw = event.target.checked;
304 handlePasswordReset(i: Login) {
305 event.preventDefault();
306 let resetForm: PasswordResetForm = {
307 email: i.state.loginForm.username_or_email,
309 WebSocketService.Instance.passwordReset(resetForm);
312 parseMessage(msg: WebSocketJsonResponse) {
313 let res = wsJsonToRes(msg);
315 toast(i18n.t(msg.error), 'danger');
316 this.state = this.emptyState;
317 this.setState(this.state);
320 if (res.op == UserOperation.Login) {
321 let data = res.data as LoginResponse;
322 this.state = this.emptyState;
323 this.setState(this.state);
324 UserService.Instance.login(data);
325 toast(i18n.t('logged_in'));
326 this.props.history.push('/');
327 } else if (res.op == UserOperation.Register) {
328 let data = res.data as LoginResponse;
329 this.state = this.emptyState;
330 this.setState(this.state);
331 UserService.Instance.login(data);
332 this.props.history.push('/communities');
333 } else if (res.op == UserOperation.PasswordReset) {
334 toast(i18n.t('reset_password_mail_sent'));
335 } else if (res.op == UserOperation.GetSite) {
336 let data = res.data as GetSiteResponse;
337 this.state.enable_nsfw = data.site.enable_nsfw;
338 this.setState(this.state);
339 document.title = `${i18n.t('login')} - ${
340 WebSocketService.Instance.site.name