private StableSession() { if (LoadSettings() == false) { _settings = new SettingsObject(); } CurrentUserData = null; }
private void GetTokenAsync(JObject paramsObject, LoginResultDelegate resultDelegate) { ApiRequest apiReq = new ApiRequest { RequestBody = paramsObject.ToString() }; Task.Run(async () => { ApiRequest.ApiResponse apiResp = await apiReq.StartRequestAsync("get_token"); Tuple<LoginResult, string> loginResult = ParseTokenResult(apiResp); if (loginResult.Item1 == LoginResult.Ok) { Tuple<UserData, string> userData = await GetUserData(); CurrentUserData = userData.Item1; IsActive = (CurrentUserData != null); if (IsActive) { SaveSettings(); UpdatePushTokenAsync(); resultDelegate(LoginResult.Ok, userData.Item2); } else { ClearTokens(); resultDelegate(LoginResult.Failed, userData.Item2); } } else { ClearTokens(); resultDelegate(loginResult.Item1, loginResult.Item2); } }); }
/// <summary> /// Tries to verify the saved token with the server. /// </summary> /// <param name="resultDelegate">Delegate that is called when operation is finished.</param> public void RestoreAsync(RestoreResultDelegate resultDelegate) { IsActive = false; if (_settings.AuthorizationTokenId > 0 && _settings.AuthorizationToken.Length > 0) { ApiRequest apiReq = new ApiRequest(_settings.AuthorizationTokenId, _settings.AuthorizationToken) { RequestBody = "{}" }; Task.Run(async () => { ApiRequest.ApiResponse apiResp = await apiReq.StartRequestAsync("verify_token"); if (apiResp.IsSuccess) { Tuple<UserData, string> userData = await GetUserData(); CurrentUserData = userData.Item1; IsActive = (CurrentUserData != null); } if (IsActive) { UpdatePushTokenAsync(); } else { ClearTokens(); } if (resultDelegate != null) resultDelegate(IsActive); }); } else { if (resultDelegate != null) Task.Run(() => resultDelegate(false)); } }