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 <input type="text" class="no-s-how" value={this.state.spamNada} onInput={linkEvent(this, this.handleSpamNada)} />
133 <div class="form-group row">
134 <div class="col-sm-10">
135 <button type="submit" class="btn btn-secondary">{this.state.registerLoading ?
136 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : 'Sign Up'}</button>
144 handleLoginSubmit(i: Login, event: any) {
145 event.preventDefault();
146 i.state.loginLoading = true;
148 WebSocketService.Instance.login(i.state.loginForm);
151 handleLoginUsernameChange(i: Login, event: any) {
152 i.state.loginForm.username_or_email = event.target.value;
156 handleLoginPasswordChange(i: Login, event: any) {
157 i.state.loginForm.password = event.target.value;
161 handleRegisterSubmit(i: Login, event: any) {
162 event.preventDefault();
163 i.state.registerLoading = true;
165 event.preventDefault();
167 let endTimer = new Date().getTime();
168 let elapsed = endTimer - i.state.registerForm.spam_timer;
170 i.state.registerForm.spam_timer = elapsed;
171 if (elapsed > 1142 && i.state.spamNada == undefined) {
172 WebSocketService.Instance.register(i.state.registerForm);
174 window.location.href = "https://github.com/dessalines/lemmy";
178 handleRegisterUsernameChange(i: Login, event: any) {
179 i.state.registerForm.username = event.target.value;
180 i.state.registerForm.spam_timer = new Date().getTime();
184 handleSpamNada(i: Login, event: any) {
185 i.state.spamNada = event.target.value;
189 handleRegisterEmailChange(i: Login, event: any) {
190 i.state.registerForm.email = event.target.value;
194 handleRegisterPasswordChange(i: Login, event: any) {
195 i.state.registerForm.password = event.target.value;
199 handleRegisterPasswordVerifyChange(i: Login, event: any) {
200 i.state.registerForm.password_verify = event.target.value;
204 parseMessage(msg: any) {
205 let op: UserOperation = msgOp(msg);
208 this.state = this.emptyState;
209 this.setState(this.state);
212 if (op == UserOperation.Login) {
213 this.state.loginLoading = false;
214 this.state.registerLoading = false;
215 let res: LoginResponse = msg;
216 UserService.Instance.login(res);
217 this.props.history.push('/');
218 } else if (op == UserOperation.Register) {
219 this.state.loginLoading = false;
220 this.state.registerLoading = false;
221 let res: LoginResponse = msg;
222 UserService.Instance.login(res);
223 this.props.history.push('/communities');