adding delete account, closes #21

This commit is contained in:
andy 2022-08-11 20:53:09 +01:00
parent 9b59baa8f1
commit ad55f5b787
3 changed files with 106 additions and 13 deletions

View File

@ -9,6 +9,7 @@
import Foundation import Foundation
import Alamofire import Alamofire
import SwiftyJSON import SwiftyJSON
import KeychainAccess
class LiveUser: ObservableObject { class LiveUser: ObservableObject {
@ -34,6 +35,31 @@ class LiveUser: ObservableObject {
self.loggedIn = loggedIn self.loggedIn = loggedIn
} }
init(playlists: [Playlist], tags: [Tag], username: String, loggedIn: Bool, user: User) {
self.playlists = playlists
self.tags = tags
self.username = username
self.loggedIn = loggedIn
self.user = user
}
func logout() {
let keychain = Keychain(service: "xyz.sarsoo.music.login")
do {
try keychain.remove("username")
try keychain.remove("jwt")
UserDefaults.standard.removeObject(forKey: "playlists")
UserDefaults.standard.removeObject(forKey: "tags")
self.loggedIn = false
} catch let error {
debugPrint("Could not clear keychain, \(error)")
}
}
func updatePlaylist(playlistIn: Playlist) { func updatePlaylist(playlistIn: Playlist) {
guard let index = self.playlists.firstIndex(of: playlistIn) else { guard let index = self.playlists.firstIndex(of: playlistIn) else {
fatalError("\(playlistIn) not found") fatalError("\(playlistIn) not found")
@ -166,7 +192,7 @@ class LiveUser: ObservableObject {
if let statusCode = response.response?.statusCode { if let statusCode = response.response?.statusCode {
switch statusCode { switch statusCode {
case 401: // token has expired case 401: // token has expired
self.loggedIn = false self.logout()
return false return false
case 400..<500: case 400..<500:
return false return false

View File

@ -12,6 +12,7 @@ import SwiftyJSON
public enum UserApi { public enum UserApi {
case getUser case getUser
case deleteUser
} }
extension UserApi: ApiRequest { extension UserApi: ApiRequest {
@ -23,6 +24,8 @@ extension UserApi: ApiRequest {
switch self { switch self {
case .getUser: case .getUser:
return "api/user" return "api/user"
case .deleteUser:
return "api/user"
} }
} }
@ -30,6 +33,8 @@ extension UserApi: ApiRequest {
switch self { switch self {
case .getUser: case .getUser:
return .get return .get
case .deleteUser:
return .delete
} }
} }
@ -49,5 +54,42 @@ extension UserApi: ApiRequest {
return ApiRequestDefaults.authMethod return ApiRequestDefaults.authMethod
} }
static func fromJSON(user: Data) -> User? {
let decoder = JSONDecoder()
do {
let user = try decoder.decode(User.self, from: user)
return user
} catch {
print(error)
}
return nil
}
static func fromJSON(user: JSON) -> User? {
let _json = user.rawString()?.data(using: .utf8)
if let data = _json {
let decoder = JSONDecoder()
do {
let user = try decoder.decode(User.self, from: data)
return user
} catch {
print(error)
}
}
return nil
}
static func fromJSON(user: [JSON]) -> [User] {
var _users: [User] = []
for dict in user {
let _iter = self.fromJSON(user: dict)
if let returned = _iter {
_users.append(returned)
}
}
return _users
}
} }

View File

@ -13,6 +13,8 @@ struct SettingsList: View {
@EnvironmentObject var liveUser: LiveUser @EnvironmentObject var liveUser: LiveUser
@State private var deleteAlertShowing = false
var body: some View { var body: some View {
NavigationView { NavigationView {
List{ List{
@ -25,21 +27,43 @@ struct SettingsList: View {
Text("Launch Web Version") Text("Launch Web Version")
} }
Button(action: { Button(action: {
let keychain = Keychain(service: "xyz.sarsoo.music.login") self.liveUser.logout()
do {
try keychain.remove("username")
try keychain.remove("password")
try keychain.remove("jwt")
self.liveUser.loggedIn = false
} catch let error {
debugPrint("Could not clear keychain, \(error)")
}
}) { }) {
Text("Log out") Text("Log out")
} }
} }
Section {
Button(action: {
deleteAlertShowing = true
}) {
Text("Delete Account")
.foregroundColor(.red)
}
.alert("Delete Account", isPresented: $deleteAlertShowing, actions: {
Text("This will irreversibly delete all of your data, are you sure?")
Button("Delete", role: .destructive) {
let api = UserApi.deleteUser
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
if self.liveUser.checkNetworkResponse(response: response) {
self.liveUser.logout()
}
else {
}
}
}
Button("Cancel", role: .cancel) {
deleteAlertShowing = false
}
}, message: {
Text("This is irreversible, are you sure you want to delete your account?")
})
}
Section( Section(
header: header:
Text("Development"), Text("Development"),
@ -70,7 +94,7 @@ struct SettingsList: View {
} }
} }
.listStyle(GroupedListStyle()) .listStyle(GroupedListStyle())
.navigationBarTitle(Text("Settings ⚡️").font(.title)) .navigationBarTitle(Text("Settings ⚡️"))
} }
} }
} }
@ -78,5 +102,6 @@ struct SettingsList: View {
struct SettingsList_Previews: PreviewProvider { struct SettingsList_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
SettingsList() SettingsList()
.environmentObject(LiveUser(playlists: [], tags: [], username: "user", loggedIn: false))
} }
} }