Skip to content

Commit c32f961

Browse files
authored
Merge pull request #134 from iceljc/features/refine-chat-window
refine user login
2 parents d2c74a4 + 2474854 commit c32f961

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

src/lib/services/auth-service.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import axios from 'axios';
55
/**
66
* @param {string} email
77
* @param {string} password
8-
* @param {function} onSucceed()
8+
* @param {function} onSucceed
9+
* @param {function} onError
910
*/
10-
export async function getToken(email, password, onSucceed) {
11+
export async function getToken(email, password, onSucceed, onError) {
1112
const credentials = btoa(`${email}:${password}`);
1213
const headers = {
1314
Authorization: `Basic ${credentials}`,
@@ -20,8 +21,9 @@ export async function getToken(email, password, onSucceed) {
2021
if (response.ok) {
2122
return response.json();
2223
} else {
23-
alert(response.statusText);
24-
return false
24+
console.log(response.statusText);
25+
onError();
26+
return false;
2527
}
2628
}).then(result => {
2729
if (!result) {
@@ -33,7 +35,9 @@ export async function getToken(email, password, onSucceed) {
3335
userStore.set(user);
3436
onSucceed();
3537
})
36-
.catch(error => alert(error.message));
38+
.catch(() => {
39+
onError();
40+
});
3741
}
3842

3943
/**

src/routes/(authentication)/login/+page.svelte

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
let status = '';
4646
let isSubmitting = false;
4747
let isRememberMe = false;
48+
4849
onMount(() => {
4950
const userName = localStorage.getItem('user_name');
5051
isRememberMe = userName !== null;
@@ -69,19 +70,31 @@
6970
msg = 'Authentication success';
7071
status = 'success';
7172
const redirectUrl = $page.url.searchParams.get('redirect');
73+
isSubmitting = false;
74+
resetStorage();
7275
if (redirectUrl) {
7376
window.location.href = decodeURIComponent(redirectUrl);
7477
} else {
7578
goto('page/dashboard');
7679
}
80+
}, () => {
7781
isSubmitting = false;
78-
resetStorage();
82+
isOpen = true;
83+
status = 'danger';
84+
msg = 'Incorrect user name or password.'
85+
setTimeout(() => {
86+
isOpen = false;
87+
status = '';
88+
msg = '';
89+
}, 3000);
7990
});
8091
isSubmitting = false;
8192
}
8293
8394
function onPasswordToggle() {
8495
var x = document.getElementById('user-password');
96+
if (!x) return;
97+
8598
if (x.type === 'password') {
8699
x.type = 'text';
87100
var icon = document.getElementById('password-eye-icon');
@@ -147,6 +160,7 @@
147160
class="form-control"
148161
id="username"
149162
placeholder="Enter username"
163+
disabled={isSubmitting}
150164
bind:value={username}
151165
/>
152166
</div>
@@ -159,6 +173,7 @@
159173
class="form-control"
160174
id="user-password"
161175
placeholder="Enter password"
176+
disabled={isSubmitting}
162177
aria-label="Password"
163178
aria-describedby="password-addon"
164179
bind:value={password}
@@ -167,6 +182,7 @@
167182
color="light"
168183
type="button"
169184
id="password-addon"
185+
disabled={isSubmitting}
170186
on:click={() => onPasswordToggle()}
171187
>
172188
<i id="password-eye-icon" class="mdi mdi-eye-outline" />
@@ -179,6 +195,7 @@
179195
class="form-check-input"
180196
type="checkbox"
181197
id="remember-check"
198+
disabled={isSubmitting}
182199
bind:checked={isRememberMe}
183200
/>
184201
<Label class="form-check-label" for="remember-check">Remember me</Label>
@@ -200,26 +217,38 @@
200217

201218
<ul class="list-inline">
202219
<li class="list-inline-item">
203-
<a href="{PUBLIC_SERVICE_URL}/sso/GitHub?redirectUrl={PUBLIC_LIVECHAT_HOST}page/user/me" class="social-list-item bg-primary text-white border-primary">
220+
<Link
221+
class="social-list-item bg-primary text-white border-primary"
222+
href="{PUBLIC_SERVICE_URL}/sso/GitHub?redirectUrl={PUBLIC_LIVECHAT_HOST}page/user/me"
223+
disabled={isSubmitting}
224+
>
204225
<i class="mdi mdi-github" />
205-
</a>
226+
</Link>
206227
</li>
207228
<li class="list-inline-item">
208-
<a href="{PUBLIC_SERVICE_URL}/sso/Keycloak?redirectUrl={PUBLIC_LIVECHAT_HOST}page/user/me" class="social-list-item bg-primary text-white border-primary">
229+
<Link
230+
class="social-list-item bg-primary text-white border-primary"
231+
href="{PUBLIC_SERVICE_URL}/sso/Keycloak?redirectUrl={PUBLIC_LIVECHAT_HOST}page/user/me"
232+
disabled={isSubmitting}
233+
>
209234
<i class="mdi mdi-cloud" />
210-
</a>
235+
</Link>
211236
</li>
212237
<li class="list-inline-item">
213-
<a href="{PUBLIC_SERVICE_URL}/sso/Google?redirectUrl={PUBLIC_LIVECHAT_HOST}page/user/me" class="social-list-item bg-danger text-white border-danger">
238+
<Link
239+
class="social-list-item bg-danger text-white border-danger"
240+
href="{PUBLIC_SERVICE_URL}/sso/Google?redirectUrl={PUBLIC_LIVECHAT_HOST}page/user/me"
241+
disabled={isSubmitting}
242+
>
214243
<i class="mdi mdi-google" />
215-
</a>
244+
</Link>
216245
</li>
217246
</ul>
218247
</div>
219248
{/if}
220249
{#if PUBLIC_AUTH_ENABLE_FIND_PWD == 'true' }
221250
<div class="mt-4 text-center">
222-
<Link href="recoverpw" class="text-muted">
251+
<Link href="recoverpw" class="text-muted" disabled={isSubmitting}>
223252
<i class="mdi mdi-lock me-1" /> Forgot your password?
224253
</Link>
225254
</div>
@@ -231,7 +260,7 @@
231260
<div class="mt-5 text-center">
232261
<p hidden={!(PUBLIC_ALLOW_SIGNUP === 'true')}>
233262
Don&apos;t have an account ?
234-
<Link href="register" class="fw-medium text-primary">Signup now</Link>
263+
<Link href="register" class="fw-medium text-primary" disabled={isSubmitting}>Signup now</Link>
235264
</p>
236265
<p>
237266
© {new Date().getFullYear()}

0 commit comments

Comments
 (0)