                         callback(true, self.token!)
                         
                     } else {
+                        self.logout()
                         callback(false, "An error occuroed.")
                     }
                     
                 } else {
+                    self.logout()
                     callback(false, "An error occured.")
                 }
             }

                if let json = returnedJson {
                    callback(false, json["message"].string!)
                } else {
                    // the user does not exist anymore, so we can delete all
                    // saved credentials
                    if error!.code == 404 {

            self.keychain.remove(Config.keychainUserEmailKey)
            self.keychain.remove(Config.keychainUserIdKey)
            self.keychain.remove(Config.keychainUserPasswordKey)
            self.logout()
        }
        
        return self.hasCredentials()
