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