Ejemplo n.º 1
0
 private bool RefreshTime(long refreshTime)
 {
     return(AuthHelpers.GetUnixTime(DateTime.Now) > refreshTime);
 }
Ejemplo n.º 2
0
        public async Task Login()
        {
            Result loginResult = new Result();

            IsLoading = true;
            try
            {
                loginResult = await _authManager.SendLoginData(UserName, Password);
            }
            catch (Exception ex)
            {
                loginResult.IsSuccess  = false;
                loginResult.ResultJson = ex.Message;
            }
            var loader = new Windows.ApplicationModel.Resources.ResourceLoader();

            if (!loginResult.IsSuccess)
            {
                loginResult.ResultJson = loader.GetString("LoginError/Text");
            }
            else if (loginResult.IsSuccess)
            {
                try
                {
                    var authTokens    = JsonConvert.DeserializeObject <Tokens>(loginResult.Tokens);
                    var expiresInDate = AuthHelpers.GetUnixTime(DateTime.Now) + authTokens.ExpiresIn;
                    if (!string.IsNullOrEmpty(authTokens.AccessToken) && !string.IsNullOrEmpty(authTokens.RefreshToken))
                    {
                        var loginUserResult =
                            await
                            _authManager.GetUserEntity(new UserAuthenticationEntity(authTokens.AccessToken, authTokens.RefreshToken, expiresInDate), "ja");

                        var loginUser  = JsonConvert.DeserializeObject <LogInUser>(loginUserResult.ResultJson);
                        var userResult =
                            await
                            _userManager.GetUser(loginUser.OnlineId,
                                                 new UserAuthenticationEntity(authTokens.AccessToken, authTokens.RefreshToken,
                                                                              expiresInDate), loginUser.Region, loginUser.Language);

                        var user             = JsonConvert.DeserializeObject <User>(userResult.ResultJson);
                        var newAccountResult = await AccountAuthHelpers.CreateUserAccount(authTokens, loginUser, user);

                        if (!newAccountResult)
                        {
                            loginResult.IsSuccess = false;
                            loginResult.Error     = "Failed to create new user in database.";
                        }
                    }
                }
                catch (Exception ex)
                {
                    loginResult.IsSuccess  = false;
                    loginResult.ResultJson = ex.Message;
                }
            }

            // Check if the result was good. If not, show error.
            await ResultChecker.CheckSuccess(loginResult);

            IsLoading = false;
            if (loginResult.IsSuccess)
            {
                NavigationService.Navigate(typeof(AccountPage));
            }
        }