Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

fix(client): replaced better-auth useSession with custom integration (#859)

Corentin Thomassetafcfcf75cb69
diff --git a/.changeset/heavy-bears-study.md b/.changeset/heavy-bears-study.md
new file mode 100644
index 00000000..f9c46736
--- /dev/null
+++ b/.changeset/heavy-bears-study.md
@@ -0,0 +1,5 @@
+---
+"@papra/app": patch
+---
+
+Fixed a race condition that could incorrectly show the "Email verified" page after successful login, even when email verification had not been completed or was not required.
\ No newline at end of file
diff --git a/apps/papra-client/src/modules/auth/auth.demo.services.ts b/apps/papra-client/src/modules/auth/auth.demo.services.ts
index 4de0e529..bac9cc9b 100644
--- a/apps/papra-client/src/modules/auth/auth.demo.services.ts
+++ b/apps/papra-client/src/modules/auth/auth.demo.services.ts
@@ -2,8 +2,7 @@ import type { createAuthClient } from './auth.services';
export function createDemoAuthClient() {
const baseClient = {
- useSession: () => () => ({
- isPending: false,
+ getSession: () => ({
data: {
user: {
id: '1',
diff --git a/apps/papra-client/src/modules/auth/auth.services.ts b/apps/papra-client/src/modules/auth/auth.services.ts
index c9a788fa..fd10c6fa 100644
--- a/apps/papra-client/src/modules/auth/auth.services.ts
+++ b/apps/papra-client/src/modules/auth/auth.services.ts
@@ -24,8 +24,8 @@ export function createAuthClient() {
requestPasswordReset: client.requestPasswordReset,
resetPassword: client.resetPassword,
sendVerificationEmail: client.sendVerificationEmail,
- useSession: client.useSession,
twoFactor: client.twoFactor,
+ getSession: client.getSession,
signOut: async () => {
trackingServices.capture({ event: 'User logged out' });
const result = await client.signOut();
@@ -39,7 +39,7 @@ export function createAuthClient() {
}
export const {
- useSession,
+ getSession,
signIn,
signUp,
signOut,
diff --git a/apps/papra-client/src/modules/auth/composables/use-session.composable.ts b/apps/papra-client/src/modules/auth/composables/use-session.composable.ts
new file mode 100644
index 00000000..7865137e
--- /dev/null
+++ b/apps/papra-client/src/modules/auth/composables/use-session.composable.ts
@@ -0,0 +1,18 @@
+import { useQuery } from '@tanstack/solid-query';
+import { getSession } from '../auth.services';
+
+export function useSession() {
+ const sessionQuery = useQuery(() => ({
+ queryKey: ['auth', 'session'],
+ queryFn: () => getSession(),
+ }));
+
+ const getUser = () => sessionQuery.data?.data?.user;
+ const getIsAuthenticated = () => Boolean(getUser());
+
+ return {
+ sessionQuery,
+ getUser,
+ getIsAuthenticated,
+ };
+}
diff --git a/apps/papra-client/src/modules/auth/middleware/protected-page.middleware.tsx b/apps/papra-client/src/modules/auth/middleware/protected-page.middleware.tsx
index 5d12dc77..20896c15 100644
--- a/apps/papra-client/src/modules/auth/middleware/protected-page.middleware.tsx
+++ b/apps/papra-client/src/modules/auth/middleware/protected-page.middleware.tsx
@@ -1,26 +1,19 @@
-import type { Component } from 'solid-js';
+import type { ParentComponent } from 'solid-js';
import { Navigate } from '@solidjs/router';
-import { Match, Suspense, Switch } from 'solid-js';
-import { Dynamic } from 'solid-js/web';
-import { useSession } from '../auth.services';
+import { Match, Switch } from 'solid-js';
+import { useSession } from '../composables/use-session.composable';
-export function createProtectedPage({ authType, component }: { authType: 'public' | 'private' | 'public-only' | 'admin'; component: Component }) {
- return () => {
- const session = useSession();
+export const PublicOnlyPage: ParentComponent = (props) => {
+ const { getIsAuthenticated, sessionQuery } = useSession();
- const getIsAuthenticated = () => Boolean(session()?.data?.user);
-
- return (
- <Suspense>
- <Switch fallback={<Dynamic component={component} />}>
- <Match when={authType === 'private' && !getIsAuthenticated()}>
- <Navigate href="/login" />
- </Match>
- <Match when={authType === 'public-only' && getIsAuthenticated()}>
- <Navigate href="/" />
- </Match>
- </Switch>
- </Suspense>
- );
- };
-}
+ return (
+ <Switch>
+ <Match when={!sessionQuery.isLoading && getIsAuthenticated()}>
+ <Navigate href="/" />
+ </Match>
+ <Match when={!sessionQuery.isLoading && !getIsAuthenticated()}>
+ {props.children}
+ </Match>
+ </Switch>
+ );
+};
diff --git a/apps/papra-client/src/modules/tracking/components/identify-user.component.tsx b/apps/papra-client/src/modules/tracking/components/identify-user.component.tsx
index ee4d8f35..5581dc26 100644
--- a/apps/papra-client/src/modules/tracking/components/identify-user.component.tsx
+++ b/apps/papra-client/src/modules/tracking/components/identify-user.component.tsx
@@ -1,6 +1,6 @@
import type { Component } from 'solid-js';
import { createEffect } from 'solid-js';
-import { useSession } from '@/modules/auth/auth.services';
+import { useSession } from '@/modules/auth/composables/use-session.composable';
import { isDemoMode } from '@/modules/config/config';
import { trackingServices } from '../tracking.services';
@@ -9,10 +9,10 @@ export const IdentifyUser: Component = () => {
return null;
}
- const session = useSession();
+ const { getUser } = useSession();
createEffect(() => {
- const user = session()?.data?.user;
+ const user = getUser();
if (user) {
trackingServices.identify({
diff --git a/apps/papra-client/src/routes.tsx b/apps/papra-client/src/routes.tsx
index ec9b0337..5a76f4d0 100644
--- a/apps/papra-client/src/routes.tsx
+++ b/apps/papra-client/src/routes.tsx
@@ -6,7 +6,7 @@ import { adminRoutes } from './modules/admin/admin.routes';
import { ApiKeysPage } from './modules/api-keys/pages/api-keys.page';
import { CreateApiKeyPage } from './modules/api-keys/pages/create-api-key.page';
import { authPagesPaths } from './modules/auth/auth.constants';
-import { createProtectedPage } from './modules/auth/middleware/protected-page.middleware';
+import { PublicOnlyPage } from './modules/auth/middleware/protected-page.middleware';
import { EmailValidationRequiredPage } from './modules/auth/pages/email-validation-required.page';
import { EmailVerificationPage } from './modules/auth/pages/email-verification.page';
import { LoginPage } from './modules/auth/pages/login.page';
@@ -230,27 +230,27 @@ export const routes: RouteDefinition[] = [
},
{
path: '/login',
- component: createProtectedPage({ authType: 'public-only', component: LoginPage }),
+ component: () => <PublicOnlyPage children={<LoginPage />} />,
},
{
path: '/register',
- component: createProtectedPage({ authType: 'public-only', component: RegisterPage }),
+ component: () => <PublicOnlyPage children={<RegisterPage />} />,
},
{
path: '/reset-password',
- component: createProtectedPage({ authType: 'public-only', component: ResetPasswordPage }),
+ component: () => <PublicOnlyPage children={<ResetPasswordPage />} />,
},
{
path: '/request-password-reset',
- component: createProtectedPage({ authType: 'public-only', component: RequestPasswordResetPage }),
+ component: () => <PublicOnlyPage children={<RequestPasswordResetPage />} />,
},
{
path: '/email-validation-required',
- component: createProtectedPage({ authType: 'public-only', component: EmailValidationRequiredPage }),
+ component: () => <PublicOnlyPage children={<EmailValidationRequiredPage />} />,
},
{
path: authPagesPaths.emailVerification,
- component: createProtectedPage({ authType: 'public-only', component: EmailVerificationPage }),
+ component: () => <PublicOnlyPage children={<EmailVerificationPage />} />,
},
{
path: '/checkout-success',