working jwt

This commit is contained in:
andy 2022-08-08 21:59:28 +01:00
parent dc6b03dd3c
commit 4f2e0f245c
5 changed files with 33 additions and 31 deletions

View File

@ -8,7 +8,6 @@
import UIKit
import SwiftyJSON
import KeychainAccess
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

View File

@ -44,27 +44,40 @@ class LiveUser: ObservableObject {
let api = PlaylistApi.getPlaylists
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
switch response.result {
case .success:
guard let data = response.data else {
fatalError("error getting playlists")
}
guard let data = response.data else {
fatalError("error getting playlists")
}
guard let json = try? JSON(data: data) else {
fatalError("error parsing reponse")
}
guard let json = try? JSON(data: data) else {
fatalError("error parsing reponse")
}
let playlists = json["playlists"].arrayValue
let playlists = json["playlists"].arrayValue
// update state
self.playlists = PlaylistApi.fromJSON(playlist: playlists).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
self.isRefreshingPlaylists = false
let encoder = JSONEncoder()
do {
UserDefaults.standard.set(String(data: try encoder.encode(playlists), encoding: .utf8), forKey: "playlists")
} catch {
print("error encoding playlists: \(error)")
// update state
self.playlists = PlaylistApi.fromJSON(playlist: playlists).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
self.isRefreshingPlaylists = false
let encoder = JSONEncoder()
do {
UserDefaults.standard.set(String(data: try encoder.encode(playlists), encoding: .utf8), forKey: "playlists")
} catch {
print("error encoding playlists: \(error)")
}
case .failure:
switch response.response?.statusCode {
case 401:
self.loggedIn = false
case _:
print("error")
}
}
}
}

View File

@ -43,16 +43,8 @@ public enum AuthMethod {
struct RequestBuilder {
static func buildRequest(apiRequest: ApiRequest) -> Alamofire.DataRequest {
print(apiRequest.authMethod)
print(apiRequest.headers)
print(apiRequest.httpMethod)
let txHeaders = apiRequest.authMethod?.auth(headers: apiRequest.headers)
print(txHeaders)
print(apiRequest.parameters)
print(apiRequest.domain + apiRequest.path)
if apiRequest.parameters != nil {
if apiRequest.parameterType != nil {

View File

@ -39,10 +39,7 @@ struct LoginScreen: View {
let keychain = Keychain(service: "xyz.sarsoo.music.login")
keychain["username"] = username
keychain["password"] = password
print(username)
print(password)
// keychain["password"] = password
let api = AuthApi.token(username: username, password: password)
RequestBuilder.buildRequest(apiRequest: api)

View File

@ -29,6 +29,7 @@ struct SettingsList: View {
do {
try keychain.remove("username")
try keychain.remove("password")
try keychain.remove("jwt")
self.liveUser.loggedIn = false