123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using Blazored.LocalStorage;
- using Microsoft.AspNetCore.Components.Authorization;
- using System.Net.Http;
- using System.Net.Http.Json;
- using System.Threading.Tasks;
- using wispro.sp.web.Auth;
- using wispro.sp.web.Models;
- using wispro.sp.web.Utils;
- using System.Security.Claims;
- using System.Collections.Generic;
- using wispro.sp.entity;
- using System;
- namespace wispro.sp.web.Services
- {
- public interface IUserService
- {
- CurrentUser CurrentUser { get; set; }
- Task<List<Staff>> GetAll();
- }
- public class UserService : IUserService
- {
- private readonly HttpClient _httpClient;
- private readonly JwtAuthenticationStateProvider _jwt;
- private readonly ILocalStorageService _localStorageService;
- private CurrentUser _CurrentUser= new CurrentUser() { Name = "" };
- public UserService(HttpClient httpClient, ILocalStorageService localStorageService, AuthenticationStateProvider jwt)
- {
- _httpClient = httpClient;
- _localStorageService = localStorageService;
- _jwt = (JwtAuthenticationStateProvider)jwt;
- }
- public CurrentUser CurrentUser
- {
- get
- {
- return _CurrentUser;
- }
- set
- {
- _CurrentUser = value;
- }
-
- }
- public async Task<CurrentUser> GetUser()
- {
- CurrentUser _user = new CurrentUser();
- var tokenInLocalStorage = await _localStorageService.GetItemAsStringAsync("authToken");
- var claims = JwtParser.ParseClaimsFromJwt(tokenInLocalStorage);
- foreach (Claim claim in claims)
- {
- if (claim.Type == ClaimTypes.Name )
- {
- _user.Name = claim.Value;
- break;
- }
- }
- return _user;
- }
- public async Task<List<Staff>> GetAll()
- {
- try
- {
- var _StaffGrade = await _httpClient.GetFromJsonAsync<List<Staff>>($"http://localhost:39476/api/Staff/GetAll");
- return _StaffGrade;
- }
- catch (Exception ex)
- {
- if (ex.Message.Contains("Unauthorized"))
- {
- _jwt.NotifyUserLogOut();
- }
- return null;
- }
- }
-
- }
- }
|