From cae7cbb98fbd6f6f698bf40fc622bddf795dc1ce Mon Sep 17 00:00:00 2001 From: francy51 Date: Sat, 21 Feb 2026 23:16:16 -0500 Subject: [PATCH] Remove social auth and stabilize email/password flow --- .env.example | 5 +-- backend/src/auth.ts | 17 ++++---- backend/src/index.ts | 4 +- backend/src/routes/better-auth.ts | 2 +- docker-compose.yml | 1 + frontend/app/auth/signin/page.tsx | 70 ++++++++----------------------- frontend/app/auth/signup/page.tsx | 19 +++++++-- 7 files changed, 46 insertions(+), 72 deletions(-) diff --git a/.env.example b/.env.example index 666c4f3..8f9bde5 100644 --- a/.env.example +++ b/.env.example @@ -11,10 +11,7 @@ NODE_ENV=development JWT_SECRET=change-this-to-a-random-secret-key BETTER_AUTH_SECRET=change-this-to-a-random-secret-key BETTER_AUTH_BASE_URL=http://localhost:3001 -GITHUB_ID= -GITHUB_SECRET= -GOOGLE_ID= -GOOGLE_SECRET= +FRONTEND_URL=http://localhost:3000 # Frontend NEXT_PUBLIC_API_URL=http://localhost:3001 diff --git a/backend/src/auth.ts b/backend/src/auth.ts index 669182b..bad2327 100644 --- a/backend/src/auth.ts +++ b/backend/src/auth.ts @@ -2,23 +2,20 @@ import { betterAuth } from "better-auth"; import { Pool } from "pg"; const defaultDatabaseUrl = `postgres://${process.env.POSTGRES_USER || 'postgres'}:${process.env.POSTGRES_PASSWORD || 'postgres'}@${process.env.POSTGRES_HOST || 'localhost'}:5432/${process.env.POSTGRES_DB || 'fiscal'}`; +const defaultFrontendUrl = process.env.FRONTEND_URL || 'http://localhost:3000'; +const trustedOrigins = defaultFrontendUrl + .split(',') + .map((origin) => origin.trim()) + .filter(Boolean); export const auth = betterAuth({ database: new Pool({ connectionString: process.env.DATABASE_URL || defaultDatabaseUrl, }), + trustedOrigins, emailAndPassword: { enabled: true, - }, - socialProviders: { - github: { - clientId: process.env.GITHUB_ID as string, - clientSecret: process.env.GITHUB_SECRET as string, - }, - google: { - clientId: process.env.GOOGLE_ID as string, - clientSecret: process.env.GOOGLE_SECRET as string, - }, + autoSignIn: true, }, user: { modelName: "users", diff --git a/backend/src/index.ts b/backend/src/index.ts index ec176cf..25fec57 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -12,11 +12,13 @@ import { openclawRoutes } from './routes/openclaw'; import { watchlistRoutes } from './routes/watchlist'; import { betterAuthRoutes } from './routes/better-auth'; +const frontendOrigin = process.env.FRONTEND_URL || 'http://localhost:3000'; + const app = new Elysia({ prefix: '/api' }) .use(cors({ - origin: '*', + origin: frontendOrigin, credentials: true, methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'] })) diff --git a/backend/src/routes/better-auth.ts b/backend/src/routes/better-auth.ts index e08232b..f3944f0 100644 --- a/backend/src/routes/better-auth.ts +++ b/backend/src/routes/better-auth.ts @@ -2,6 +2,6 @@ import { Elysia } from 'elysia'; import { auth } from '../auth'; export const betterAuthRoutes = new Elysia() - .all('/api/auth/*', async ({ request }) => { + .all('/auth/*', async ({ request }) => { return auth.handler(request); }); diff --git a/docker-compose.yml b/docker-compose.yml index 9cb3b3a..cc96064 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -32,6 +32,7 @@ services: POSTGRES_HOST: postgres BETTER_AUTH_SECRET: ${BETTER_AUTH_SECRET:-local-dev-better-auth-secret-change-me} BETTER_AUTH_BASE_URL: ${BETTER_AUTH_BASE_URL:-http://localhost:3001} + FRONTEND_URL: ${FRONTEND_URL:-http://localhost:3000} expose: - "3001" depends_on: diff --git a/frontend/app/auth/signin/page.tsx b/frontend/app/auth/signin/page.tsx index e140078..1c660a9 100644 --- a/frontend/app/auth/signin/page.tsx +++ b/frontend/app/auth/signin/page.tsx @@ -1,14 +1,24 @@ 'use client'; -import { signIn } from '@/lib/better-auth'; -import { useState } from 'react'; +import { signIn, useSession } from '@/lib/better-auth'; +import { useRouter } from 'next/navigation'; +import { useEffect, useState } from 'react'; export default function SignIn() { + const { data: session, isPending: sessionPending } = useSession(); + const router = useRouter(); + const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); + useEffect(() => { + if (!sessionPending && session?.user) { + router.replace('/'); + } + }, [sessionPending, session, router]); + const handleCredentialsLogin = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); @@ -22,9 +32,11 @@ export default function SignIn() { if (result.error) { setError(result.error.message || 'Invalid credentials'); - } else { - window.location.href = '/'; + return; } + + router.replace('/'); + router.refresh(); } catch (err) { setError('Login failed'); } finally { @@ -32,17 +44,6 @@ export default function SignIn() { } }; - const handleSocialSignIn = async (provider: 'github' | 'google') => { - try { - await signIn.social({ - provider, - callbackURL: '/', - }); - } catch (err) { - setError(`Failed to sign in with ${provider}`); - } - }; - return (
@@ -89,50 +90,15 @@ export default function SignIn() { -
-
-
-
-
-
- Or continue with -
-
-
- -
- - -
-

- Don't have an account?{' '} + Don't have an account?{' '} Sign up diff --git a/frontend/app/auth/signup/page.tsx b/frontend/app/auth/signup/page.tsx index 5f861e6..282f617 100644 --- a/frontend/app/auth/signup/page.tsx +++ b/frontend/app/auth/signup/page.tsx @@ -1,15 +1,25 @@ 'use client'; -import { signUp } from '@/lib/better-auth'; -import { useState } from 'react'; +import { signUp, useSession } from '@/lib/better-auth'; +import { useRouter } from 'next/navigation'; +import { useEffect, useState } from 'react'; export default function SignUp() { + const { data: session, isPending: sessionPending } = useSession(); + const router = useRouter(); + const [name, setName] = useState(''); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); + useEffect(() => { + if (!sessionPending && session?.user) { + router.replace('/'); + } + }, [sessionPending, session, router]); + const handleSignUp = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); @@ -25,7 +35,8 @@ export default function SignUp() { if (result.error) { setError(result.error.message || 'Sign up failed'); } else { - window.location.href = '/'; + router.replace('/'); + router.refresh(); } } catch (err) { setError('Sign up failed'); @@ -94,7 +105,7 @@ export default function SignUp() {