Spaces:
Sleeping
Sleeping
Commit ·
5657fcc
1
Parent(s): b256452
fix: Better Auth integration - map sign-in/email to backend login, exclude auth from rewrites
Browse files- frontend/next.config.js +6 -2
- frontend/pages/api/auth/[...all].ts +20 -4
- frontend/pages/login.tsx +17 -2
frontend/next.config.js
CHANGED
|
@@ -12,8 +12,12 @@ const nextConfig = {
|
|
| 12 |
destination: 'http://localhost:5000/:path*',
|
| 13 |
},
|
| 14 |
{
|
| 15 |
-
source: '/api/:path*',
|
| 16 |
-
destination: 'http://localhost:8000/api/:path*',
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
},
|
| 18 |
];
|
| 19 |
},
|
|
|
|
| 12 |
destination: 'http://localhost:5000/:path*',
|
| 13 |
},
|
| 14 |
{
|
| 15 |
+
source: '/api/todos/:path*',
|
| 16 |
+
destination: 'http://localhost:8000/api/todos/:path*',
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
source: '/health',
|
| 20 |
+
destination: 'http://localhost:8000/health',
|
| 21 |
},
|
| 22 |
];
|
| 23 |
},
|
frontend/pages/api/auth/[...all].ts
CHANGED
|
@@ -1,10 +1,25 @@
|
|
| 1 |
/**
|
| 2 |
* Better Auth API route handler.
|
| 3 |
-
*
|
| 4 |
*/
|
| 5 |
import type { NextApiRequest, NextApiResponse } from 'next';
|
| 6 |
|
| 7 |
-
const BACKEND_URL =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
export default async function handler(
|
| 10 |
req: NextApiRequest,
|
|
@@ -12,9 +27,10 @@ export default async function handler(
|
|
| 12 |
) {
|
| 13 |
const { all } = req.query;
|
| 14 |
const path = Array.isArray(all) ? all.join('/') : all || '';
|
|
|
|
| 15 |
|
| 16 |
try {
|
| 17 |
-
const backendUrl = `${BACKEND_URL}/api/auth/${
|
| 18 |
const response = await fetch(backendUrl, {
|
| 19 |
method: req.method || 'GET',
|
| 20 |
headers: {
|
|
@@ -33,4 +49,4 @@ export default async function handler(
|
|
| 33 |
} catch (error) {
|
| 34 |
res.status(500).json({ error: 'Auth service unavailable' });
|
| 35 |
}
|
| 36 |
-
}
|
|
|
|
| 1 |
/**
|
| 2 |
* Better Auth API route handler.
|
| 3 |
+
* Maps Better Auth endpoints to FastAPI backend auth endpoints.
|
| 4 |
*/
|
| 5 |
import type { NextApiRequest, NextApiResponse } from 'next';
|
| 6 |
|
| 7 |
+
const BACKEND_URL = 'http://localhost:8000';
|
| 8 |
+
|
| 9 |
+
/**
|
| 10 |
+
* Map Better Auth paths to backend paths.
|
| 11 |
+
* Better Auth uses: /api/auth/sign-in/email, /api/auth/sign-up/email
|
| 12 |
+
* Backend uses: /api/auth/login, /api/auth/signup
|
| 13 |
+
*/
|
| 14 |
+
function mapAuthPath(path: string): string {
|
| 15 |
+
if (path === 'sign-in/email' || path === 'sign-in') {
|
| 16 |
+
return 'login';
|
| 17 |
+
}
|
| 18 |
+
if (path === 'sign-up/email' || path === 'sign-up') {
|
| 19 |
+
return 'signup';
|
| 20 |
+
}
|
| 21 |
+
return path;
|
| 22 |
+
}
|
| 23 |
|
| 24 |
export default async function handler(
|
| 25 |
req: NextApiRequest,
|
|
|
|
| 27 |
) {
|
| 28 |
const { all } = req.query;
|
| 29 |
const path = Array.isArray(all) ? all.join('/') : all || '';
|
| 30 |
+
const mappedPath = mapAuthPath(path);
|
| 31 |
|
| 32 |
try {
|
| 33 |
+
const backendUrl = `${BACKEND_URL}/api/auth/${mappedPath}`;
|
| 34 |
const response = await fetch(backendUrl, {
|
| 35 |
method: req.method || 'GET',
|
| 36 |
headers: {
|
|
|
|
| 49 |
} catch (error) {
|
| 50 |
res.status(500).json({ error: 'Auth service unavailable' });
|
| 51 |
}
|
| 52 |
+
}
|
frontend/pages/login.tsx
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
/**
|
| 2 |
-
* Login page with modern UI
|
| 3 |
*/
|
| 4 |
import { useState, FormEvent } from 'react';
|
| 5 |
import { useRouter } from 'next/router';
|
| 6 |
-
import {
|
|
|
|
| 7 |
|
| 8 |
export default function Login() {
|
| 9 |
const router = useRouter();
|
|
@@ -18,6 +19,20 @@ export default function Login() {
|
|
| 18 |
setLoading(true);
|
| 19 |
|
| 20 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
const response = await apiCall('/api/auth/login', 'POST', { email, password }, false);
|
| 22 |
setToken(response.token);
|
| 23 |
router.push('/todos');
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Login page with modern UI using Better Auth signIn.email().
|
| 3 |
*/
|
| 4 |
import { useState, FormEvent } from 'react';
|
| 5 |
import { useRouter } from 'next/router';
|
| 6 |
+
import { signIn } from '@/lib/auth';
|
| 7 |
+
import { setToken } from '@/lib/api';
|
| 8 |
|
| 9 |
export default function Login() {
|
| 10 |
const router = useRouter();
|
|
|
|
| 19 |
setLoading(true);
|
| 20 |
|
| 21 |
try {
|
| 22 |
+
// Better Auth signIn - calls /api/auth/sign-in/email
|
| 23 |
+
// which maps to backend /api/auth/login via [...all].ts
|
| 24 |
+
const result = await signIn.email({ email, password });
|
| 25 |
+
if (result?.data?.token) {
|
| 26 |
+
setToken(result.data.token);
|
| 27 |
+
router.push('/todos');
|
| 28 |
+
return;
|
| 29 |
+
}
|
| 30 |
+
if (result?.error) {
|
| 31 |
+
throw new Error(result.error.message || 'Login failed');
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
// Fallback: direct API call to backend
|
| 35 |
+
const { apiCall } = await import('@/lib/api');
|
| 36 |
const response = await apiCall('/api/auth/login', 'POST', { email, password }, false);
|
| 37 |
setToken(response.token);
|
| 38 |
router.push('/todos');
|