-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automatic sign-in without interaction
Closes #99
- Loading branch information
1 parent
5b0651f
commit 889322e
Showing
5 changed files
with
54 additions
and
24 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export function navigateToSignIn( | ||
redirectTo: string = "/dashboard", | ||
prompt?: "none" | undefined, | ||
) { | ||
// Build the sign-in URL | ||
const loginEndpoint = `${import.meta.env.VITE_ACCOUNTS_API_URL}/providers/${import.meta.env.VITE_AUTH_PROVIDER}/login`; | ||
const signInURL = new URL(loginEndpoint); | ||
signInURL.searchParams.append( | ||
"redirect_uri", | ||
new URL(redirectTo, window.location.href).toString(), | ||
); | ||
if (prompt) { | ||
signInURL.searchParams.append("prompt", prompt); | ||
} | ||
|
||
// Record the login time (to prevent immediate automatic re-login) | ||
localStorage.setItem("lastLogin", Date.now().toString()); | ||
|
||
// Navigate to the sign-in endpoint | ||
window.location.assign(signInURL.toString()); | ||
} | ||
|
||
export function shouldAutoSignIn() { | ||
// Try to log in without interaction | ||
const lastLogin = localStorage.getItem("lastLogin"); | ||
return ( | ||
lastLogin === null || Date.now() - Number(lastLogin) > 30 * 60 * 1000 // 30 minutes | ||
); | ||
} | ||
|
||
export function navigateToSignOut(redirectTo?: string) { | ||
// Build the sign-out URL | ||
const logoutEndpoint = `${import.meta.env.VITE_ACCOUNTS_API_URL}/logout`; | ||
const signOutURL = new URL(logoutEndpoint); | ||
signOutURL.searchParams.append( | ||
"redirect_uri", | ||
new URL(redirectTo ?? "", window.location.href).toString(), | ||
); | ||
|
||
// Record the login time (to prevent immediate automatic re-login) | ||
localStorage.setItem("lastLogin", Date.now().toString()); | ||
|
||
// Navigate to the sign-out endpoint | ||
window.location.assign(signOutURL.toString()); | ||
} |
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