2019-03-21 01:22:31 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-10-19 00:20:27 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
2019-03-23 01:42:57 +00:00
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-10-19 00:20:27 +00:00
|
|
|
import {
|
|
|
|
LoginForm,
|
|
|
|
RegisterForm,
|
|
|
|
LoginResponse,
|
|
|
|
UserOperation,
|
2019-10-30 03:35:39 +00:00
|
|
|
PasswordResetForm,
|
2020-01-03 18:52:21 +00:00
|
|
|
GetSiteResponse,
|
2019-10-19 00:20:27 +00:00
|
|
|
} from '../interfaces';
|
2019-03-23 01:42:57 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2020-01-23 03:29:11 +00:00
|
|
|
import { msgOp, validEmail, toast } from '../utils';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../i18next';
|
|
|
|
import { T } from 'inferno-i18next';
|
2019-03-21 01:22:31 +00:00
|
|
|
|
|
|
|
interface State {
|
|
|
|
loginForm: LoginForm;
|
|
|
|
registerForm: RegisterForm;
|
2019-04-08 21:46:09 +00:00
|
|
|
loginLoading: boolean;
|
|
|
|
registerLoading: boolean;
|
2020-01-03 18:52:21 +00:00
|
|
|
enable_nsfw: boolean;
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Login extends Component<any, State> {
|
2019-03-23 01:42:57 +00:00
|
|
|
private subscription: Subscription;
|
2019-03-21 01:22:31 +00:00
|
|
|
|
2019-04-25 15:51:23 +00:00
|
|
|
emptyState: State = {
|
|
|
|
loginForm: {
|
|
|
|
username_or_email: undefined,
|
2019-10-19 00:20:27 +00:00
|
|
|
password: undefined,
|
2019-04-25 15:51:23 +00:00
|
|
|
},
|
|
|
|
registerForm: {
|
|
|
|
username: undefined,
|
|
|
|
password: undefined,
|
|
|
|
password_verify: undefined,
|
|
|
|
admin: false,
|
2019-08-14 02:52:43 +00:00
|
|
|
show_nsfw: false,
|
2019-04-25 15:51:23 +00:00
|
|
|
},
|
|
|
|
loginLoading: false,
|
2019-04-26 05:50:42 +00:00
|
|
|
registerLoading: false,
|
2020-01-03 18:52:21 +00:00
|
|
|
enable_nsfw: undefined,
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|
2019-04-25 15:51:23 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
constructor(props: any, context: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
super(props, context);
|
|
|
|
|
2019-04-25 15:51:23 +00:00
|
|
|
this.state = this.emptyState;
|
2019-03-21 01:22:31 +00:00
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
this.subscription = WebSocketService.Instance.subject
|
2020-01-23 03:29:11 +00:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
2019-10-19 00:20:27 +00:00
|
|
|
.subscribe(
|
|
|
|
msg => this.parseMessage(msg),
|
|
|
|
err => console.error(err),
|
|
|
|
() => console.log('complete')
|
|
|
|
);
|
2020-01-03 18:52:21 +00:00
|
|
|
|
|
|
|
WebSocketService.Instance.getSite();
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
|
|
|
<div class="row">
|
2019-10-19 00:20:27 +00:00
|
|
|
<div class="col-12 col-lg-6 mb-4">{this.loginForm()}</div>
|
|
|
|
<div class="col-12 col-lg-6">{this.registerForm()}</div>
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loginForm() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
|
2019-04-20 04:06:25 +00:00
|
|
|
<h5>Login</h5>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="form-group row">
|
2019-10-19 00:20:27 +00:00
|
|
|
<label class="col-sm-2 col-form-label">
|
|
|
|
<T i18nKey="email_or_username">#</T>
|
|
|
|
</label>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="col-sm-10">
|
2019-10-19 00:20:27 +00:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
class="form-control"
|
|
|
|
value={this.state.loginForm.username_or_email}
|
|
|
|
onInput={linkEvent(this, this.handleLoginUsernameChange)}
|
|
|
|
required
|
|
|
|
minLength={3}
|
|
|
|
/>
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
2019-10-19 00:20:27 +00:00
|
|
|
<label class="col-sm-2 col-form-label">
|
|
|
|
<T i18nKey="password">#</T>
|
|
|
|
</label>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="col-sm-10">
|
2019-10-19 00:20:27 +00:00
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
value={this.state.loginForm.password}
|
|
|
|
onInput={linkEvent(this, this.handleLoginPasswordChange)}
|
|
|
|
class="form-control"
|
|
|
|
required
|
|
|
|
/>
|
2019-11-02 06:41:57 +00:00
|
|
|
<button
|
|
|
|
disabled={!validEmail(this.state.loginForm.username_or_email)}
|
2019-10-30 03:35:39 +00:00
|
|
|
onClick={linkEvent(this, this.handlePasswordReset)}
|
2019-11-02 06:41:57 +00:00
|
|
|
className="btn p-0 btn-link d-inline-block float-right text-muted small font-weight-bold"
|
2019-10-30 03:35:39 +00:00
|
|
|
>
|
|
|
|
<T i18nKey="forgot_password">#</T>
|
2019-11-02 06:41:57 +00:00
|
|
|
</button>
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-sm-10">
|
2019-10-19 00:20:27 +00:00
|
|
|
<button type="submit" class="btn btn-secondary">
|
|
|
|
{this.state.loginLoading ? (
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
|
|
|
) : (
|
|
|
|
i18n.t('login')
|
|
|
|
)}
|
|
|
|
</button>
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
registerForm() {
|
|
|
|
return (
|
|
|
|
<form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
|
2019-10-19 00:20:27 +00:00
|
|
|
<h5>
|
|
|
|
<T i18nKey="sign_up">#</T>
|
|
|
|
</h5>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="form-group row">
|
2019-10-19 00:20:27 +00:00
|
|
|
<label class="col-sm-2 col-form-label">
|
|
|
|
<T i18nKey="username">#</T>
|
|
|
|
</label>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="col-sm-10">
|
2019-10-19 00:20:27 +00:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
class="form-control"
|
|
|
|
value={this.state.registerForm.username}
|
|
|
|
onInput={linkEvent(this, this.handleRegisterUsernameChange)}
|
|
|
|
required
|
|
|
|
minLength={3}
|
|
|
|
maxLength={20}
|
|
|
|
pattern="[a-zA-Z0-9_]+"
|
|
|
|
/>
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
2019-10-19 00:20:27 +00:00
|
|
|
<label class="col-sm-2 col-form-label">
|
|
|
|
<T i18nKey="email">#</T>
|
|
|
|
</label>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="col-sm-10">
|
2019-10-19 00:20:27 +00:00
|
|
|
<input
|
|
|
|
type="email"
|
|
|
|
class="form-control"
|
|
|
|
placeholder={i18n.t('optional')}
|
|
|
|
value={this.state.registerForm.email}
|
|
|
|
onInput={linkEvent(this, this.handleRegisterEmailChange)}
|
|
|
|
minLength={3}
|
|
|
|
/>
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
2019-10-19 00:20:27 +00:00
|
|
|
<label class="col-sm-2 col-form-label">
|
|
|
|
<T i18nKey="password">#</T>
|
|
|
|
</label>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="col-sm-10">
|
2019-10-19 00:20:27 +00:00
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
value={this.state.registerForm.password}
|
|
|
|
onInput={linkEvent(this, this.handleRegisterPasswordChange)}
|
|
|
|
class="form-control"
|
|
|
|
required
|
|
|
|
/>
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
2019-10-19 00:20:27 +00:00
|
|
|
<label class="col-sm-2 col-form-label">
|
|
|
|
<T i18nKey="verify_password">#</T>
|
|
|
|
</label>
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="col-sm-10">
|
2019-10-19 00:20:27 +00:00
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
value={this.state.registerForm.password_verify}
|
|
|
|
onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)}
|
|
|
|
class="form-control"
|
|
|
|
required
|
|
|
|
/>
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-01-03 18:52:21 +00:00
|
|
|
{this.state.enable_nsfw && (
|
2019-12-11 20:21:47 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<div class="form-check">
|
|
|
|
<input
|
|
|
|
class="form-check-input"
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.registerForm.show_nsfw}
|
|
|
|
onChange={linkEvent(this, this.handleRegisterShowNsfwChange)}
|
|
|
|
/>
|
|
|
|
<label class="form-check-label">
|
|
|
|
<T i18nKey="show_nsfw">#</T>
|
|
|
|
</label>
|
|
|
|
</div>
|
2019-08-14 02:52:43 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-12-11 20:21:47 +00:00
|
|
|
)}
|
2019-03-21 01:22:31 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-sm-10">
|
2019-10-19 00:20:27 +00:00
|
|
|
<button type="submit" class="btn btn-secondary">
|
|
|
|
{this.state.registerLoading ? (
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
|
|
|
) : (
|
|
|
|
i18n.t('sign_up')
|
|
|
|
)}
|
|
|
|
</button>
|
2019-03-21 01:22:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleLoginSubmit(i: Login, event: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
event.preventDefault();
|
2019-04-08 21:46:09 +00:00
|
|
|
i.state.loginLoading = true;
|
|
|
|
i.setState(i.state);
|
2019-03-21 01:22:31 +00:00
|
|
|
WebSocketService.Instance.login(i.state.loginForm);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleLoginUsernameChange(i: Login, event: any) {
|
2019-03-23 01:42:57 +00:00
|
|
|
i.state.loginForm.username_or_email = event.target.value;
|
|
|
|
i.setState(i.state);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleLoginPasswordChange(i: Login, event: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
i.state.loginForm.password = event.target.value;
|
2019-03-23 01:42:57 +00:00
|
|
|
i.setState(i.state);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleRegisterSubmit(i: Login, event: any) {
|
2019-04-16 23:04:23 +00:00
|
|
|
event.preventDefault();
|
2019-04-08 21:46:09 +00:00
|
|
|
i.state.registerLoading = true;
|
|
|
|
i.setState(i.state);
|
2019-04-26 05:50:42 +00:00
|
|
|
|
2019-05-15 16:32:33 +00:00
|
|
|
WebSocketService.Instance.register(i.state.registerForm);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleRegisterUsernameChange(i: Login, event: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
i.state.registerForm.username = event.target.value;
|
2019-04-26 15:31:23 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleRegisterEmailChange(i: Login, event: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
i.state.registerForm.email = event.target.value;
|
2019-12-29 01:25:36 +00:00
|
|
|
if (i.state.registerForm.email == '') {
|
|
|
|
i.state.registerForm.email = undefined;
|
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
i.setState(i.state);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleRegisterPasswordChange(i: Login, event: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
i.state.registerForm.password = event.target.value;
|
2019-03-23 01:42:57 +00:00
|
|
|
i.setState(i.state);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleRegisterPasswordVerifyChange(i: Login, event: any) {
|
2019-03-21 01:22:31 +00:00
|
|
|
i.state.registerForm.password_verify = event.target.value;
|
2019-03-23 01:42:57 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-08-14 02:52:43 +00:00
|
|
|
handleRegisterShowNsfwChange(i: Login, event: any) {
|
|
|
|
i.state.registerForm.show_nsfw = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-10-30 03:35:39 +00:00
|
|
|
handlePasswordReset(i: Login) {
|
2019-11-02 06:41:57 +00:00
|
|
|
event.preventDefault();
|
2019-10-30 03:35:39 +00:00
|
|
|
let resetForm: PasswordResetForm = {
|
|
|
|
email: i.state.loginForm.username_or_email,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.passwordReset(resetForm);
|
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
let op: UserOperation = msgOp(msg);
|
|
|
|
if (msg.error) {
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(i18n.t(msg.error), 'danger');
|
2019-04-25 15:51:23 +00:00
|
|
|
this.state = this.emptyState;
|
2019-04-08 21:46:09 +00:00
|
|
|
this.setState(this.state);
|
2019-03-23 01:42:57 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2019-04-25 18:26:44 +00:00
|
|
|
if (op == UserOperation.Login) {
|
2019-05-02 05:26:31 +00:00
|
|
|
this.state = this.emptyState;
|
|
|
|
this.setState(this.state);
|
2019-03-26 18:00:18 +00:00
|
|
|
let res: LoginResponse = msg;
|
2019-04-08 05:19:02 +00:00
|
|
|
UserService.Instance.login(res);
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(i18n.t('logged_in'));
|
2019-03-23 01:42:57 +00:00
|
|
|
this.props.history.push('/');
|
2019-04-25 18:26:44 +00:00
|
|
|
} else if (op == UserOperation.Register) {
|
2019-05-02 05:26:31 +00:00
|
|
|
this.state = this.emptyState;
|
|
|
|
this.setState(this.state);
|
2019-04-25 18:26:44 +00:00
|
|
|
let res: LoginResponse = msg;
|
|
|
|
UserService.Instance.login(res);
|
|
|
|
this.props.history.push('/communities');
|
2019-10-30 03:35:39 +00:00
|
|
|
} else if (op == UserOperation.PasswordReset) {
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(i18n.t('reset_password_mail_sent'));
|
2020-01-03 18:52:21 +00:00
|
|
|
} else if (op == UserOperation.GetSite) {
|
|
|
|
let res: GetSiteResponse = msg;
|
|
|
|
this.state.enable_nsfw = res.site.enable_nsfw;
|
|
|
|
this.setState(this.state);
|
|
|
|
document.title = `${i18n.t('login')} - ${
|
|
|
|
WebSocketService.Instance.site.name
|
|
|
|
}`;
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
}
|