     }
 
 }
+
+fileprivate struct LoginResponse {
+
+    private static let dateFormatter = Iso8601DateFormatter()
+
+    var userID: Int
+    var username: String
+    var authToken: String
+    var authTokenExpiry: Date
+
+    init?(json: [String : Any]) {
+        var userID: Int = 0
+        guard let username = json["Username"] as? String,
+               let userIDString = json["Uid"] as? String,
+               let authToken = json["Token"] as? String,
+               let dateString = json["TokenValidUntil"] as? String,
+               let expiry = LoginResponse.dateFormatter.date(from: dateString),
+               Scanner(string: userIDString).scanInt(&userID) else {
+            return nil
+        }
+
+        self.userID = userID
+        self.username = username
+        self.authToken = authToken
+        self.authTokenExpiry = expiry
+    }
+
+}

    private var userAuthenticationTokenValid = false
    private var registeredDeviceToken: Data?
    private var userAuthenticationToken: String?
    private let dateFormatter = Iso8601DateFormatter()
    private var loginObservers = [LoginObserver]()

    init(remoteNotificationsTokenRegistration: RemoteNotificationsTokenRegistration,

    }

    private func handleNetworkLoginResponse(_ responseData: Data?) {
        guard let responseData = responseData,
              let json = try? JSONSerialization.jsonObject(with: responseData, options: .allowFragments),
              let jsonDictionary = json as? [String : Any],
              let response = LoginResponse(json: jsonDictionary) else {
            loginObservers.forEach { $0.loginFailed() }
            return
        }

        let credential = LoginCredential(username: response.username,
                                         registrationNumber: response.userID,
                                         authenticationToken: response.authToken,
                                         tokenExpiryDate: response.authTokenExpiry)
        loginCredentialStore.store(credential)
        loginObservers.forEach { $0.loginSucceeded() }
    }
