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 } 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
60 msg => this.parseMessage(msg),
61 err => console.error(err),
62 () => console.log('complete')
65 WebSocketService.Instance.getSite();
68 componentWillUnmount() {
69 this.subscription.unsubscribe();
74 <div class="container">
76 <div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
77 <div class="col-12 col-lg-6">{this.registerForm()}</div>
86 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
88 <div class="form-group row">
89 <label class="col-sm-2 col-form-label">
90 <T i18nKey="email_or_username">#</T>
92 <div class="col-sm-10">
96 value={this.state.loginForm.username_or_email}
97 onInput={linkEvent(this, this.handleLoginUsernameChange)}
103 <div class="form-group row">
104 <label class="col-sm-2 col-form-label">
105 <T i18nKey="password">#</T>
107 <div class="col-sm-10">
110 value={this.state.loginForm.password}
111 onInput={linkEvent(this, this.handleLoginPasswordChange)}
116 disabled={!validEmail(this.state.loginForm.username_or_email)}
117 onClick={linkEvent(this, this.handlePasswordReset)}
118 className="btn p-0 btn-link 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 {this.state.enable_nsfw && (
208 <div class="form-group row">
209 <div class="col-sm-10">
210 <div class="form-check">
212 class="form-check-input"
214 checked={this.state.registerForm.show_nsfw}
215 onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
217 <label class="form-check-label">
218 <T i18nKey="show_nsfw">#</T>
224 <div class="form-group row">
225 <div class="col-sm-10">
226 <button type="submit" class="btn btn-secondary">
227 {this.state.registerLoading ? (
228 <svg class="icon icon-spinner spin">
229 <use xlinkHref="#icon-spinner"></use>
241 handleLoginSubmit(i: Login, event: any) {
242 event.preventDefault();
243 i.state.loginLoading = true;
245 WebSocketService.Instance.login(i.state.loginForm);
248 handleLoginUsernameChange(i: Login, event: any) {
249 i.state.loginForm.username_or_email = event.target.value;
253 handleLoginPasswordChange(i: Login, event: any) {
254 i.state.loginForm.password = event.target.value;
258 handleRegisterSubmit(i: Login, event: any) {
259 event.preventDefault();
260 i.state.registerLoading = true;
263 WebSocketService.Instance.register(i.state.registerForm);
266 handleRegisterUsernameChange(i: Login, event: any) {
267 i.state.registerForm.username = event.target.value;
271 handleRegisterEmailChange(i: Login, event: any) {
272 i.state.registerForm.email = event.target.value;
273 if (i.state.registerForm.email == '') {
274 i.state.registerForm.email = undefined;
279 handleRegisterPasswordChange(i: Login, event: any) {
280 i.state.registerForm.password = event.target.value;
284 handleRegisterPasswordVerifyChange(i: Login, event: any) {
285 i.state.registerForm.password_verify = event.target.value;
289 handleRegisterShowNsfwChange(i: Login, event: any) {
290 i.state.registerForm.show_nsfw = event.target.checked;
294 handlePasswordReset(i: Login) {
295 event.preventDefault();
296 let resetForm: PasswordResetForm = {
297 email: i.state.loginForm.username_or_email,
299 WebSocketService.Instance.passwordReset(resetForm);
302 parseMessage(msg: any) {
303 let op: UserOperation = msgOp(msg);
305 alert(i18n.t(msg.error));
306 this.state = this.emptyState;
307 this.setState(this.state);
310 if (op == UserOperation.Login) {
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('/');
316 } else if (op == UserOperation.Register) {
317 this.state = this.emptyState;
318 this.setState(this.state);
319 let res: LoginResponse = msg;
320 UserService.Instance.login(res);
321 this.props.history.push('/communities');
322 } else if (op == UserOperation.PasswordReset) {
323 alert(i18n.t('reset_password_mail_sent'));
324 } else if (op == UserOperation.GetSite) {
325 let res: GetSiteResponse = msg;
326 this.state.enable_nsfw = res.site.enable_nsfw;
327 this.setState(this.state);
328 document.title = `${i18n.t('login')} - ${
329 WebSocketService.Instance.site.name