Adds login/logout methods to avoid multi authentication

sync
Laurent 1 year ago
parent 234bb42ea7
commit 9d3935b563
  1. 2
      LeStorage/ApiCallCollection.swift
  2. 1
      LeStorage/Codables/Settings.swift
  3. 27
      LeStorage/Services.swift
  4. 19
      LeStorage/StoreCenter.swift

@ -158,7 +158,7 @@ actor ApiCallCollection<T: Storable>: SomeCallCollection {
apiCall.lastAttemptDate = Date()
do {
try await self._executeApiCall(apiCall)
let _ = try await self._executeApiCall(apiCall)
} catch {
Logger.error(error)
}

@ -15,4 +15,5 @@ class Settings: MicroStorable {
var userId: String? = nil
var username: String? = nil
var deviceId: String? = nil
}

@ -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 }

@ -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<T: Storable>(type: T.Type) {
let apiCallCollection = ApiCallCollection<T>()
self._apiCallCollections[T.resourceName()] = apiCallCollection

Loading…
Cancel
Save