Generating account ID

This commit is contained in:
Pengwius 2021-03-08 12:37:02 +01:00
parent b0ccdd48cc
commit ee8f6e240a
3 changed files with 36 additions and 43 deletions

View file

@ -142,30 +142,27 @@ public class Sdk {
let parsedError = self.parseResponse(response) { let parsedError = self.parseResponse(response) {
completionHandler(parsedError) completionHandler(parsedError)
} else { } else {
do {
let keychain = Keychain() let keychain = Keychain()
let allAccountsCheck: String! = keychain["allAccounts"] ?? "[]" let allAccountsCheck: String! = keychain["allAccounts"] ?? "[]"
let receiveValueJSON = try! JSON(data: data) let receiveValueJSON = try! JSON(data: data)
//parsing allAccounts to array //parsing allAccounts to array
var allAccountsArray: [String] = []
if(allAccountsCheck != "[]"){
let data = Data(allAccountsCheck.utf8) let data = Data(allAccountsCheck.utf8)
do { do {
let ids = try JSONSerialization.jsonObject(with: data) as! [String] var ids = try JSONSerialization.jsonObject(with: data) as! [Int]
for id in ids { if(ids == [])
let student = keychain[id] {
let data = Data(student!.utf8) ids = [0]
let accountParsed = try! JSON(data: data) } else {
allAccountsArray.append("\(accountParsed["account"]["UserLogin"])") ids.append(ids.last! + 1)
} }
keychain["allAccounts"] = "\(ids)"
} catch { } catch {
print(error) print(error)
} }
}
if allAccountsArray.contains("\(receiveValueJSON["Envelope"]["UserName"])") {
keychain["accountExist"] = "true"
} else {
// Get private key // Get private key
guard let privateKeyRawData = self.certificate.getPrivateKeyData(format: .DER), guard let privateKeyRawData = self.certificate.getPrivateKeyData(format: .DER),
let privateKeyString = String(data: privateKeyRawData, encoding: .utf8)? let privateKeyString = String(data: privateKeyRawData, encoding: .utf8)?
@ -196,18 +193,14 @@ public class Sdk {
} }
""" """
keychain["\(receiveValueJSON["Envelope"]["LoginId"])"] = "\(saveAccount)" let ids = keychain["allAccounts"]
let allAccounts = keychain["allAccounts"] ?? "[]" let dataIds: Data = Data(ids!.utf8)
let data = Data(allAccounts.utf8) let idsArray = try JSONSerialization.jsonObject(with: dataIds) as! [Int]
do { let id = idsArray.last
var array = try JSONSerialization.jsonObject(with: data) as! [String]
array.append("\(receiveValueJSON["Envelope"]["LoginId"])")
keychain["allAccounts"] = "\(array)"
} catch {
print(error)
}
keychain["actualStudentId"] = "\(receiveValueJSON["Envelope"]["LoginId"])" keychain["\(id!)"] = "\(saveAccount)"
keychain["actualStudentId"] = "\(id!)"
keychain["actualAccountEmail"] = "\(receiveValueJSON["Envelope"]["UserName"])" keychain["actualAccountEmail"] = "\(receiveValueJSON["Envelope"]["UserName"])"
let endpointURL: String = "\(receiveValueJSON["Envelope"]["RestURL"])api/mobile/register/hebe" let endpointURL: String = "\(receiveValueJSON["Envelope"]["RestURL"])api/mobile/register/hebe"
@ -228,9 +221,11 @@ public class Sdk {
// Handle unexpected error // Handle unexpected error
} }
}.resume() }.resume()
} catch {
print(error)
}
} }
completionHandler(nil) completionHandler(nil)
}
}) })
.store(in: &cancellables) .store(in: &cancellables)
} }

View file

@ -25,12 +25,9 @@ struct AccountManagerView: View {
if(allAccounts != "[]"){ if(allAccounts != "[]"){
let data = Data(allAccounts.utf8) let data = Data(allAccounts.utf8)
do { do {
let ids = try JSONSerialization.jsonObject(with: data) as! [String] let ids = try JSONSerialization.jsonObject(with: data) as! [Int]
for id in ids { for id in ids {
let student = keychain[id] allAccountsArray.append("\(id)")
let data = Data(student!.utf8)
let accountParsed = try! JSON(data: data)
allAccountsArray.append("\(accountParsed["account"]["UserLogin"])")
} }
} catch { } catch {
print(error) print(error)
@ -65,11 +62,15 @@ struct AccountManagerView: View {
Section(header: Text("chooseAccount") Section(header: Text("chooseAccount")
.font(.title)) { .font(.title)) {
ForEach(getStudentsNames(), id: \.self) { student in ForEach(getStudentsNames(), id: \.self) { student in
HStack {
Button("\(student)") { setActualAccount() }
.foregroundColor(Color("customControlColor"))
let keychain = Keychain() let keychain = Keychain()
if("\(keychain["actualAccountEmail"] ?? "")" == student) { HStack {
let studentString = "\(keychain[student] ?? "{}")"
let data: Data = Data(studentString.utf8)
let studentJSON = try! JSON(data: data)
let studentEmail = "\(studentJSON["account"]["UserName"])"
Button("\(studentEmail)") { setActualAccount() }
.foregroundColor(Color("customControlColor"))
if("\(keychain["actualAccountId"] ?? "0")" == "\(student)") {
Image(systemName: "checkmark.circle") Image(systemName: "checkmark.circle")
.foregroundColor(.green) .foregroundColor(.green)
} }

View file

@ -61,17 +61,14 @@ struct LoginView: View {
case "deviceExist": case "deviceExist":
showingAlert.toggle() showingAlert.toggle()
success = false
default: default:
buttonValue = String(format: NSLocalizedString("invalidData", comment: "loginButton")) buttonValue = String(format: NSLocalizedString("invalidData", comment: "loginButton"))
} }
} else { } else {
let keychain = Keychain()
if(keychain["accountExist"] == "true") {
showingAlert.toggle()
keychain["accountExist"] = "false"
}
print("success") print("success")
success = true
} }
} }
} }