feat: login page — register, forgot password, no prefilled email
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user