Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: skip auth terms confirmation once accepted on same browser (#17256)

YuTengjing1835d9eb6478
diff --git a/src/features/AuthShell/AuthAgreement.test.tsx b/src/features/AuthShell/AuthAgreement.test.tsx
index 13a5e509971..b9d27395755 100644
--- a/src/features/AuthShell/AuthAgreement.test.tsx
+++ b/src/features/AuthShell/AuthAgreement.test.tsx
@@ -27,6 +27,7 @@ vi.mock('react-i18next', async (importOriginal) => ({
afterEach(() => {
vi.restoreAllMocks();
+ localStorage.clear();
});
const expectLinksToOpenInNewTabs = () => {
@@ -159,4 +160,44 @@ describe('useAuthAgreement', () => {
expect(requestConfirmation).not.toHaveBeenCalled();
expect(continueAction).toHaveBeenCalledOnce();
});
+
+ it('should skip confirmation on later visits after consent was given once', () => {
+ const requestConfirmation = vi.fn((onConfirm: () => void) => onConfirm());
+ const continueAction = vi.fn();
+ const firstVisit = renderHook(() => useAuthAgreement(requestConfirmation));
+
+ act(() => {
+ firstVisit.result.current.continueWithAgreement(continueAction);
+ });
+ firstVisit.unmount();
+
+ // A fresh hook instance simulates the user returning to the sign-in page.
+ const secondVisit = renderHook(() => useAuthAgreement(requestConfirmation));
+
+ expect(secondVisit.result.current.agreementChecked).toBe(true);
+
+ act(() => {
+ secondVisit.result.current.continueWithAgreement(continueAction);
+ });
+
+ expect(requestConfirmation).toHaveBeenCalledOnce();
+ expect(continueAction).toHaveBeenCalledTimes(2);
+ });
+
+ it('should forget persisted consent once the agreement is unchecked', () => {
+ const requestConfirmation = vi.fn();
+ const firstVisit = renderHook(() => useAuthAgreement(requestConfirmation));
+
+ act(() => {
+ firstVisit.result.current.setAgreementChecked(true);
+ });
+ act(() => {
+ firstVisit.result.current.setAgreementChecked(false);
+ });
+ firstVisit.unmount();
+
+ const secondVisit = renderHook(() => useAuthAgreement(requestConfirmation));
+
+ expect(secondVisit.result.current.agreementChecked).toBe(false);
+ });
});
diff --git a/src/features/AuthShell/AuthAgreement.tsx b/src/features/AuthShell/AuthAgreement.tsx
index 084a805fc3a..72336b30c6f 100644
--- a/src/features/AuthShell/AuthAgreement.tsx
+++ b/src/features/AuthShell/AuthAgreement.tsx
@@ -8,6 +8,32 @@ import { Trans, useTranslation } from 'react-i18next';
import { PRIVACY_URL, TERMS_URL } from '@/const/url';
+/**
+ * Remembers that the user already accepted the terms & privacy policy on this
+ * browser, so returning users are not asked to confirm again on every sign-in.
+ */
+const AGREEMENT_ACCEPTED_KEY = 'lobehub:auth:agreement-accepted:v1';
+
+const readStoredAgreement = () => {
+ try {
+ return localStorage.getItem(AGREEMENT_ACCEPTED_KEY) === 'true';
+ } catch {
+ return false;
+ }
+};
+
+const persistAgreement = (accepted: boolean) => {
+ try {
+ if (accepted) {
+ localStorage.setItem(AGREEMENT_ACCEPTED_KEY, 'true');
+ } else {
+ localStorage.removeItem(AGREEMENT_ACCEPTED_KEY);
+ }
+ } catch {
+ // Ignore localStorage errors (e.g., quota exceeded, private mode)
+ }
+};
+
const styles = createStaticStyles(({ css, cssVar }) => ({
link: css`
cursor: pointer;
@@ -69,7 +95,12 @@ const AgreementText = memo<AgreementTextProps>(({ i18nKey }) => {
export const useAuthAgreement = (requestConfirmation?: RequestAgreementConfirmation) => {
const { t } = useTranslation(['auth', 'common']);
- const [agreementChecked, setAgreementChecked] = useState(false);
+ const [agreementChecked, setAgreementCheckedState] = useState(readStoredAgreement);
+
+ const setAgreementChecked = useCallback((checked: boolean) => {
+ setAgreementCheckedState(checked);
+ persistAgreement(checked);
+ }, []);
const showConfirmation = useCallback(
(onConfirm: ContinueWithAgreement) => {
@@ -101,7 +132,7 @@ export const useAuthAgreement = (requestConfirmation?: RequestAgreementConfirmat
continueAction();
});
},
- [agreementChecked, showConfirmation],
+ [agreementChecked, setAgreementChecked, showConfirmation],
);
return { agreementChecked, continueWithAgreement, setAgreementChecked };