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,
32 registerLoading: false,
33 spamTimer: new Date().getTime()
36 constructor(props: any, context: any) {
37 super(props, context);
39 this.state = this.emptyState;
41 this.subscription = WebSocketService.Instance.subject
42 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
44 (msg) => this.parseMessage(msg),
45 (err) => console.error(err),
46 () => console.log("complete")
50 componentWillUnmount() {
51 this.subscription.unsubscribe();
55 document.title = "Login - Lemmy";
60 <div class="container">
62 <div class="col-12 col-lg-6 mb-4">
65 <div class="col-12 col-lg-6">
76 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
78 <div class="form-group row">
79 <label class="col-sm-2 col-form-label">Email or Username</label>
80 <div class="col-sm-10">
81 <input type="text" class="form-control" value={this.state.loginForm.username_or_email} onInput={linkEvent(this, this.handleLoginUsernameChange)} required minLength={3} />
84 <div class="form-group row">
85 <label class="col-sm-2 col-form-label">Password</label>
86 <div class="col-sm-10">
87 <input type="password" value={this.state.loginForm.password} onInput={linkEvent(this, this.handleLoginPasswordChange)} class="form-control" required />
90 <div class="form-group row">
91 <div class="col-sm-10">
92 <button type="submit" class="btn btn-secondary">{this.state.loginLoading ?
93 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : 'Login'}</button>
97 Forgot your password or deleted your account? Reset your password. TODO
103 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
105 <div class="form-group row">
106 <label class="col-sm-2 col-form-label">Username</label>
107 <div class="col-sm-10">
108 <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_]+" />
111 <div class="form-group row">
112 <label class="col-sm-2 col-form-label">Email</label>
113 <div class="col-sm-10">
114 <input type="email" class="form-control" placeholder="Optional" value={this.state.registerForm.email} onInput={linkEvent(this, this.handleRegisterEmailChange)} minLength={3} />
117 <div class="form-group row">
118 <label class="col-sm-2 col-form-label">Password</label>
119 <div class="col-sm-10">
120 <input type="password" value={this.state.registerForm.password} onInput={linkEvent(this, this.handleRegisterPasswordChange)} class="form-control" required />
123 <div class="form-group row">
124 <label class="col-sm-2 col-form-label">Verify Password</label>
125 <div class="col-sm-10">
126 <input type="password" value={this.state.registerForm.password_verify} onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)} class="form-control" required />
129 <input type="hidden" value={this.state.spamTimer} />
130 <div class="form-group row">
131 <div class="col-sm-10">
132 <button type="submit" class="btn btn-secondary">{this.state.registerLoading ?
133 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : 'Sign Up'}</button>
141 handleLoginSubmit(i: Login, event: any) {
142 event.preventDefault();
143 i.state.loginLoading = true;
145 WebSocketService.Instance.login(i.state.loginForm);
148 handleLoginUsernameChange(i: Login, event: any) {
149 i.state.loginForm.username_or_email = event.target.value;
153 handleLoginPasswordChange(i: Login, event: any) {
154 i.state.loginForm.password = event.target.value;
158 handleRegisterSubmit(i: Login, event: any) {
159 event.preventDefault();
160 i.state.registerLoading = true;
162 event.preventDefault();
164 let endTimer = new Date().getTime();
165 let elapsed = endTimer - i.state.spamTimer;
166 if (elapsed > 4500) {
167 WebSocketService.Instance.register(i.state.registerForm);
169 location.reload(true);
173 handleRegisterUsernameChange(i: Login, event: any) {
174 i.state.registerForm.username = event.target.value;
178 handleRegisterEmailChange(i: Login, event: any) {
179 i.state.registerForm.email = event.target.value;
183 handleRegisterPasswordChange(i: Login, event: any) {
184 i.state.registerForm.password = event.target.value;
188 handleRegisterPasswordVerifyChange(i: Login, event: any) {
189 i.state.registerForm.password_verify = event.target.value;
193 parseMessage(msg: any) {
194 let op: UserOperation = msgOp(msg);
197 this.state = this.emptyState;
198 this.setState(this.state);
201 if (op == UserOperation.Login) {
202 this.state.loginLoading = false;
203 this.state.registerLoading = false;
204 let res: LoginResponse = msg;
205 UserService.Instance.login(res);
206 this.props.history.push('/');
207 } else if (op == UserOperation.Register) {
208 this.state.loginLoading = false;
209 this.state.registerLoading = false;
210 let res: LoginResponse = msg;
211 UserService.Instance.login(res);
212 this.props.history.push('/communities');