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';
16 import { T } from 'inferno-i18next';
20 registerForm: RegisterForm;
21 loginLoading: boolean;
22 registerLoading: boolean;
26 export class Login extends Component<any, State> {
27 private subscription: Subscription;
31 username_or_email: undefined,
37 password_verify: undefined,
42 registerLoading: false,
43 enable_nsfw: undefined,
46 constructor(props: any, context: any) {
47 super(props, context);
49 this.state = this.emptyState;
51 this.subscription = WebSocketService.Instance.subject
52 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
54 msg => this.parseMessage(msg),
55 err => console.error(err),
56 () => console.log('complete')
59 WebSocketService.Instance.getSite();
62 componentWillUnmount() {
63 this.subscription.unsubscribe();
68 <div class="container">
70 <div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
71 <div class="col-12 col-lg-6">{this.registerForm()}</div>
80 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
82 <div class="form-group row">
83 <label class="col-sm-2 col-form-label">
84 <T i18nKey="email_or_username">#</T>
86 <div class="col-sm-10">
90 value={this.state.loginForm.username_or_email}
91 onInput={linkEvent(this, this.handleLoginUsernameChange)}
97 <div class="form-group row">
98 <label class="col-sm-2 col-form-label">
99 <T i18nKey="password">#</T>
101 <div class="col-sm-10">
104 value={this.state.loginForm.password}
105 onInput={linkEvent(this, this.handleLoginPasswordChange)}
110 disabled={!validEmail(this.state.loginForm.username_or_email)}
111 onClick={linkEvent(this, this.handlePasswordReset)}
112 className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold"
114 <T i18nKey="forgot_password">#</T>
118 <div class="form-group row">
119 <div class="col-sm-10">
120 <button type="submit" class="btn btn-secondary">
121 {this.state.loginLoading ? (
122 <svg class="icon icon-spinner spin">
123 <use xlinkHref="#icon-spinner"></use>
137 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
139 <T i18nKey="sign_up">#</T>
141 <div class="form-group row">
142 <label class="col-sm-2 col-form-label">
143 <T i18nKey="username">#</T>
145 <div class="col-sm-10">
149 value={this.state.registerForm.username}
150 onInput={linkEvent(this, this.handleRegisterUsernameChange)}
154 pattern="[a-zA-Z0-9_]+"
158 <div class="form-group row">
159 <label class="col-sm-2 col-form-label">
160 <T i18nKey="email">#</T>
162 <div class="col-sm-10">
166 placeholder={i18n.t('optional')}
167 value={this.state.registerForm.email}
168 onInput={linkEvent(this, this.handleRegisterEmailChange)}
173 <div class="form-group row">
174 <label class="col-sm-2 col-form-label">
175 <T i18nKey="password">#</T>
177 <div class="col-sm-10">
180 value={this.state.registerForm.password}
181 onInput={linkEvent(this, this.handleRegisterPasswordChange)}
187 <div class="form-group row">
188 <label class="col-sm-2 col-form-label">
189 <T i18nKey="verify_password">#</T>
191 <div class="col-sm-10">
194 value={this.state.registerForm.password_verify}
195 onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
201 {this.state.enable_nsfw && (
202 <div class="form-group row">
203 <div class="col-sm-10">
204 <div class="form-check">
206 class="form-check-input"
208 checked={this.state.registerForm.show_nsfw}
209 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
211 <label class="form-check-label">
212 <T i18nKey="show_nsfw">#</T>
218 <div class="form-group row">
219 <div class="col-sm-10">
220 <button type="submit" class="btn btn-secondary">
221 {this.state.registerLoading ? (
222 <svg class="icon icon-spinner spin">
223 <use xlinkHref="#icon-spinner"></use>
235 handleLoginSubmit(i: Login, event: any) {
236 event.preventDefault();
237 i.state.loginLoading = true;
239 WebSocketService.Instance.login(i.state.loginForm);
242 handleLoginUsernameChange(i: Login, event: any) {
243 i.state.loginForm.username_or_email = event.target.value;
247 handleLoginPasswordChange(i: Login, event: any) {
248 i.state.loginForm.password = event.target.value;
252 handleRegisterSubmit(i: Login, event: any) {
253 event.preventDefault();
254 i.state.registerLoading = true;
257 WebSocketService.Instance.register(i.state.registerForm);
260 handleRegisterUsernameChange(i: Login, event: any) {
261 i.state.registerForm.username = event.target.value;
265 handleRegisterEmailChange(i: Login, event: any) {
266 i.state.registerForm.email = event.target.value;
267 if (i.state.registerForm.email == '') {
268 i.state.registerForm.email = undefined;
273 handleRegisterPasswordChange(i: Login, event: any) {
274 i.state.registerForm.password = event.target.value;
278 handleRegisterPasswordVerifyChange(i: Login, event: any) {
279 i.state.registerForm.password_verify = event.target.value;
283 handleRegisterShowNsfwChange(i: Login, event: any) {
284 i.state.registerForm.show_nsfw = event.target.checked;
288 handlePasswordReset(i: Login) {
289 event.preventDefault();
290 let resetForm: PasswordResetForm = {
291 email: i.state.loginForm.username_or_email,
293 WebSocketService.Instance.passwordReset(resetForm);
296 parseMessage(msg: WebSocketJsonResponse) {
297 let res = wsJsonToRes(msg);
299 toast(i18n.t(msg.error), 'danger');
300 this.state = this.emptyState;
301 this.setState(this.state);
304 if (res.op == UserOperation.Login) {
305 let data = res.data as LoginResponse;
306 this.state = this.emptyState;
307 this.setState(this.state);
308 UserService.Instance.login(data);
309 toast(i18n.t('logged_in'));
310 this.props.history.push('/');
311 } else if (res.op == UserOperation.Register) {
312 let data = res.data as LoginResponse;
313 this.state = this.emptyState;
314 this.setState(this.state);
315 UserService.Instance.login(data);
316 this.props.history.push('/communities');
317 } else if (res.op == UserOperation.PasswordReset) {
318 toast(i18n.t('reset_password_mail_sent'));
319 } else if (res.op == UserOperation.GetSite) {
320 let data = res.data as GetSiteResponse;
321 this.state.enable_nsfw = data.site.enable_nsfw;
322 this.setState(this.state);
323 document.title = `${i18n.t('login')} - ${
324 WebSocketService.Instance.site.name