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)}
215 {this.state.enable_nsfw && (
216 <div class="form-group row">
217 <div class="col-sm-10">
218 <div class="form-check">
220 class="form-check-input"
221 id="register-show-nsfw"
223 checked={this.state.registerForm.show_nsfw}
224 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
226 <label class="form-check-label" htmlFor="register-show-nsfw">
227 {i18n.t('show_nsfw')}
233 <div class="form-group row">
234 <div class="col-sm-10">
235 <button type="submit" class="btn btn-secondary">
236 {this.state.registerLoading ? (
237 <svg class="icon icon-spinner spin">
238 <use xlinkHref="#icon-spinner"></use>
250 handleLoginSubmit(i: Login, event: any) {
251 event.preventDefault();
252 i.state.loginLoading = true;
254 WebSocketService.Instance.login(i.state.loginForm);
257 handleLoginUsernameChange(i: Login, event: any) {
258 i.state.loginForm.username_or_email = event.target.value;
262 handleLoginPasswordChange(i: Login, event: any) {
263 i.state.loginForm.password = event.target.value;
267 handleRegisterSubmit(i: Login, event: any) {
268 event.preventDefault();
269 i.state.registerLoading = true;
272 WebSocketService.Instance.register(i.state.registerForm);
275 handleRegisterUsernameChange(i: Login, event: any) {
276 i.state.registerForm.username = event.target.value;
280 handleRegisterEmailChange(i: Login, event: any) {
281 i.state.registerForm.email = event.target.value;
282 if (i.state.registerForm.email == '') {
283 i.state.registerForm.email = undefined;
288 handleRegisterPasswordChange(i: Login, event: any) {
289 i.state.registerForm.password = event.target.value;
293 handleRegisterPasswordVerifyChange(i: Login, event: any) {
294 i.state.registerForm.password_verify = event.target.value;
298 handleRegisterShowNsfwChange(i: Login, event: any) {
299 i.state.registerForm.show_nsfw = event.target.checked;
303 handlePasswordReset(i: Login) {
304 event.preventDefault();
305 let resetForm: PasswordResetForm = {
306 email: i.state.loginForm.username_or_email,
308 WebSocketService.Instance.passwordReset(resetForm);
311 parseMessage(msg: WebSocketJsonResponse) {
312 let res = wsJsonToRes(msg);
314 toast(i18n.t(msg.error), 'danger');
315 this.state = this.emptyState;
316 this.setState(this.state);
319 if (res.op == UserOperation.Login) {
320 let data = res.data as LoginResponse;
321 this.state = this.emptyState;
322 this.setState(this.state);
323 UserService.Instance.login(data);
324 WebSocketService.Instance.userJoin();
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 WebSocketService.Instance.userJoin();
333 this.props.history.push('/communities');
334 } else if (res.op == UserOperation.PasswordReset) {
335 toast(i18n.t('reset_password_mail_sent'));
336 } else if (res.op == UserOperation.GetSite) {
337 let data = res.data as GetSiteResponse;
338 this.state.enable_nsfw = data.site.enable_nsfw;
339 this.setState(this.state);
340 document.title = `${i18n.t('login')} - ${
341 WebSocketService.Instance.site.name