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;
16 export class Login extends Component<any, State> {
17 private subscription: Subscription;
21 username_or_email: undefined,
27 password_verify: undefined,
31 registerLoading: false
34 constructor(props: any, context: any) {
35 super(props, context);
37 this.state = this.emptyState;
39 this.subscription = WebSocketService.Instance.subject
40 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
42 (msg) => this.parseMessage(msg),
43 (err) => console.error(err),
44 () => console.log("complete")
48 componentWillUnmount() {
49 this.subscription.unsubscribe();
53 document.title = "Login - Lemmy";
58 <div class="container">
60 <div class="col-12 col-lg-6 mb-4">
63 <div class="col-12 col-lg-6">
74 <form onSubmit={linkEvent(this, this.handleLoginSubmit)}>
76 <div class="form-group row">
77 <label class="col-sm-2 col-form-label">Email or Username</label>
78 <div class="col-sm-10">
79 <input type="text" class="form-control" value={this.state.loginForm.username_or_email} onInput={linkEvent(this, this.handleLoginUsernameChange)} required minLength={3} />
82 <div class="form-group row">
83 <label class="col-sm-2 col-form-label">Password</label>
84 <div class="col-sm-10">
85 <input type="password" value={this.state.loginForm.password} onInput={linkEvent(this, this.handleLoginPasswordChange)} class="form-control" required />
88 <div class="form-group row">
89 <div class="col-sm-10">
90 <button type="submit" class="btn btn-secondary">{this.state.loginLoading ?
91 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : 'Login'}</button>
95 Forgot your password or deleted your account? Reset your password. TODO
101 <form onSubmit={linkEvent(this, this.handleRegisterSubmit)}>
103 <div class="form-group row">
104 <label class="col-sm-2 col-form-label">Username</label>
105 <div class="col-sm-10">
106 <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_]+" />
109 <div class="form-group row">
110 <label class="col-sm-2 col-form-label">Email</label>
111 <div class="col-sm-10">
112 <input type="email" class="form-control" placeholder="Optional" value={this.state.registerForm.email} onInput={linkEvent(this, this.handleRegisterEmailChange)} minLength={3} />
115 <div class="form-group row">
116 <label class="col-sm-2 col-form-label">Password</label>
117 <div class="col-sm-10">
118 <input type="password" value={this.state.registerForm.password} onInput={linkEvent(this, this.handleRegisterPasswordChange)} class="form-control" required />
121 <div class="form-group row">
122 <label class="col-sm-2 col-form-label">Verify Password</label>
123 <div class="col-sm-10">
124 <input type="password" value={this.state.registerForm.password_verify} onInput={linkEvent(this, this.handleRegisterPasswordVerifyChange)} class="form-control" required />
127 <div class="form-group row">
128 <div class="col-sm-10">
129 <button type="submit" class="btn btn-secondary">{this.state.registerLoading ?
130 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : 'Sign Up'}</button>
138 handleLoginSubmit(i: Login, event: any) {
139 event.preventDefault();
140 i.state.loginLoading = true;
142 WebSocketService.Instance.login(i.state.loginForm);
145 handleLoginUsernameChange(i: Login, event: any) {
146 i.state.loginForm.username_or_email = event.target.value;
150 handleLoginPasswordChange(i: Login, event: any) {
151 i.state.loginForm.password = event.target.value;
155 handleRegisterSubmit(i: Login, event: any) {
156 event.preventDefault();
157 i.state.registerLoading = true;
159 event.preventDefault();
160 setTimeout(function(){
161 WebSocketService.Instance.register(i.state.registerForm);
165 handleRegisterUsernameChange(i: Login, event: any) {
166 i.state.registerForm.username = event.target.value;
170 handleRegisterEmailChange(i: Login, event: any) {
171 i.state.registerForm.email = event.target.value;
175 handleRegisterPasswordChange(i: Login, event: any) {
176 i.state.registerForm.password = event.target.value;
180 handleRegisterPasswordVerifyChange(i: Login, event: any) {
181 i.state.registerForm.password_verify = event.target.value;
185 parseMessage(msg: any) {
186 let op: UserOperation = msgOp(msg);
189 this.state = this.emptyState;
190 this.setState(this.state);
193 if (op == UserOperation.Login) {
194 this.state.loginLoading = false;
195 this.state.registerLoading = false;
196 let res: LoginResponse = msg;
197 UserService.Instance.login(res);
198 this.props.history.push('/');
199 } else if (op == UserOperation.Register) {
200 this.state.loginLoading = false;
201 this.state.registerLoading = false;
202 let res: LoginResponse = msg;
203 UserService.Instance.login(res);
204 this.props.history.push('/communities');