1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
11 } from '../interfaces';
12 import { WebSocketService, UserService } from '../services';
13 import { msgOp, validEmail, toast } from '../utils';
14 import { i18n } from '../i18next';
15 import { T } from 'inferno-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)}>
81 <div class="form-group row">
82 <label class="col-sm-2 col-form-label">
83 <T i18nKey="email_or_username">#</T>
85 <div class="col-sm-10">
89 value={this.state.loginForm.username_or_email}
90 onInput={linkEvent(this, this.handleLoginUsernameChange)}
96 <div class="form-group row">
97 <label class="col-sm-2 col-form-label">
98 <T i18nKey="password">#</T>
100 <div class="col-sm-10">
103 value={this.state.loginForm.password}
104 onInput={linkEvent(this, this.handleLoginPasswordChange)}
109 disabled={!validEmail(this.state.loginForm.username_or_email)}
110 onClick={linkEvent(this, this.handlePasswordReset)}
111 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold"
113 <T i18nKey="forgot_password">#</T>
117 <div class="form-group row">
118 <div class="col-sm-10">
119 <button type="submit" class="btn btn-secondary">
120 {this.state.loginLoading ? (
121 <svg class="icon icon-spinner spin">
122 <use xlinkHref="#icon-spinner"></use>
136 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
138 <T i18nKey="sign_up">#</T>
140 <div class="form-group row">
141 <label class="col-sm-2 col-form-label">
142 <T i18nKey="username">#</T>
144 <div class="col-sm-10">
148 value={this.state.registerForm.username}
149 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
153 pattern="[a-zA-Z0-9_]+"
157 <div class="form-group row">
158 <label class="col-sm-2 col-form-label">
159 <T i18nKey="email">#</T>
161 <div class="col-sm-10">
165 placeholder={i18n.t('optional')}
166 value={this.state.registerForm.email}
167 onInput={linkEvent(this, this.handleRegisterEmailChange)}
172 <div class="form-group row">
173 <label class="col-sm-2 col-form-label">
174 <T i18nKey="password">#</T>
176 <div class="col-sm-10">
179 value={this.state.registerForm.password}
180 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
186 <div class="form-group row">
187 <label class="col-sm-2 col-form-label">
188 <T i18nKey="verify_password">#</T>
190 <div class="col-sm-10">
193 value={this.state.registerForm.password_verify}
194 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
200 {this.state.enable_nsfw && (
201 <div class="form-group row">
202 <div class="col-sm-10">
203 <div class="form-check">
205 class="form-check-input"
207 checked={this.state.registerForm.show_nsfw}
208 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
210 <label class="form-check-label">
211 <T i18nKey="show_nsfw">#</T>
217 <div class="form-group row">
218 <div class="col-sm-10">
219 <button type="submit" class="btn btn-secondary">
220 {this.state.registerLoading ? (
221 <svg class="icon icon-spinner spin">
222 <use xlinkHref="#icon-spinner"></use>
234 handleLoginSubmit(i: Login, event: any) {
235 event.preventDefault();
236 i.state.loginLoading = true;
238 WebSocketService.Instance.login(i.state.loginForm);
241 handleLoginUsernameChange(i: Login, event: any) {
242 i.state.loginForm.username_or_email = event.target.value;
246 handleLoginPasswordChange(i: Login, event: any) {
247 i.state.loginForm.password = event.target.value;
251 handleRegisterSubmit(i: Login, event: any) {
252 event.preventDefault();
253 i.state.registerLoading = true;
256 WebSocketService.Instance.register(i.state.registerForm);
259 handleRegisterUsernameChange(i: Login, event: any) {
260 i.state.registerForm.username = event.target.value;
264 handleRegisterEmailChange(i: Login, event: any) {
265 i.state.registerForm.email = event.target.value;
266 if (i.state.registerForm.email == '') {
267 i.state.registerForm.email = undefined;
272 handleRegisterPasswordChange(i: Login, event: any) {
273 i.state.registerForm.password = event.target.value;
277 handleRegisterPasswordVerifyChange(i: Login, event: any) {
278 i.state.registerForm.password_verify = event.target.value;
282 handleRegisterShowNsfwChange(i: Login, event: any) {
283 i.state.registerForm.show_nsfw = event.target.checked;
287 handlePasswordReset(i: Login) {
288 event.preventDefault();
289 let resetForm: PasswordResetForm = {
290 email: i.state.loginForm.username_or_email,
292 WebSocketService.Instance.passwordReset(resetForm);
295 parseMessage(msg: any) {
296 let op: UserOperation = msgOp(msg);
298 toast(i18n.t(msg.error), 'danger');
299 this.state = this.emptyState;
300 this.setState(this.state);
303 if (op == UserOperation.Login) {
304 this.state = this.emptyState;
305 this.setState(this.state);
306 let res: LoginResponse = msg;
307 UserService.Instance.login(res);
308 toast(i18n.t('logged_in'));
309 this.props.history.push('/');
310 } else if (op == UserOperation.Register) {
311 this.state = this.emptyState;
312 this.setState(this.state);
313 let res: LoginResponse = msg;
314 UserService.Instance.login(res);
315 this.props.history.push('/communities');
316 } else if (op == UserOperation.PasswordReset) {
317 toast(i18n.t('reset_password_mail_sent'));
318 } else if (op == UserOperation.GetSite) {
319 let res: GetSiteResponse = msg;
320 this.state.enable_nsfw = res.site.enable_nsfw;
321 this.setState(this.state);
322 document.title = `${i18n.t('login')} - ${
323 WebSocketService.Instance.site.name