From 9d3935b563e938b7fb6a1bc3a4f424c38f3875e8 Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 9 Jul 2024 11:31:03 +0200 Subject: [PATCH] Adds login/logout methods to avoid multi authentication --- LeStorage/ApiCallCollection.swift | 2 +- LeStorage/Codables/Settings.swift | 1 + LeStorage/Services.swift | 27 +++++++++++++++++++++------ LeStorage/StoreCenter.swift | 19 +++++++++++++++++++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/LeStorage/ApiCallCollection.swift b/LeStorage/ApiCallCollection.swift index 14355b7..72b8920 100644 --- a/LeStorage/ApiCallCollection.swift +++ b/LeStorage/ApiCallCollection.swift @@ -158,7 +158,7 @@ actor ApiCallCollection: SomeCallCollection { apiCall.lastAttemptDate = Date() do { - try await self._executeApiCall(apiCall) + let _ = try await self._executeApiCall(apiCall) } catch { Logger.error(error) } diff --git a/LeStorage/Codables/Settings.swift b/LeStorage/Codables/Settings.swift index 3895506..bd04379 100644 --- a/LeStorage/Codables/Settings.swift +++ b/LeStorage/Codables/Settings.swift @@ -15,4 +15,5 @@ class Settings: MicroStorable { var userId: String? = nil var username: String? = nil + var deviceId: String? = nil } diff --git a/LeStorage/Services.swift b/LeStorage/Services.swift index 8f2b4aa..deb5d1b 100644 --- a/LeStorage/Services.swift +++ b/LeStorage/Services.swift @@ -16,13 +16,14 @@ public enum HTTPMethod: String, CaseIterable, Codable { fileprivate enum ServiceConf: String { case createAccount = "users/" - case requestToken = "api-token-auth/" + case requestToken = "token-auth/" + case logout = "api-token-logout/" case getUser = "user-by-token/" case changePassword = "change-password/" var method: HTTPMethod { switch self { - case .createAccount, .requestToken: + case .createAccount, .requestToken, .logout: return .post case .changePassword: return .put @@ -35,7 +36,7 @@ fileprivate enum ServiceConf: String { switch self { case .createAccount, .requestToken: return false - case .getUser, .changePassword: + case .getUser, .changePassword, .logout: return true // default: // return nil @@ -297,7 +298,7 @@ public class Services { /// - password: the account's password public func requestToken(username: String, password: String) async throws -> String { var postRequest = try self._baseRequest(conf: .requestToken) - let credentials = Credentials(username: username, password: password) + let credentials = Credentials(username: username, password: password, deviceId: StoreCenter.main.deviceId()) postRequest.httpBody = try jsonEncoder.encode(credentials) let response: AuthResponse = try await self._runRequest(postRequest) self._storeToken(username: username, token: response.token) @@ -325,11 +326,21 @@ public class Services { _ = try await requestToken(username: username, password: password) let postRequest = try self._baseRequest(conf: .getUser) let user: U = try await self._runRequest(postRequest) - StoreCenter.main.setUserUUID(uuidString: user.id) - StoreCenter.main.setUserName(user.username) +// StoreCenter.main.setUserUUID(uuidString: user.id) +// StoreCenter.main.setUserName(user.username) + StoreCenter.main.setUserInfo(user: user) return user } + /// A login method that actually requests a token from the server, and stores the appropriate data for later usage + /// - Parameters: + /// - username: the account's username + /// - password: the account's password + public func logout() async throws { + let logoutRequest = try self._baseRequest(conf: .logout) + let _: Empty = try await self._runRequest(logoutRequest) + } + /// A method that sends a request to change a user's password /// - Parameters: /// - oldPassword: the account's old password @@ -421,6 +432,7 @@ struct AuthResponse: Codable { struct Credentials: Codable { var username: String var password: String + var deviceId: String } struct Token: Codable { var token: String @@ -428,6 +440,9 @@ struct Token: Codable { struct Email: Codable { var email: String } +struct Empty: Codable { + +} public protocol UserBase: Codable { var id: String { get } diff --git a/LeStorage/StoreCenter.swift b/LeStorage/StoreCenter.swift index b4527eb..bbe3e40 100644 --- a/LeStorage/StoreCenter.swift +++ b/LeStorage/StoreCenter.swift @@ -6,6 +6,7 @@ // import Foundation +import UIKit public class StoreCenter { @@ -81,6 +82,13 @@ public class StoreCenter { // MARK: - Settings + func setUserInfo(user: UserBase) { + self._settingsStorage.update { settings in + settings.userId = user.id + settings.username = user.username + } + } + /// Stores the user UUID func setUserUUID(uuidString: String) { self._settingsStorage.update { settings in @@ -132,6 +140,17 @@ public class StoreCenter { } } + func deviceId() -> String { + let deviceId: String = self._settingsStorage.item.deviceId ?? UIDevice.current.identifierForVendor?.uuidString ?? + UUID().uuidString + self._settingsStorage.update { settings in + settings.deviceId = deviceId + } + return deviceId + } + + // MARK: - Api Calls + func loadApiCallCollection(type: T.Type) { let apiCallCollection = ApiCallCollection() self._apiCallCollections[T.resourceName()] = apiCallCollection