1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
10 } from '../interfaces';
11 import { WebSocketService, UserService } from '../services';
12 import { msgOp } from '../utils';
13 import { i18n } from '../i18next';
14 import { T } from 'inferno-i18next';
18 registerForm: RegisterForm;
19 loginLoading: boolean;
20 registerLoading: boolean;
23 export class Login extends Component<any, State> {
24 private subscription: Subscription;
28 username_or_email: undefined,
34 password_verify: undefined,
39 registerLoading: false,
42 constructor(props: any, context: any) {
43 super(props, context);
45 this.state = this.emptyState;
47 this.subscription = WebSocketService.Instance.subject
57 msg => this.parseMessage(msg),
58 err => console.error(err),
59 () => console.log('complete')
63 componentWillUnmount() {
64 this.subscription.unsubscribe();
68 document.title = `${i18n.t('login')} - ${
69 WebSocketService.Instance.site.name
75 <div class="container">
77 <div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
78 <div class="col-12 col-lg-6">{this.registerForm()}</div>
87 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
89 <div class="form-group row">
90 <label class="col-sm-2 col-form-label">
91 <T i18nKey="email_or_username">#</T>
93 <div class="col-sm-10">
97 value={this.state.loginForm.username_or_email}
98 onInput={linkEvent(this, this.handleLoginUsernameChange)}
104 <div class="form-group row">
105 <label class="col-sm-2 col-form-label">
106 <T i18nKey="password">#</T>
108 <div class="col-sm-10">
111 value={this.state.loginForm.password}
112 onInput={linkEvent(this, this.handleLoginPasswordChange)}
117 onClick={linkEvent(this, this.handlePasswordReset)}
118 class="pointer d-inline-block float-right text-muted small font-weight-bold"
120 <T i18nKey="forgot_password">#</T>
124 <div class="form-group row">
125 <div class="col-sm-10">
126 <button type="submit" class="btn btn-secondary">
127 {this.state.loginLoading ? (
128 <svg class="icon icon-spinner spin">
129 <use xlinkHref="#icon-spinner"></use>
143 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
145 <T i18nKey="sign_up">#</T>
147 <div class="form-group row">
148 <label class="col-sm-2 col-form-label">
149 <T i18nKey="username">#</T>
151 <div class="col-sm-10">
155 value={this.state.registerForm.username}
156 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
160 pattern="[a-zA-Z0-9_]+"
164 <div class="form-group row">
165 <label class="col-sm-2 col-form-label">
166 <T i18nKey="email">#</T>
168 <div class="col-sm-10">
172 placeholder={i18n.t('optional')}
173 value={this.state.registerForm.email}
174 onInput={linkEvent(this, this.handleRegisterEmailChange)}
179 <div class="form-group row">
180 <label class="col-sm-2 col-form-label">
181 <T i18nKey="password">#</T>
183 <div class="col-sm-10">
186 value={this.state.registerForm.password}
187 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
193 <div class="form-group row">
194 <label class="col-sm-2 col-form-label">
195 <T i18nKey="verify_password">#</T>
197 <div class="col-sm-10">
200 value={this.state.registerForm.password_verify}
201 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
207 <div class="form-group row">
208 <div class="col-sm-10">
209 <div class="form-check">
211 class="form-check-input"
213 checked={this.state.registerForm.show_nsfw}
214 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
216 <label class="form-check-label">
217 <T i18nKey="show_nsfw">#</T>
222 <div class="form-group row">
223 <div class="col-sm-10">
224 <button type="submit" class="btn btn-secondary">
225 {this.state.registerLoading ? (
226 <svg class="icon icon-spinner spin">
227 <use xlinkHref="#icon-spinner"></use>
239 handleLoginSubmit(i: Login, event: any) {
240 event.preventDefault();
241 i.state.loginLoading = true;
243 WebSocketService.Instance.login(i.state.loginForm);
246 handleLoginUsernameChange(i: Login, event: any) {
247 i.state.loginForm.username_or_email = event.target.value;
251 handleLoginPasswordChange(i: Login, event: any) {
252 i.state.loginForm.password = event.target.value;
256 handleRegisterSubmit(i: Login, event: any) {
257 event.preventDefault();
258 i.state.registerLoading = true;
261 WebSocketService.Instance.register(i.state.registerForm);
264 handleRegisterUsernameChange(i: Login, event: any) {
265 i.state.registerForm.username = event.target.value;
269 handleRegisterEmailChange(i: Login, event: any) {
270 i.state.registerForm.email = event.target.value;
274 handleRegisterPasswordChange(i: Login, event: any) {
275 i.state.registerForm.password = event.target.value;
279 handleRegisterPasswordVerifyChange(i: Login, event: any) {
280 i.state.registerForm.password_verify = event.target.value;
284 handleRegisterShowNsfwChange(i: Login, event: any) {
285 i.state.registerForm.show_nsfw = event.target.checked;
289 handlePasswordReset(i: Login) {
290 let resetForm: PasswordResetForm = {
291 email: i.state.loginForm.username_or_email,
293 WebSocketService.Instance.passwordReset(resetForm);
296 parseMessage(msg: any) {
297 let op: UserOperation = msgOp(msg);
299 alert(i18n.t(msg.error));
300 this.state = this.emptyState;
301 this.setState(this.state);
304 if (op == UserOperation.Login) {
305 this.state = this.emptyState;
306 this.setState(this.state);
307 let res: LoginResponse = msg;
308 UserService.Instance.login(res);
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 alert(i18n.t('reset_password_mail_sent'));