From b089a79d859808433e98186b1381deedb4c3e05e Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Wed, 13 Jan 2021 13:16:30 -0500 Subject: [PATCH] [+] Create local storage and automatically add email and token in requests --- ProjectClock/Net.swift | 11 ++++++++++- ProjectClock/ProjectClock.entitlements | 8 ++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/ProjectClock/Net.swift b/ProjectClock/Net.swift index 2536169..42d050f 100644 --- a/ProjectClock/Net.swift +++ b/ProjectClock/Net.swift @@ -9,6 +9,7 @@ import Foundation /// Base URL of the HTTP server let baseUrl = "http://localhost:8080/api" // TODO: Production settings let JSON = JSONDecoder() +let localStorage = UserDefaults(suiteName: "group.org.hydev.alarm.clock")! /// API class struct API @@ -65,7 +66,7 @@ func createUrl(_ node: String, _ params: [String: String]? = [:]) -> URL } /** - Send a HTTP request + Send a HTTP request. Email and authentication token (the initial hash of a password) is automatically added if they exist in localStorage. - Parameter api: API Node (Eg. APIs.register) - Parameter params: Parameters to send to the server (Check the documentation of the API node to see which parameters you need) @@ -74,6 +75,14 @@ func createUrl(_ node: String, _ params: [String: String]? = [:]) -> URL */ func send(_ api: API, _ params: [String: String]? = [:], _ success: @escaping (T) -> Void, err: @escaping (String) -> Void = {it in}) { + // Add default params + var params = params + if params != nil + { + if params!["email"] == nil { params!["email"] = localStorage.string(forKey: "email") } + if params!["pass"] == nil { params!["pass"] = localStorage.string(forKey: "pass") } + } + let url = createUrl(api.loc, params) // Create task diff --git a/ProjectClock/ProjectClock.entitlements b/ProjectClock/ProjectClock.entitlements index 2ab14a2..1ca1a54 100644 --- a/ProjectClock/ProjectClock.entitlements +++ b/ProjectClock/ProjectClock.entitlements @@ -2,9 +2,9 @@ - com.apple.developer.healthkit - - com.apple.developer.healthkit.access - + com.apple.security.application-groups + + group.org.hydev.alarm.clock +