|
|
|
|
@ -20,10 +20,11 @@ fileprivate enum ServiceConf: String { |
|
|
|
|
case logout = "api-token-logout/" |
|
|
|
|
case getUser = "user-by-token/" |
|
|
|
|
case changePassword = "change-password/" |
|
|
|
|
case postDeviceToken = "device-token/" |
|
|
|
|
|
|
|
|
|
var method: HTTPMethod { |
|
|
|
|
switch self { |
|
|
|
|
case .createAccount, .requestToken, .logout: |
|
|
|
|
case .createAccount, .requestToken, .logout, .postDeviceToken: |
|
|
|
|
return .post |
|
|
|
|
case .changePassword: |
|
|
|
|
return .put |
|
|
|
|
@ -36,7 +37,7 @@ fileprivate enum ServiceConf: String { |
|
|
|
|
switch self { |
|
|
|
|
case .createAccount, .requestToken: |
|
|
|
|
return false |
|
|
|
|
case .getUser, .changePassword, .logout: |
|
|
|
|
case .getUser, .changePassword, .logout, .postDeviceToken: |
|
|
|
|
return true |
|
|
|
|
// default: |
|
|
|
|
// return nil |
|
|
|
|
@ -343,6 +344,17 @@ public class Services { |
|
|
|
|
let _: Empty = try await self._runRequest(logoutRequest) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// 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 postDeviceToken(deviceToken: Data) async throws { |
|
|
|
|
let tokenString = deviceToken.map { String(format: "%02x", $0) }.joined() |
|
|
|
|
let token = DeviceToken(value: tokenString) |
|
|
|
|
Logger.log("Send device token = \(tokenString)") |
|
|
|
|
let _: DeviceToken = try await self._runRequest(serviceConf: .postDeviceToken, payload: token) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// A method that sends a request to change a user's password |
|
|
|
|
/// - Parameters: |
|
|
|
|
/// - oldPassword: the account's old password |
|
|
|
|
@ -445,6 +457,9 @@ struct Email: Codable { |
|
|
|
|
struct Empty: Codable { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
struct DeviceToken: Codable { |
|
|
|
|
var value: String |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public protocol UserBase: Codable { |
|
|
|
|
var id: String { get } |
|
|
|
|
|