public override async Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            string authToken = await _localStorageService.GetItemAsStringAsync("authToken");

            var identity = new ClaimsIdentity();

            _http.DefaultRequestHeaders.Authorization = null;

            if (!string.IsNullOrEmpty(authToken))
            {
                try
                {
                    identity = new ClaimsIdentity(ParseClaimsFromJwt(authToken), "jwt");
                    _http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
                    await _bananaService.GetBananas();
                }
                catch (Exception)
                {
                    await _localStorageService.RemoveItemAsync("authToken");

                    identity = new ClaimsIdentity();
                }
            }

            var user  = new ClaimsPrincipal(identity);
            var state = new AuthenticationState(user);


            NotifyAuthenticationStateChanged(Task.FromResult(state));
            return(state);
        }
Beispiel #2
0
        public async Task AddUnit(int unitId)
        {
            Unit unit   = Units.First(unit => unit.Id == unitId);
            var  result = await _http.PostAsJsonAsync <int>("api/UserUnit", unitId);

            if (result.StatusCode != System.Net.HttpStatusCode.OK)
            {
                _toastService.ShowError(await result.Content.ReadAsStringAsync());
            }
            else
            {
                await _bananaService.GetBananas();

                _toastService.ShowSuccess($"Your {unit.Title} has been built!", "Unit built!");
            }
        }
Beispiel #3
0
        public async Task AddUnit(int unitId)
        {
            var unit = Units.FirstOrDefault(x => x.Id == unitId);

            if (unit == null)
            {
                return;
            }
            var res = await _http.PostAsJsonAsync <int>("api/UserUnit", unitId);

            if (res.StatusCode != HttpStatusCode.OK)
            {
                _toastService.ShowError(await res.Content.ReadAsStringAsync());
            }
            else
            {
                await _bananaService.GetBananas();

                _toastService.ShowSuccess($"Your {unit.Title} has been built!", "Unit built!");
            }
        }
        public override async Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            var identity = new ClaimsIdentity();

            _http.DefaultRequestHeaders.Authorization = null;

            var authToken = await _localStorageService.GetItemAsStringAsync("authToken");

            if (!string.IsNullOrEmpty(authToken))
            {
                try
                {
                    identity = new ClaimsIdentity(ParseClaimsFromJwt(authToken), "jwt");
                    _http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
                    await _bananaService.GetBananas();
                }
                catch (Exception)
                {
                    await _localStorageService.RemoveItemAsync("authToken");

                    identity = new ClaimsIdentity();
                }
            }

            var user  = new ClaimsPrincipal(identity);
            var state = new AuthenticationState(user);

            NotifyAuthenticationStateChanged(Task.FromResult(state));

            return(state);

            /*
             * var state = new AuthenticationState(new ClaimsPrincipal());
             *
             * if (await _localStorageService.GetItemAsync<bool>("isAuthenticated"))
             * {
             *  var identity = new ClaimsIdentity(
             *  new []
             *  {
             *      new Claim(ClaimTypes.Name, "Georg")
             *  }, "Test auth type");
             * var user = new ClaimsPrincipal(identity);
             * state = new AuthenticationState(user);
             *
             * NotifyAuthenticationStateChanged(Task.FromResult(state));
             * return state;
             * }
             *
             * NotifyAuthenticationStateChanged(Task.FromResult(state));
             * return state;
             */
        }