Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(auth): stop rejecting the desktop account's own email at login (#1152)

Tashfeen9cfc5b2d3feb
diff --git a/server/src/__tests__/routes/auth-login-identifier.test.ts b/server/src/__tests__/routes/auth-login-identifier.test.ts
new file mode 100644
index 00000000..3253c94b
--- /dev/null
+++ b/server/src/__tests__/routes/auth-login-identifier.test.ts
@@ -0,0 +1,70 @@
+import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
+import express, { type Express } from 'express';
+import { authRouter } from '../../routes/auth.js';
+import { initDb, getDb } from '../../db/index.js';
+import { createUser } from '../../services/auth.js';
+
+// The desktop app seeds its hidden account as `desktop@localhost`
+// (desktop/src/server-host.ts). That address has no TLD, so while /login shared
+// the signup schema's z.email() every sign-in on a desktop install was rejected
+// with "A valid email is required" before the password was ever checked — which
+// also made the reset-then-sign-in-from-a-browser workaround in #807 impossible.
+// Logging in is a lookup, so the address is matched, not validated.
+
+function app(): Express {
+ const a = express();
+ a.use(express.json());
+ a.use('/api/auth', authRouter);
+ return a;
+}
+
+async function postLogin(body: unknown) {
+ const server = app().listen(0, '127.0.0.1');
+ if (!server.listening) await new Promise<void>(resolve => server.once('listening', () => resolve()));
+ const addr = server.address() as { port: number };
+ const res = await fetch(`http://127.0.0.1:${addr.port}/api/auth/login`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify(body),
+ });
+ const data = await res.json().catch(() => null);
+ server.close();
+ return { status: res.status, body: data as any };
+}
+
+describe('login accepts the identifier the desktop app actually seeds', () => {
+ beforeAll(() => {
+ initDb(':memory:');
+ });
+
+ beforeEach(() => {
+ getDb().prepare('DELETE FROM sessions').run();
+ getDb().prepare('DELETE FROM users').run();
+ });
+
+ it('signs in an account whose email has no TLD', async () => {
+ createUser('desktop@localhost', 'a-long-random-password');
+ const res = await postLogin({ email: 'desktop@localhost', password: 'a-long-random-password' });
+ expect(res.status).toBe(200);
+ expect(res.body.token).toBeTruthy();
+ });
+
+ it('still rejects the wrong password for that account, with the credential error', async () => {
+ createUser('desktop@localhost', 'a-long-random-password');
+ const res = await postLogin({ email: 'desktop@localhost', password: 'not-the-password' });
+ expect(res.status).toBe(401);
+ // Not a 400 about the address shape — the address was fine, the password was not.
+ expect(res.body.error.message).toBe('Invalid email or password');
+ });
+
+ it('signs in an account created under an older, shorter password policy', async () => {
+ createUser('[email protected]', 'short');
+ const res = await postLogin({ email: '[email protected]', password: 'short' });
+ expect(res.status).toBe(200);
+ });
+
+ it('still requires both fields', async () => {
+ const res = await postLogin({ email: '', password: '' });
+ expect(res.status).toBe(400);
+ });
+});
diff --git a/server/src/routes/auth.ts b/server/src/routes/auth.ts
index 483c429d..3e85163a 100644
--- a/server/src/routes/auth.ts
+++ b/server/src/routes/auth.ts
@@ -23,11 +23,25 @@ const failedPasswordAttempts = new Map<number, number>();
// /status, /setup and /login are reachable without a session (bootstrap);
// /logout and /me validate the token themselves.
-const credentialsSchema = z.object({
+// Signing up is the one place the address has to look like an address.
+const signupSchema = z.object({
email: z.string().email('A valid email is required'),
password: z.string().min(8, 'Password must be at least 8 characters'),
});
+// Logging in is a lookup, not a registration, so the address is matched rather
+// than validated. The desktop app seeds its hidden account as
+// `desktop@localhost` (server-host.ts), which has no TLD and so could never
+// satisfy z.email() — every login attempt on a desktop install failed with
+// "A valid email is required" before the password was even checked, including
+// the reset-then-sign-in-from-a-browser route suggested in #807. Length rules
+// belong to signup too: an account created under an older policy must still be
+// able to get in.
+const loginSchema = z.object({
+ email: z.string().min(1, 'Email is required'),
+ password: z.string().min(1, 'Password is required'),
+});
+
// ── Brute-force throttle ──────────────────────────────────────────────────
// Simple in-memory per-email limiter. A local single-user tool doesn't need a
// distributed store; this just blunts online password guessing.
@@ -104,7 +118,7 @@ authRouter.post('/setup', (req: Request, res: Response) => {
return;
}
- const parsed = credentialsSchema.safeParse(req.body);
+ const parsed = signupSchema.safeParse(req.body);
if (!parsed.success) {
res.status(400).json({ error: { message: parsed.error.errors.map(e => e.message).join(', ') } });
return;
@@ -116,7 +130,7 @@ authRouter.post('/setup', (req: Request, res: Response) => {
});
authRouter.post('/login', (req: Request, res: Response) => {
- const parsed = credentialsSchema.safeParse(req.body);
+ const parsed = loginSchema.safeParse(req.body);
if (!parsed.success) {
res.status(400).json({ error: { message: parsed.error.errors.map(e => e.message).join(', ') } });
return;