Files
paradox-save-parser-frontend/src/services/AuthService.ts
T
2026-09-04 19:36:12 +02:00

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()