Commit 630d568 1 parent e439bcf commit 630d568 Copy full SHA for 630d568
File tree 4 files changed +74
-12
lines changed
4 files changed +74
-12
lines changed Original file line number Diff line number Diff line change 1
1
<script lang =" ts" >
2
- import { Col , Container , Row } from " @sveltestrap/sveltestrap" ;
2
+ import {
3
+ Col ,
4
+ Container ,
5
+ Modal ,
6
+ ModalBody ,
7
+ ModalHeader ,
8
+ Row ,
9
+ Spinner ,
10
+ } from " @sveltestrap/sveltestrap" ;
3
11
import SelectTrackedStatiosn from " ./lib/SelectTrackedStations.svelte" ;
4
12
import Stations from " ./lib/Stations.svelte" ;
5
13
import Login from " ./lib/Login.svelte" ;
14
+ import { checkSavedPassword } from " ./lib/auth.svelte" ;
15
+
16
+ let checkingPassword: boolean = $state (true );
17
+ let foundPassword: boolean = $state (false );
18
+ checkSavedPassword ().then ((fp ) => {
19
+ foundPassword = fp ;
20
+ checkingPassword = false ;
21
+ });
6
22
</script >
7
23
8
24
<main >
23
39
></Row
24
40
>
25
41
</Container >
26
- <Login ></Login >
42
+ {#if checkingPassword }
43
+ <Modal isOpen ={checkingPassword } centered ={true }>
44
+ <ModalHeader >Logging In...</ModalHeader >
45
+ <ModalBody >
46
+ <Spinner ></Spinner >
47
+ </ModalBody >
48
+ </Modal >
49
+ {:else if ! foundPassword }
50
+ <Login ></Login >
51
+ {/if }
27
52
</main >
Original file line number Diff line number Diff line change 8
8
ModalFooter ,
9
9
ModalHeader ,
10
10
} from " @sveltestrap/sveltestrap" ;
11
- import { authState } from " ./auth.svelte" ;
11
+ import { authState , setPassword } from " ./auth.svelte" ;
12
12
13
13
let open = $state (true );
14
14
let loggingIn = $state (false );
33
33
.then (async (res ) => {
34
34
if (res .status === 200 ) {
35
35
authState .loggedIn = true ;
36
- authState . password = password ;
36
+ setPassword ( password ) ;
37
37
open = false ;
38
38
} else {
39
39
error = (await res .text ()) || res .statusText ;
46
46
.finally (() => {
47
47
loggingIn = false ;
48
48
});
49
- // authState.loggedIn = true;
50
- // open = false;
51
49
}
52
50
</script >
53
51
72
70
<Button color ="primary" type ="submit" disabled ={loggingIn }
73
71
>Login</Button
74
72
>
75
- <Button color ="secondary" onclick ={() => (open = false )}
76
- >Guest</Button
73
+ <Button
74
+ color =" secondary"
75
+ type =" button"
76
+ onclick ={() => (open = false )}>Guest</Button
77
77
>
78
78
</ModalFooter >
79
79
</form >
Original file line number Diff line number Diff line change 1
1
<script lang =" ts" >
2
2
import { Button , Input , InputGroup } from " @sveltestrap/sveltestrap" ;
3
- import { authState } from " ./auth.svelte" ;
3
+ import { authState , getPassword } from " ./auth.svelte" ;
4
4
5
5
let loading = $state (true );
6
6
$effect (() => {
37
37
method: " POST" ,
38
38
headers: {
39
39
" Content-Type" : " application/x-www-form-urlencoded" ,
40
- Authorization: authState . password ,
40
+ Authorization: getPassword () ?? " " ,
41
41
},
42
42
body: formData .toString (),
43
43
})
Original file line number Diff line number Diff line change 1
- export let authState : { loggedIn : boolean ; password : string } = $state ( {
1
+ export let authState : { loggedIn : boolean } = $state ( {
2
2
loggedIn : false ,
3
- password : "" ,
4
3
} ) ;
4
+
5
+ export function getPassword ( ) : string | null {
6
+ return localStorage . getItem ( "password" ) ;
7
+ }
8
+
9
+ export function setPassword ( password : string ) {
10
+ debugger ;
11
+ localStorage . setItem ( "password" , password ) ;
12
+ }
13
+
14
+ export async function checkSavedPassword ( ) : Promise < boolean > {
15
+ const savedPassword = localStorage . getItem ( "password" ) ;
16
+ if ( savedPassword !== null ) {
17
+ const formData = new URLSearchParams ( {
18
+ password : savedPassword ,
19
+ } ) ;
20
+ return fetch ( import . meta. env . VITE_API_URL + "/login" , {
21
+ method : "POST" ,
22
+ headers : {
23
+ "Content-Type" : "application/x-www-form-urlencoded" ,
24
+ } ,
25
+ body : formData . toString ( ) ,
26
+ } )
27
+ . then ( async ( res ) => {
28
+ if ( res . status === 200 ) {
29
+ authState . loggedIn = true ;
30
+ return true ;
31
+ } else {
32
+ throw res ;
33
+ }
34
+ } )
35
+ . catch ( ( err ) => {
36
+ localStorage . removeItem ( "password" ) ;
37
+ return false ;
38
+ } ) ;
39
+ }
40
+ return false ;
41
+ }
You can’t perform that action at this time.
0 commit comments