Skip to content

Commit

Permalink
z
Browse files Browse the repository at this point in the history
  • Loading branch information
AmirAgassi committed Nov 23, 2024
1 parent 1968ab5 commit f8e23d5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
6 changes: 5 additions & 1 deletion backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=["*"],
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/components/LoginPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ export default function LoginPage() {
{isLogin ? 'Sign in to your account' : 'Create an account'}
</h2>
</div>
<form className="mt-8 space-y-6" onSubmit={handleSubmit}>
<form className="mt-8 space-y-6" onSubmit={handleSubmit} autoComplete="on">
<div className="space-y-4">
{!isLogin && (
<div>
<input
type="text"
name="username"
autoComplete="username"
value={formData.username}
onChange={handleChange}
className="w-full rounded border p-2"
Expand All @@ -85,6 +86,7 @@ export default function LoginPage() {
<input
type="email"
name="email"
autoComplete="email"
value={formData.email}
onChange={handleChange}
className="w-full rounded border p-2"
Expand All @@ -96,6 +98,7 @@ export default function LoginPage() {
<input
type="password"
name="password"
autoComplete={isLogin ? "current-password" : "new-password"}
value={formData.password}
onChange={handleChange}
className="w-full rounded border p-2"
Expand Down
63 changes: 37 additions & 26 deletions frontend/src/contexts/AuthContext.jsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,61 @@
import { createContext, useContext, useState } from 'react'
import toast from 'react-hot-toast'

const AuthContext = createContext(null)
const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000'
const API_URL = import.meta.env.VITE_API_URL

export const AuthProvider = ({ children }) => {
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
}
}
Expand Down

0 comments on commit f8e23d5

Please sign in to comment.