56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
//TODO: replace this whole service with Discord OAuth. The target audience
|
|
// already uses Discord, so email/password login is not worth building. The
|
|
// /api/auth/login and /api/auth/register routes below are never implemented —
|
|
// delete them along with this service.
|
|
//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()
|