feat: login page — register, forgot password, no prefilled email

This commit is contained in:
Cosmo
2026-03-25 11:11:58 +00:00
parent 7ca78bfd1f
commit 14fcf7f770
2 changed files with 147 additions and 20 deletions

View File

@@ -58,6 +58,27 @@ class APIService {
}
}
func register(email: String, password: String, name: String) async throws -> LoginResponse {
let url = URL(string: "\(baseURL)/api/auth/register")!
let body = try JSONEncoder().encode(["email": email, "password": password, "name": name])
let req = makeRequest(url: url, method: "POST", body: body)
let (data, response) = try await URLSession.shared.data(for: req)
guard let httpResponse = response as? HTTPURLResponse else { throw APIError.networkError("Нет ответа") }
if httpResponse.statusCode == 409 { throw APIError.serverError(409, "Email уже занят") }
if httpResponse.statusCode != 200 && httpResponse.statusCode != 201 {
let msg = String(data: data, encoding: .utf8) ?? "Unknown"
throw APIError.serverError(httpResponse.statusCode, msg)
}
return try JSONDecoder().decode(LoginResponse.self, from: data)
}
func forgotPassword(email: String) async throws {
let url = URL(string: "\(baseURL)/api/auth/forgot-password")!
let body = try JSONEncoder().encode(["email": email])
let req = makeRequest(url: url, method: "POST", body: body)
_ = try await URLSession.shared.data(for: req)
}
func getProfile(token: String) async throws -> ProfileResponse {
let url = URL(string: "\(baseURL)/api/profile")!
let req = makeRequest(url: url, token: token)
@@ -84,4 +105,19 @@ class APIService {
}
return try JSONDecoder().decode(LatestHealthResponse.self, from: data)
}
func getHeatmap(token: String, days: Int = 7) async throws -> [HeatmapEntry] {
let url = URL(string: "\(baseURL)/api/health/heatmap?days=\(days)")!
let req = makeRequest(url: url, token: token)
let (data, response) = try await URLSession.shared.data(for: req)
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
throw APIError.networkError("Heatmap недоступен")
}
// Try array first, then wrapped response
if let entries = try? JSONDecoder().decode([HeatmapEntry].self, from: data) {
return entries
}
let wrapped = try JSONDecoder().decode(HeatmapResponse.self, from: data)
return wrapped.data
}
}