Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix(auth): turning password sign-in off also stops reset links (#577)

Pasqual Troncone6bd8d58dc125
diff --git a/apps/api/src/__tests__/integration/password-reset.test.ts b/apps/api/src/__tests__/integration/password-reset.test.ts
index fb0d311f..375ac28a 100644
--- a/apps/api/src/__tests__/integration/password-reset.test.ts
+++ b/apps/api/src/__tests__/integration/password-reset.test.ts
@@ -1,20 +1,36 @@
import { describe, it, expect, beforeEach } from 'bun:test';
-import { auth } from '@repo/auth';
+import { auth, setAuthSettings } from '@repo/auth';
import { db, verification } from '@repo/db';
import { like } from 'drizzle-orm';
-import { authedApi } from '#tests/helpers/app';
+import { app, authedApi } from '#tests/helpers/app';
import { signUpTestUser } from '#tests/helpers/auth';
import { resetDb } from '#tests/helpers/db';
// The reset link goes out by mail, and no provider is configured in tests, so the
// token is read from the verification row better-auth wrote for it.
-async function resetToken(): Promise<string> {
+async function resetTokens(): Promise<string[]> {
const rows = await db
.select({ identifier: verification.identifier })
.from(verification)
.where(like(verification.identifier, 'reset-password:%'));
- expect(rows).toHaveLength(1);
- return rows[0].identifier.slice('reset-password:'.length);
+ return rows.map((row) => row.identifier.slice('reset-password:'.length));
+}
+
+async function resetToken(): Promise<string> {
+ const tokens = await resetTokens();
+ expect(tokens).toHaveLength(1);
+ return tokens[0];
+}
+
+// The endpoint the forgot-password screen posts to, through the same gate as the browser.
+function requestReset(email: string) {
+ return app.handle(
+ new Request('http://localhost/api/auth/request-password-reset', {
+ method: 'POST',
+ headers: { 'content-type': 'application/json' },
+ body: JSON.stringify({ email, redirectTo: 'http://localhost:3001/reset-password' }),
+ }),
+ );
}
describe('password reset', () => {
@@ -32,4 +48,24 @@ describe('password reset', () => {
expect((await asUser.projects.get()).status).toBe(401);
});
+
+ it('mails no reset link while password sign-in is off', async () => {
+ const user = await signUpTestUser();
+ await setAuthSettings({ emailPassword: false });
+
+ const res = await requestReset(user.email);
+
+ expect(res.status).toBe(403);
+ expect(await res.json()).toMatchObject({ code: 'PASSWORD_AUTH_DISABLED' });
+ expect(await resetTokens()).toHaveLength(0);
+ });
+
+ it('mails a reset link while password sign-in is on', async () => {
+ const user = await signUpTestUser();
+
+ const res = await requestReset(user.email);
+
+ expect(res.status).toBe(200);
+ expect(await resetTokens()).toHaveLength(1);
+ });
});
diff --git a/apps/web/src/features/auth/components/reset-password/AuthResetPasswordForm.tsx b/apps/web/src/features/auth/components/reset-password/AuthResetPasswordForm.tsx
index 89291d17..ee146a6e 100644
--- a/apps/web/src/features/auth/components/reset-password/AuthResetPasswordForm.tsx
+++ b/apps/web/src/features/auth/components/reset-password/AuthResetPasswordForm.tsx
@@ -7,6 +7,7 @@ import { useTranslations } from 'next-intl';
import { Button } from '@/components/ui/button';
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from '@/components/ui/field';
import { Input } from '@/components/ui/input';
+import { useAuthConfig } from '@/services/authConfig.service';
import AuthFormHeader from '../AuthFormHeader';
import AuthMessagePanel from '../AuthMessagePanel';
import { setNewPassword } from '../../services/auth.service';
@@ -21,6 +22,9 @@ export default function AuthResetPasswordForm() {
const token = params.get('token') ?? '';
const email = params.get('email') ?? '';
const linkError = params.get('error');
+ // The api refuses /reset-password while the password form is off, so a link issued
+ // before it was turned off must not offer a password field.
+ const passwordEnabled = useAuthConfig()?.emailPassword !== false;
const [password, setPassword] = useState('');
const [confirm, setConfirm] = useState('');
@@ -42,6 +46,20 @@ export default function AuthResetPasswordForm() {
);
}
+ if (!passwordEnabled) {
+ return (
+ <AuthMessagePanel
+ title={t('resetPassword.deadLinkTitle')}
+ description={t('errors.passwordDisabled')}
+ footer={
+ <Link href="/login" className="underline underline-offset-4">
+ {t('forgotPassword.backToSignIn')}
+ </Link>
+ }
+ />
+ );
+ }
+
if (!token || linkError) {
return (
<AuthMessagePanel
diff --git a/packages/auth/src/index.ts b/packages/auth/src/index.ts
index 94aa00d8..803151d6 100644
--- a/packages/auth/src/index.ts
+++ b/packages/auth/src/index.ts
@@ -382,7 +382,7 @@ const PASSWORD_PATHS = new Set([
'/sign-up/email',
'/sign-in/email',
'/sign-in/username',
- '/forget-password',
+ '/request-password-reset',
'/reset-password',
'/sign-in/magic-link',
'/magic-link/verify',