-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlogin.gts
234 lines (216 loc) · 6.24 KB
/
login.gts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { fn } from '@ember/helper';
import { on } from '@ember/modifier';
import { action } from '@ember/object';
import type RouterService from '@ember/routing/router-service';
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { restartableTask } from 'ember-concurrency';
import { type LoginResponse } from 'matrix-js-sdk';
import moment from 'moment';
import {
Button,
FieldContainer,
BoxelInput,
} from '@cardstack/boxel-ui/components';
import {
isMatrixError,
type MatrixError,
} from '@cardstack/host/lib/matrix-utils';
import type MatrixService from '@cardstack/host/services/matrix-service';
import { AuthMode } from './auth';
interface Signature {
Args: {
setMode: (mode: AuthMode) => void;
};
}
export default class Login extends Component<Signature> {
<template>
<span class='title'>Sign in to your Boxel Account</span>
<FieldContainer
@label='Email Address or Username'
@tag='label'
@vertical={{true}}
class='field'
>
<BoxelInput
data-test-username-field
type='text'
@value={{this.username}}
@onInput={{this.setUsername}}
@onKeyPress={{this.handleEnter}}
/>
</FieldContainer>
<FieldContainer
@label='Password'
@tag='label'
@vertical={{true}}
class='field'
>
<BoxelInput
data-test-password-field
type='password'
@value={{this.password}}
@onInput={{this.setPassword}}
@onKeyPress={{this.handleEnter}}
/>
</FieldContainer>
<Button
@kind='text-only'
class='forgot-password'
data-test-forgot-password
{{on 'click' (fn @setMode 'forgot-password')}}
>Forgot password?</Button>
<Button
class='button'
data-test-login-btn
@kind='primary'
@disabled={{this.isLoginButtonDisabled}}
@loading={{this.doLogin.isRunning}}
{{on 'click' this.login}}
>
Sign in</Button>
{{#if this.error}}
<div class='error' data-test-login-error>{{this.error}}</div>
{{/if}}
<span class='or'>or</span>
<Button
class='button'
data-test-register-user
{{on 'click' (fn @setMode 'register')}}
>Create a new Boxel account</Button>
<style scoped>
form {
display: flex;
flex-direction: column;
}
.title {
font: 600 var(--boxel-font-med);
margin-bottom: var(--boxel-sp-sm);
padding: 0;
}
.field {
margin-top: var(--boxel-sp);
}
.field :deep(input:autofill) {
transition:
background-color 0s 600000s,
color 0s 600000s;
}
.forgot-password {
border: none;
padding: 0;
margin-bottom: var(--boxel-sp-lg);
margin-left: auto;
color: var(--boxel-dark);
font: 500 var(--boxel-font-xs);
}
.forgot-password:hover {
color: var(--boxel-highlight);
background-color: transparent;
}
.button {
--boxel-button-padding: var(--boxel-sp-sm);
width: 100%;
}
.button :deep(.boxel-loading-indicator) {
display: flex;
justify-content: center;
align-items: center;
}
.or {
margin: var(--boxel-sp-sm) auto;
font: 500 var(--boxel-font-sm);
}
.error {
color: var(--boxel-error-100);
padding: 0;
font: 500 var(--boxel-font-xs);
margin: var(--boxel-sp-xxs) auto 0 auto;
}
</style>
</template>
@tracked private error: string | undefined;
@tracked private username: string | undefined;
@tracked private password: string | undefined;
@service private declare matrixService: MatrixService;
@service declare router: RouterService;
private get isLoginButtonDisabled() {
return (
!this.username || !this.password || this.error || this.doLogin.isRunning
);
}
@action handleEnter(event: KeyboardEvent) {
if (event.key === 'Enter') {
!this.isLoginButtonDisabled && this.login();
}
}
@action
private setUsername(username: string) {
this.username = username;
this.error = undefined;
}
@action
private setPassword(password: string) {
this.password = password;
this.error = undefined;
}
@action
private login() {
this.doLogin.perform();
}
private doLogin = restartableTask(async () => {
if (!this.username) {
throw new Error(
`bug: should never get here: login button disabled when no username`,
);
} else if (!this.password) {
throw new Error(
`bug: should never get here: login button disabled when no password`,
);
}
let auth: LoginResponse;
try {
auth = await this.matrixService.login(this.username, this.password);
} catch (e: any) {
if (isMatrixError(e)) {
this.error = `Sign in failed. ${extractMatrixErrorMessage(e)}`;
} else {
this.error = `Sign in failed: ${e.message}`;
}
throw e;
}
if (auth) {
// note that any commands after this await will not be executed as the act
// of starting the matrix service sets tracked properties that result in this
// component being removed from the DOM and destroyed. Keep in mind that in EC tasks,
// awaits are really just syntactic sugar for yields, and that we yield to
// this.matrixService.start()
await this.matrixService.start({ auth, refreshRoutes: true });
} else {
throw new Error(
`bug: should be impossible to get here - successful matrix login with no auth response`,
);
}
});
}
export function extractMatrixErrorMessage(e: MatrixError) {
if (e.httpStatus === 403) {
return 'Please check your credentials and try again.';
} else if (e.httpStatus === 429) {
if (e.data.retry_after_ms) {
moment.relativeTimeRounding(Math.ceil);
return `Too many failed attempts, try again ${moment
.duration(e.data.retry_after_ms)
.humanize(true)}.`;
}
return 'Too many failed attempts, try again later.';
} else {
return `Unknown error ${e.httpStatus}: ${e.data.error}`;
}
}
declare module '@glint/environment-ember-loose/registry' {
export default interface Login {
'Matrix::Login': typeof Login;
}
}