brz9front/src/lib/api.js

43 lines
1.1 KiB
JavaScript

export async function signup(email, password) {
const apiUrl = import.meta.env.VITE_API_URL;
const response = await fetch(apiUrl + '/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ email, password })
});
const data = await response.json();
if (!response.ok || data.error) {
throw new Error(data.error || `Signup failed with status ${response.status}`);
}
return data;
}
export async function login(email, password) {
const apiUrl = import.meta.env.VITE_API_URL;
const response = await fetch(apiUrl + '/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: email,
password: password
})
});
const data = await response.json();
if (!response.ok || data.error) {
throw new Error(data.error || `Login failed with status ${response.status}`);
}
// Here you could do something with the login response, like storing a user token
return data;
}