public IndexViewModel( ProtectedLocalStorage localStorageService, GameService gameService ) { _localStorageService = localStorageService; _gameService = gameService; Debug.WriteLine($"{nameof(IndexViewModel)} #{m_Index} .ctor"); string[] filterWords() => Words.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray(); this.WhenActivated(context => { Debug.WriteLine($"{nameof(IndexViewModel)} #{m_Index} Activating"); // local storage this.WhenAnyValue(x => x.Username) .Skip(1) // init .Do(x => _localStorageService.SetAsync(nameof(Username), x ?? string.Empty)) .Subscribe() .DisposeWith(context); this.WhenAnyValue(x => x.IsLoggedIn) .Skip(1) // init .Do(x => _localStorageService.SetAsync(nameof(IsLoggedIn), x)) .Subscribe() .DisposeWith(context); Words.ObserveCollectionChanges() .Skip(1) // init .Do(x => _localStorageService.SetAsync(nameof(Words), filterWords())) .Subscribe() .DisposeWith(context); this.WhenAnyValue(x => x.Player) .Select(x => x != null) .BindTo(this, x => x.IsLoggedIn) .DisposeWith(context); this.WhenAnyValue(x => x.Player) .Select(x => x != null ? x.GetConnectionToken() : Disposable.Empty) .DisposeMany() .Subscribe() .DisposeWith(context); this.WhenAnyValue(x => x.Player) .Where(x => x != null) .Select(x => x.GetWordsInteraction.RegisterHandler(interactionContext => interactionContext.SetOutput(filterWords()) ) ) .DisposeMany() .Subscribe() .DisposeWith(context); Disposable.Create(() => Debug.WriteLine($"{nameof(IndexViewModel)} #{m_Index} Disposing")) .DisposeWith(context); }); }
public async Task MarkUserAsAuthenticated(string token) { _token = token; if (_accessor.IsServerStarted()) { await _storage.SetAsync(TokenKey, token); } SetAuthenticationState(GetAuthenticationStateAsync()); }
public async Task RefreshToken() { if (string.IsNullOrEmpty(_tokenProvider.RefreshToken)) { return; } var httpClient = _httpClientFactory.CreateClient(); var metaDataResponse = await httpClient.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest { Address = _options.Authority, Policy = { RequireHttps = _options.RequireHttpsMetadata }, }); var response = await httpClient.RequestRefreshTokenAsync(new RefreshTokenRequest { Address = metaDataResponse.TokenEndpoint, ClientId = _options.ClientId, ClientSecret = _options.ClientSecret, RefreshToken = _tokenProvider.RefreshToken, }); if (response.IsError) { if (response.HttpStatusCode == System.Net.HttpStatusCode.BadRequest) { return; } throw new Exception(response.Raw); } _tokenProvider.AccessToken = response.AccessToken; _tokenProvider.RefreshToken = response.RefreshToken; _tokenProvider.ExpiresAt = DateTime.UtcNow + TimeSpan.FromSeconds(response.ExpiresIn); await _protectedLocalStorage.SetAsync("tokens", _tokenProvider); }
public async Task SavePlayerIdAsync(int playerId) { await _preferenceStore.SetAsync(PreferredPlayerIdKey, playerId); }