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 WebSocketService.Instance.register(i.state.registerForm);
163 handleRegisterUsernameChange(i: Login, event: any) {
164 i.state.registerForm.username = event.target.value;
168 handleRegisterEmailChange(i: Login, event: any) {
169 i.state.registerForm.email = event.target.value;
173 handleRegisterPasswordChange(i: Login, event: any) {
174 i.state.registerForm.password = event.target.value;
178 handleRegisterPasswordVerifyChange(i: Login, event: any) {
179 i.state.registerForm.password_verify = event.target.value;
183 parseMessage(msg: any) {
184 let op: UserOperation = msgOp(msg);
187 this.state = this.emptyState;
188 this.setState(this.state);
191 if (op == UserOperation.Register || op == UserOperation.Login) {
192 this.state.loginLoading = false;
193 this.state.registerLoading = false;
194 let res: LoginResponse = msg;
195 UserService.Instance.login(res);
196 this.props.history.push('/');