Blazor & WASM in combination to get statistics from Spotify API for performing the song analysis. With separate microservices for auth, Spotify, user data tracking, and application, connected through gRPC with Polly.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AuthClientService.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Grpc.Net.Client;
  2. using GrpcShared.DTO.Auth;
  3. using GrpcShared.Interfaces;
  4. using NemAnBlazor.Services.Interfaces;
  5. using ProtoBuf.Grpc.Client;
  6. using GrpcShared;
  7. using GrpcShared.DTO.User;
  8. using GrpcShared.DTO;
  9. using System.Security.Claims;
  10. using Microsoft.AspNetCore.Components.Authorization;
  11. using Blazored.LocalStorage;
  12. using GrpcShared.DTO.Db;
  13. namespace NemAnBlazor.Services
  14. {
  15. public class AuthClientService : AuthenticationStateProvider, IAuthClientService
  16. {
  17. private IAuthService _serviceClient;
  18. private ILocalStorageService _localStorage;
  19. public AuthClientService(GrpcChannel grpcChannel, ILocalStorageService sessionStorage)
  20. {
  21. _serviceClient = grpcChannel.CreateGrpcService<IAuthService>();
  22. _localStorage = sessionStorage;
  23. }
  24. public async Task<TokenResponse> GetAccessToken(TokenRequest request)
  25. {
  26. return await _serviceClient.GetAccessToken(request);
  27. }
  28. //public override async Task<AuthenticationState> GetAuthenticationStateAsync()
  29. //{
  30. // string token = await _localStorage.GetItemAsync<string>("token");
  31. // token = "BQBMgFm6jnFNWWeZEMGIRP_f-ENPid7Kw8JubAyuWAe4JK0S1DPFGlaAdZ_Fey6ePkCnz8-cqC0oyRmrciWUy5ISUTQKDe8PTQn4iBRMYCgM0n4GnS1xAErHJcm4Vpu2TAngk-4vQUOfTQRcedNTfCaHKP4uFJgTlTI7JHGrtB-_EZLnFcZ2OQe31oFQIJ1wM3ZtvwnN";
  32. // if (token == null) return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
  33. // var userInfo = await _serviceClient.GetUserInfo(new TokenMessage { Token = token });
  34. // List<Claim> claims = new();
  35. // claims.Add(new Claim("email", userInfo.email!));
  36. // claims.Add(new Claim("id", userInfo.id!));
  37. // claims.Add(new Claim("name", userInfo.display_name!));
  38. // ClaimsIdentity identity = new(claims, "jwt");
  39. // ClaimsIdentity identity = new();
  40. // ClaimsPrincipal user = new(identity);
  41. // AuthenticationState state = new(user);
  42. // NotifyAuthenticationStateChanged(Task.FromResult(state));
  43. // return state;
  44. //}
  45. public async Task<CodeRequest> GetAuthParams()
  46. {
  47. return await _serviceClient.GetAuthParams();
  48. }
  49. public async Task<UserInfoResponse> GetUserInfo(UserResponse token)
  50. {
  51. return await _serviceClient.GetUserInfo(token);
  52. }
  53. public override async Task<AuthenticationState> GetAuthenticationStateAsync()
  54. {
  55. //return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
  56. await Task.Delay(500);
  57. string userId = await _localStorage.GetItemAsync<string>("user_info");
  58. //token = "BQBMgFm6jnFNWWeZEMGIRP_f-ENPid7Kw8JubAyuWAe4JK0S1DPFGlaAdZ_Fey6ePkCnz8-cqC0oyRmrciWUy5ISUTQKDe8PTQn4iBRMYCgM0n4GnS1xAErHJcm4Vpu2TAngk-4vQUOfTQRcedNTfCaHKP4uFJgTlTI7JHGrtB-_EZLnFcZ2OQe31oFQIJ1wM3ZtvwnN";
  59. if (userId == null) return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
  60. // var userInfo = await _serviceClient.GetUserInfo(new UserResponse { Token = token, RefreshToken = refreshT });
  61. List<Claim> claims = new();
  62. //claims.Add(new Claim("email", userInfo.Email!));
  63. claims.Add(new Claim("id", userId!));
  64. //claims.Add(new Claim("name", userInfo.DisplayName!));
  65. ClaimsIdentity identity = new(claims, "jwt");
  66. //ClaimsIdentity identity = new();
  67. ClaimsPrincipal user = new(identity);
  68. AuthenticationState state = new(user);
  69. // NotifyAuthenticationStateChanged(Task.FromResult(state));
  70. return state;
  71. }
  72. public async Task<RefreshTokenResponse> RefreshAccessToken(UserResponse token)
  73. {
  74. return await _serviceClient.RefreshAccessToken(token);
  75. }
  76. }
  77. }