Skip to content

Commit

Permalink
feat: automatic sign-in without interaction
Browse files Browse the repository at this point in the history
Closes #99
  • Loading branch information
ArtemSBulgakov committed Oct 3, 2024
1 parent 5b0651f commit 889322e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
19 changes: 0 additions & 19 deletions src/api/accounts/paths.ts

This file was deleted.

45 changes: 45 additions & 0 deletions src/api/accounts/sign-in.ts
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());
}
4 changes: 4 additions & 0 deletions src/api/helpers/AuthManager.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { $accounts, accountsTypes } from "@/api/accounts";
import { navigateToSignIn, shouldAutoSignIn } from "@/api/accounts/sign-in.ts";
import {
invalidateMyAccessToken,
useMyAccessToken,
Expand Down Expand Up @@ -27,6 +28,9 @@ export function AuthManager({ children }: PropsWithChildren) {
setStoredMe(me ?? null);
if (!me) {
invalidateMyAccessToken();
if (shouldAutoSignIn()) {
navigateToSignIn(window.location.href, "none");
}
}
}
}, [me, isPending, setStoredMe]);
Expand Down
6 changes: 3 additions & 3 deletions src/components/common/SignInButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSignInUrl } from "@/api/accounts/paths.ts";
import { navigateToSignIn } from "@/api/accounts/sign-in.ts";
import clsx from "clsx";
import { forwardRef } from "react";

Expand All @@ -21,7 +21,7 @@ export const SignInButton = forwardRef(function SignInButton_(
className,
)}
onClick={(e) => {
window.location.assign(getSignInUrl(signInRedirect));
navigateToSignIn(signInRedirect);
onClick?.(e);
}}
>
Expand All @@ -44,7 +44,7 @@ export const SignInButtonIcon = forwardRef(function SignInButtonIcon(
className,
)}
onClick={(e) => {
window.location.assign(getSignInUrl(signInRedirect));
navigateToSignIn(signInRedirect);
onClick?.(e);
}}
>
Expand Down
4 changes: 2 additions & 2 deletions src/components/layout/UserMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSignOutUrl } from "@/api/accounts/paths.ts";
import { navigateToSignOut } from "@/api/accounts/sign-in.ts";
import { useMe } from "@/api/accounts/user.ts";
import { SignInButtonIcon } from "@/components/common/SignInButton";
import { SidebarContext } from "@/components/layout/Sidebar";
Expand Down Expand Up @@ -120,7 +120,7 @@ function UserMenu({ isMobile, isSidebar }: UserMenuProps) {
</Link>
<button
onClick={() => {
window.location.assign(getSignOutUrl());
navigateToSignOut();
setIsOpen(false);
setSidebarOpened(false);
}}
Expand Down

0 comments on commit 889322e

Please sign in to comment.