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, validEmail } 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 disabled={!validEmail(this.state.loginForm.username_or_email)}
118 onClick={linkEvent(this, this.handlePasswordReset)}
119 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold"
121 <T i18nKey="forgot_password">#</T>
125 <div class="form-group row">
126 <div class="col-sm-10">
127 <button type="submit" class="btn btn-secondary">
128 {this.state.loginLoading ? (
129 <svg class="icon icon-spinner spin">
130 <use xlinkHref="#icon-spinner"></use>
144 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
146 <T i18nKey="sign_up">#</T>
148 <div class="form-group row">
149 <label class="col-sm-2 col-form-label">
150 <T i18nKey="username">#</T>
152 <div class="col-sm-10">
156 value={this.state.registerForm.username}
157 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
161 pattern="[a-zA-Z0-9_]+"
165 <div class="form-group row">
166 <label class="col-sm-2 col-form-label">
167 <T i18nKey="email">#</T>
169 <div class="col-sm-10">
173 placeholder={i18n.t('optional')}
174 value={this.state.registerForm.email}
175 onInput={linkEvent(this, this.handleRegisterEmailChange)}
180 <div class="form-group row">
181 <label class="col-sm-2 col-form-label">
182 <T i18nKey="password">#</T>
184 <div class="col-sm-10">
187 value={this.state.registerForm.password}
188 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
194 <div class="form-group row">
195 <label class="col-sm-2 col-form-label">
196 <T i18nKey="verify_password">#</T>
198 <div class="col-sm-10">
201 value={this.state.registerForm.password_verify}
202 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
208 {WebSocketService.Instance.site.enable_nsfw && (
209 <div class="form-group row">
210 <div class="col-sm-10">
211 <div class="form-check">
213 class="form-check-input"
215 checked={this.state.registerForm.show_nsfw}
216 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
218 <label class="form-check-label">
219 <T i18nKey="show_nsfw">#</T>
225 <div class="form-group row">
226 <div class="col-sm-10">
227 <button type="submit" class="btn btn-secondary">
228 {this.state.registerLoading ? (
229 <svg class="icon icon-spinner spin">
230 <use xlinkHref="#icon-spinner"></use>
242 handleLoginSubmit(i: Login, event: any) {
243 event.preventDefault();
244 i.state.loginLoading = true;
246 WebSocketService.Instance.login(i.state.loginForm);
249 handleLoginUsernameChange(i: Login, event: any) {
250 i.state.loginForm.username_or_email = event.target.value;
254 handleLoginPasswordChange(i: Login, event: any) {
255 i.state.loginForm.password = event.target.value;
259 handleRegisterSubmit(i: Login, event: any) {
260 event.preventDefault();
261 i.state.registerLoading = true;
264 WebSocketService.Instance.register(i.state.registerForm);
267 handleRegisterUsernameChange(i: Login, event: any) {
268 i.state.registerForm.username = event.target.value;
272 handleRegisterEmailChange(i: Login, event: any) {
273 i.state.registerForm.email = event.target.value;
274 if (i.state.registerForm.email == '') {
275 i.state.registerForm.email = undefined;
280 handleRegisterPasswordChange(i: Login, event: any) {
281 i.state.registerForm.password = event.target.value;
285 handleRegisterPasswordVerifyChange(i: Login, event: any) {
286 i.state.registerForm.password_verify = event.target.value;
290 handleRegisterShowNsfwChange(i: Login, event: any) {
291 i.state.registerForm.show_nsfw = event.target.checked;
295 handlePasswordReset(i: Login) {
296 event.preventDefault();
297 let resetForm: PasswordResetForm = {
298 email: i.state.loginForm.username_or_email,
300 WebSocketService.Instance.passwordReset(resetForm);
303 parseMessage(msg: any) {
304 let op: UserOperation = msgOp(msg);
306 alert(i18n.t(msg.error));
307 this.state = this.emptyState;
308 this.setState(this.state);
311 if (op == UserOperation.Login) {
312 this.state = this.emptyState;
313 this.setState(this.state);
314 let res: LoginResponse = msg;
315 UserService.Instance.login(res);
316 this.props.history.push('/');
317 } else if (op == UserOperation.Register) {
318 this.state = this.emptyState;
319 this.setState(this.state);
320 let res: LoginResponse = msg;
321 UserService.Instance.login(res);
322 this.props.history.push('/communities');
323 } else if (op == UserOperation.PasswordReset) {
324 alert(i18n.t('reset_password_mail_sent'));