52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
//TODO: controllable rate limits
|
|
|
|
class AuthService {
|
|
private API_BASE = '/api/auth'
|
|
|
|
// Hashes the password before sending it to the backend
|
|
private hashPassword(password: string) {
|
|
// TODO: password hashing
|
|
return password
|
|
}
|
|
|
|
// Logs in with email and password, returning the parsed JSON response.
|
|
async login(email: string, password: string) {
|
|
const response = await fetch(`${this.API_BASE}/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
email,
|
|
password: this.hashPassword(password),
|
|
}),
|
|
})
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Login failed')
|
|
}
|
|
|
|
return await response.json()
|
|
}
|
|
|
|
// Registers a new user, returning the parsed JSON response.
|
|
async register(name: string, email: string, password: string) {
|
|
const response = await fetch(`${this.API_BASE}/register`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
name,
|
|
email,
|
|
password: this.hashPassword(password),
|
|
}),
|
|
})
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Registration failed')
|
|
}
|
|
|
|
return await response.json()
|
|
}
|
|
}
|
|
|
|
// Export a singleton instance
|
|
export const authService = new AuthService()
|