diff --git a/backend/main.py b/backend/main.py index 5a7d8e7..e4e9466 100644 --- a/backend/main.py +++ b/backend/main.py @@ -32,7 +32,11 @@ # configure CORS app.add_middleware( CORSMiddleware, - allow_origins=["http://localhost:5173"], # your frontend URL + allow_origins=[ + "http://localhost:5173", # development + "http://195.242.13.94", # production + "https://195.242.13.94", # https just in case lol + ], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], diff --git a/frontend/src/components/LoginPage.jsx b/frontend/src/components/LoginPage.jsx index 4d3b9ca..bafa5c3 100644 --- a/frontend/src/components/LoginPage.jsx +++ b/frontend/src/components/LoginPage.jsx @@ -66,13 +66,14 @@ export default function LoginPage() { {isLogin ? 'Sign in to your account' : 'Create an account'} -
+
{!isLogin && (
{ const [user, setUser] = useState(null) - const login = async (email, password) => { + const signup = async (email, password, username) => { try { - const response = await fetch(`${API_URL}/token`, { + const response = await fetch(`${API_URL}/register`, { method: 'POST', - headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, - body: new URLSearchParams({ - username: email, - password: password, - }), + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ email, password, username }), }) - const data = await response.json() - if (response.ok) { - setUser(data.user) - localStorage.setItem('token', data.access_token) - return true + + if (!response.ok) { + const errorData = await response.json() + toast.error(errorData.detail || 'Signup failed') + return false } - return false + + const data = await response.json() + setUser(data.user) + localStorage.setItem('token', data.access_token) + return true } catch (error) { - console.error('login error:', error) + console.error('signup error:', error) + toast.error('Network error. Please check your connection.') return false } } - const signup = async (email, password, username) => { + const login = async (email, password) => { try { - const response = await fetch(`${API_URL}/register`, { + const response = await fetch(`${API_URL}/token`, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ email, password, username }), + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: new URLSearchParams({ + username: email, + password: password, + }), }) - const data = await response.json() - if (response.ok) { - setUser(data.user) - localStorage.setItem('token', data.access_token) - return true + + if (!response.ok) { + const errorData = await response.json() + toast.error(errorData.detail || 'Login failed') + return false } - return false + + const data = await response.json() + setUser(data.user) + localStorage.setItem('token', data.access_token) + return true } catch (error) { - console.error('signup error:', error) + console.error('login error:', error) + toast.error('Network error. Please check your connection.') return false } }