| [ProtoMember(2)] | [ProtoMember(2)] | ||||
| public string? Type { get; set; } | public string? Type { get; set; } | ||||
| [ProtoMember(3)] | |||||
| public string? Include_External { get; set; } | |||||
| [ProtoMember(4)] | |||||
| public int? Limit { get; set; } | |||||
| [ProtoMember(5)] | |||||
| public int? Offset { get; set; } | |||||
| } | } | ||||
| } | } |
| public string? Name { get; set; } | public string? Name { get; set; } | ||||
| [ProtoMember(5)] | [ProtoMember(5)] | ||||
| public DateTimeOffset? ReleaseDate { get; set; } | |||||
| public string? ReleaseDate { get; set; } | |||||
| [ProtoMember(6)] | [ProtoMember(6)] | ||||
| public int? TotalTracks { get; set; } | public int? TotalTracks { get; set; } |
| using Microsoft.Extensions.Options; | using Microsoft.Extensions.Options; | ||||
| using GrpcShared.DTO.Auth; | using GrpcShared.DTO.Auth; | ||||
| using SpotifyService.Services; | using SpotifyService.Services; | ||||
| using Blazored.SessionStorage; | |||||
| using Blazored.LocalStorage; | |||||
| var builder = WebApplication.CreateBuilder(args); | var builder = WebApplication.CreateBuilder(args); | ||||
| #if DEBUG | #if DEBUG | ||||
| builder.Services.AddGrpc(); | builder.Services.AddGrpc(); | ||||
| builder.Services.AddCodeFirstGrpc(); | builder.Services.AddCodeFirstGrpc(); | ||||
| builder.Services.AddCodeFirstGrpcReflection(); | builder.Services.AddCodeFirstGrpcReflection(); | ||||
| builder.Services.AddBlazoredSessionStorage(); | |||||
| builder.Services.AddBlazoredLocalStorage(); | |||||
| //call spotify api | //call spotify api | ||||
| builder.Services.AddHttpClient(); | builder.Services.AddHttpClient(); | ||||
| //using IdentityProvider.Protos.AuthService; | //using IdentityProvider.Protos.AuthService; | ||||
| using Blazored.SessionStorage; | |||||
| using Blazored.LocalStorage; | |||||
| using Grpc.Net.Client; | using Grpc.Net.Client; | ||||
| using GrpcShared; | using GrpcShared; | ||||
| using GrpcShared.DTO; | using GrpcShared.DTO; |
| </PropertyGroup> | </PropertyGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <PackageReference Include="Blazored.SessionStorage" Version="2.2.0" /> | |||||
| <PackageReference Include="Blazored.LocalStorage" Version="4.2.0" /> | |||||
| <PackageReference Include="Grpc.Net.Client" Version="2.47.0" /> | <PackageReference Include="Grpc.Net.Client" Version="2.47.0" /> | ||||
| <PackageReference Include="Grpc.Net.Client.Web" Version="2.47.0" /> | <PackageReference Include="Grpc.Net.Client.Web" Version="2.47.0" /> | ||||
| <PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="6.0.8" /> | <PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="6.0.8" /> |
| @using NemAnBlazor.Services.Interfaces | @using NemAnBlazor.Services.Interfaces | ||||
| @inject NavigationManager NavigationMgr | @inject NavigationManager NavigationMgr | ||||
| @inject IAuthClientService AuthService | @inject IAuthClientService AuthService | ||||
| @inject Blazored.SessionStorage.ISessionStorageService sessionStorage | |||||
| @inject Blazored.LocalStorage.ILocalStorageService localStorage | |||||
| <PageTitle>Redirecting...</PageTitle> | <PageTitle>Redirecting...</PageTitle> | ||||
| //if (response.access_token == null) NavigationMgr.NavigateTo("/"); | //if (response.access_token == null) NavigationMgr.NavigateTo("/"); | ||||
| //store access token in local storage | //store access token in local storage | ||||
| await sessionStorage.SetItemAsync("token", response.AccessToken); | |||||
| await sessionStorage.SetItemAsync("refresh_token", response.RefreshToken); | |||||
| await localStorage.SetItemAsync("token", response.AccessToken); | |||||
| await localStorage.SetItemAsync("refresh_token", response.RefreshToken); | |||||
| //redirect to home | //redirect to home | ||||
| NavigationMgr.NavigateTo("/home"); | NavigationMgr.NavigateTo("/home"); |
| @using NemAnBlazor.Services.Interfaces | @using NemAnBlazor.Services.Interfaces | ||||
| @using System.Diagnostics | @using System.Diagnostics | ||||
| @*@inject HttpClient Http*@ | @*@inject HttpClient Http*@ | ||||
| @inject Blazored.SessionStorage.ISessionStorageService sessionStorage | |||||
| @inject Blazored.LocalStorage.ILocalStorageService localStorage | |||||
| @inject ITrackClientService SearchService | @inject ITrackClientService SearchService | ||||
| @inject IAuthClientService AuthService | @inject IAuthClientService AuthService | ||||
| private async Task Click() | private async Task Click() | ||||
| { | { | ||||
| var token = await sessionStorage.GetItemAsync<string>("token"); | |||||
| var token = await localStorage.GetItemAsync<string>("token"); | |||||
| TokenMessage tm = new() { Token = token }; | TokenMessage tm = new() { Token = token }; | ||||
| SearchRequest request = new() { Query = "aitch", Type = "track", Token = token }; | SearchRequest request = new() { Query = "aitch", Type = "track", Token = token }; | ||||
| SearchResponse searchResponse = await SearchService.GetListSearchAsync(request); | SearchResponse searchResponse = await SearchService.GetListSearchAsync(request); |
| @using GrpcShared.DTO | @using GrpcShared.DTO | ||||
| @using GrpcShared.DTO.Track | @using GrpcShared.DTO.Track | ||||
| @using NemAnBlazor.Services.Interfaces | @using NemAnBlazor.Services.Interfaces | ||||
| @inject Blazored.SessionStorage.ISessionStorageService sessionStorage | |||||
| @inject Blazored.LocalStorage.ILocalStorageService localStorage | |||||
| @inject IStatsClientService spotifyService | @inject IStatsClientService spotifyService | ||||
| @inject ITrackClientService trackService | @inject ITrackClientService trackService | ||||
| @code { | @code { | ||||
| protected override async Task OnInitializedAsync() | protected override async Task OnInitializedAsync() | ||||
| { | { | ||||
| string tokenS = await sessionStorage.GetItemAsync<string>("token"); | |||||
| string tokenS = await localStorage.GetItemAsync<string>("token"); | |||||
| TokenMessage token = new TokenMessage{Token = tokenS}; | TokenMessage token = new TokenMessage{Token = tokenS}; | ||||
| CurrentTrackResponse response = await spotifyService.GetCurrentlyPlayingTrack(token); | CurrentTrackResponse response = await spotifyService.GetCurrentlyPlayingTrack(token); |
| global using Microsoft.AspNetCore.Components.Authorization; | global using Microsoft.AspNetCore.Components.Authorization; | ||||
| using Blazored.SessionStorage; | |||||
| using Blazored.LocalStorage; | |||||
| using Grpc.Net.Client; | using Grpc.Net.Client; | ||||
| using Grpc.Net.Client.Web; | using Grpc.Net.Client.Web; | ||||
| using Microsoft.AspNetCore.Components; | using Microsoft.AspNetCore.Components; | ||||
| builder.Services.AddScoped<ITrackClientService, TrackClientService>(); | builder.Services.AddScoped<ITrackClientService, TrackClientService>(); | ||||
| builder.Services.AddScoped<IAuthClientService, AuthClientService>(); | builder.Services.AddScoped<IAuthClientService, AuthClientService>(); | ||||
| builder.Services.AddScoped<IStatsClientService, StatsClientService>(); | builder.Services.AddScoped<IStatsClientService, StatsClientService>(); | ||||
| builder.Services.AddBlazoredSessionStorage(); | |||||
| builder.Services.AddBlazoredLocalStorage(); | |||||
| builder.Services.AddScoped<AuthenticationStateProvider, AuthClientService>(); | builder.Services.AddScoped<AuthenticationStateProvider, AuthClientService>(); | ||||
| builder.Services.AddAuthorizationCore(); | builder.Services.AddAuthorizationCore(); | ||||
| using GrpcShared.DTO; | using GrpcShared.DTO; | ||||
| using System.Security.Claims; | using System.Security.Claims; | ||||
| using Microsoft.AspNetCore.Components.Authorization; | using Microsoft.AspNetCore.Components.Authorization; | ||||
| using Blazored.SessionStorage; | |||||
| using Blazored.LocalStorage; | |||||
| namespace NemAnBlazor.Services | namespace NemAnBlazor.Services | ||||
| { | { | ||||
| { | { | ||||
| private IAuthService _serviceClient; | private IAuthService _serviceClient; | ||||
| private ISessionStorageService _sessionStorage; | |||||
| public AuthClientService(GrpcChannel grpcChannel, ISessionStorageService sessionStorage) | |||||
| private ILocalStorageService _sessionStorage; | |||||
| public AuthClientService(GrpcChannel grpcChannel, ILocalStorageService sessionStorage) | |||||
| { | { | ||||
| _serviceClient = grpcChannel.CreateGrpcService<IAuthService>(); | _serviceClient = grpcChannel.CreateGrpcService<IAuthService>(); | ||||
| _sessionStorage = sessionStorage; | _sessionStorage = sessionStorage; |
| return response; | return response; | ||||
| } | } | ||||
| public static async Task PutData(HttpClient client, string url, string token) | |||||
| { | |||||
| //add header | |||||
| client.DefaultRequestHeaders.Add(HeaderNames.Authorization, "Bearer " + token); | |||||
| //get request | |||||
| await client.PutAsync(url, null); | |||||
| } | |||||
| } | } | ||||
| } | } |
| { | { | ||||
| var client = _httpClientFactory.CreateClient("HttpClient"); | var client = _httpClientFactory.CreateClient("HttpClient"); | ||||
| client.DefaultRequestHeaders.Add(HeaderNames.Authorization, "Bearer " + request.Token); | |||||
| var param = new Dictionary<string, List<string>>(); | var param = new Dictionary<string, List<string>>(); | ||||
| param["ids"] = request.Ids!; | param["ids"] = request.Ids!; | ||||
| var query = UriUtil(param); | var query = UriUtil(param); | ||||
| string url = $"me/tracks/{query}"; | |||||
| await client.PutAsync($"me/tracks/{query}", null); | |||||
| //the response type has nothing to do with the method, it's there so that the method can be called | |||||
| await HttpUtils.HttpUtils<MultipleTrackResponse>.PutData(client, url, request.Token!); | |||||
| } | } | ||||
| public static string UriUtil(Dictionary<string, List<string>> param) | public static string UriUtil(Dictionary<string, List<string>> param) | ||||
| { | { |