-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement password management and loading modal in login flow
- Loading branch information
1 parent
e439bcf
commit 630d568
Showing
4 changed files
with
74 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,41 @@ | ||
export let authState: { loggedIn: boolean; password: string } = $state({ | ||
export let authState: { loggedIn: boolean } = $state({ | ||
loggedIn: false, | ||
password: "", | ||
}); | ||
|
||
export function getPassword(): string | null { | ||
return localStorage.getItem("password"); | ||
} | ||
|
||
export function setPassword(password: string) { | ||
debugger; | ||
localStorage.setItem("password", password); | ||
} | ||
|
||
export async function checkSavedPassword(): Promise<boolean> { | ||
const savedPassword = localStorage.getItem("password"); | ||
if (savedPassword !== null) { | ||
const formData = new URLSearchParams({ | ||
password: savedPassword, | ||
}); | ||
return fetch(import.meta.env.VITE_API_URL + "/login", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}, | ||
body: formData.toString(), | ||
}) | ||
.then(async (res) => { | ||
if (res.status === 200) { | ||
authState.loggedIn = true; | ||
return true; | ||
} else { | ||
throw res; | ||
} | ||
}) | ||
.catch((err) => { | ||
localStorage.removeItem("password"); | ||
return false; | ||
}); | ||
} | ||
return false; | ||
} |