1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from "rxjs";
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import { LoginForm, RegisterForm, LoginResponse, UserOperation } from '../interfaces';
5 import { WebSocketService, UserService } from '../services';
6 import { msgOp } from '../utils';
10 registerForm: RegisterForm;
11 loginLoading: boolean;
12 registerLoading: boolean;
17 export class Login extends Component<any, State> {
18 private subscription: Subscription;
22 username_or_email: undefined,
28 password_verify: undefined,
30 spam_timer: undefined,
33 registerLoading: false,
37 constructor(props: any, context: any) {
38 super(props, context);
40 this.state = this.emptyState;
42 this.subscription = WebSocketService.Instance.subject
43 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
45 (msg) => this.parseMessage(msg),
46 (err) => console.error(err),
47 () => console.log("complete")
51 componentWillUnmount() {
52 this.subscription.unsubscribe();
56 document.title = "Login - Lemmy";
61 <div class="container">
63 <div class="col-12 col-lg-6 mb-4">
66 <div class="col-12 col-lg-6">
77 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
79 <div class="form-group row">
80 <label class="col-sm-2 col-form-label">Email or Username</label>
81 <div class="col-sm-10">
82 <input type="text" class="form-control" value={this.state.loginForm.username_or_email} onInput={linkEvent(this, this.handleLoginUsernameChange)} required minLength={3} />
85 <div class="form-group row">
86 <label class="col-sm-2 col-form-label">Password</label>
87 <div class="col-sm-10">
88 <input type="password" value={this.state.loginForm.password} onInput={linkEvent(this, this.handleLoginPasswordChange)} class="form-control" required />
91 <div class="form-group row">
92 <div class="col-sm-10">
93 <button type="submit" class="btn btn-secondary">{this.state.loginLoading ?
94 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : 'Login'}</button>
98 Forgot your password or deleted your account? Reset your password. TODO
104 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
106 <div class="form-group row">
107 <label class="col-sm-2 col-form-label">Username</label>
108 <div class="col-sm-10">
109 <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_]+" />
112 <div class="form-group row">
113 <label class="col-sm-2 col-form-label">Email</label>
114 <div class="col-sm-10">
115 <input type="email" class="form-control" placeholder="Optional" value={this.state.registerForm.email} onInput={linkEvent(this, this.handleRegisterEmailChange)} minLength={3} />
118 <div class="form-group row">
119 <label class="col-sm-2 col-form-label">Password</label>
120 <div class="col-sm-10">
121 <input type="password" value={this.state.registerForm.password} onInput={linkEvent(this, this.handleRegisterPasswordChange)} class="form-control" required />
124 <div class="form-group row">
125 <label class="col-sm-2 col-form-label">Verify Password</label>
126 <div class="col-sm-10">
127 <input type="password" value={this.state.registerForm.password_verify} onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)} class="form-control" required />
130 <input type="hidden" value={this.state.registerForm.spam_timer} />
131 <input type="text" class="d-none" value={this.state.spamNada} onInput={linkEvent(this, this.handleSpamNada)} />
132 <div class="form-group row">
133 <div class="col-sm-10">
134 <button type="submit" class="btn btn-secondary">{this.state.registerLoading ?
135 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : 'Sign Up'}</button>
143 handleLoginSubmit(i: Login, event: any) {
144 event.preventDefault();
145 i.state.loginLoading = true;
147 WebSocketService.Instance.login(i.state.loginForm);
150 handleLoginUsernameChange(i: Login, event: any) {
151 i.state.loginForm.username_or_email = event.target.value;
155 handleLoginPasswordChange(i: Login, event: any) {
156 i.state.loginForm.password = event.target.value;
160 handleRegisterSubmit(i: Login, event: any) {
161 event.preventDefault();
162 i.state.registerLoading = true;
164 event.preventDefault();
166 let endTimer = new Date().getTime();
167 let elapsed = endTimer - i.state.registerForm.spam_timer;
169 i.state.registerForm.spam_timer = elapsed;
170 if (elapsed > 1142 && i.state.spamNada == undefined) {
171 WebSocketService.Instance.register(i.state.registerForm);
173 window.location.href = "https://github.com/dessalines/lemmy";
177 handleRegisterUsernameChange(i: Login, event: any) {
178 i.state.registerForm.username = event.target.value;
179 i.state.registerForm.spam_timer = new Date().getTime();
183 handleSpamNada(i: Login, event: any) {
184 i.state.spamNada = event.target.value;
188 handleRegisterEmailChange(i: Login, event: any) {
189 i.state.registerForm.email = event.target.value;
193 handleRegisterPasswordChange(i: Login, event: any) {
194 i.state.registerForm.password = event.target.value;
198 handleRegisterPasswordVerifyChange(i: Login, event: any) {
199 i.state.registerForm.password_verify = event.target.value;
203 parseMessage(msg: any) {
204 let op: UserOperation = msgOp(msg);
207 this.state = this.emptyState;
208 this.setState(this.state);
211 if (op == UserOperation.Login) {
212 this.state.loginLoading = false;
213 this.state.registerLoading = false;
214 let res: LoginResponse = msg;
215 UserService.Instance.login(res);
216 this.props.history.push('/');
217 } else if (op == UserOperation.Register) {
218 this.state.loginLoading = false;
219 this.state.registerLoading = false;
220 let res: LoginResponse = msg;
221 UserService.Instance.login(res);
222 this.props.history.push('/communities');