updated service to save and delete json token
This commit is contained in:
parent
f70d3e9994
commit
fba938fa2c
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Blazor-ApexCharts" Version="0.9.20-beta" />
|
<PackageReference Include="Blazor-ApexCharts" Version="0.9.20-beta" />
|
||||||
|
<PackageReference Include="Blazored.LocalStorage" Version="4.3.0" />
|
||||||
<PackageReference Include="xunit" Version="2.4.2" />
|
<PackageReference Include="xunit" Version="2.4.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
using Microsoft.AspNetCore.Components;
|
using Microsoft.AspNetCore.Components;
|
||||||
using Microsoft.AspNetCore.Components.Web;
|
using Microsoft.AspNetCore.Components.Web;
|
||||||
using PanoptesFrontend.Services;
|
using PanoptesFrontend.Services;
|
||||||
|
using Blazored.LocalStorage;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
@ -8,8 +9,9 @@ var builder = WebApplication.CreateBuilder(args);
|
|||||||
builder.Services.AddRazorPages();
|
builder.Services.AddRazorPages();
|
||||||
builder.Services.AddServerSideBlazor();
|
builder.Services.AddServerSideBlazor();
|
||||||
builder.Services.AddHttpClient();
|
builder.Services.AddHttpClient();
|
||||||
builder.Services.AddSingleton<IHttpService, HttpService>();
|
builder.Services.AddScoped<IHttpService, HttpService>();
|
||||||
builder.Services.AddSingleton<IAccountService, AccountService>();
|
builder.Services.AddScoped<IAccountService, AccountService>();
|
||||||
|
builder.Services.AddBlazoredLocalStorage();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
using PanoptesFrontend.Data.Account;
|
using PanoptesFrontend.Data.Account;
|
||||||
using PanoptesFrontend.Data;
|
using PanoptesFrontend.Data;
|
||||||
using Microsoft.AspNetCore.Components;
|
using Blazored.LocalStorage;
|
||||||
using System.Collections.Generic;
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace PanoptesFrontend.Services;
|
namespace PanoptesFrontend.Services;
|
||||||
|
|
||||||
@ -17,24 +16,30 @@ public interface IAccountService
|
|||||||
public class AccountService : IAccountService
|
public class AccountService : IAccountService
|
||||||
{
|
{
|
||||||
private IHttpService httpService;
|
private IHttpService httpService;
|
||||||
|
private ILocalStorageService localStorage;
|
||||||
|
|
||||||
public AccountService(IHttpService httpService) {
|
public AccountService(IHttpService httpService, ILocalStorageService localStorage) {
|
||||||
this.httpService = httpService;
|
this.httpService = httpService;
|
||||||
|
this.localStorage = localStorage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task Register(AddUser model){
|
public async Task Register(AddUser model){
|
||||||
// endpoint doesnt exist yet
|
|
||||||
await httpService.PostAsync("http://localhost:10000/user/register", model);
|
await httpService.PostAsync("http://localhost:10000/user/register", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Login(LoginUser model){
|
public async Task Login(LoginUser model){
|
||||||
// endpoint doesnt exist yet
|
var response = await httpService.PostAsync("http://localhost:10000/user/login", model);
|
||||||
await httpService.PostAsync("http://localhost:10000/user/login", model);
|
|
||||||
|
var jsonDoc = JsonDocument.Parse(response);
|
||||||
|
var token = jsonDoc.RootElement.GetProperty("token").GetString();
|
||||||
|
|
||||||
|
// Save the JWT token to local storage
|
||||||
|
await localStorage.SetItemAsync("authToken", token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Logout(User model){
|
public async Task Logout(User model){
|
||||||
// endpoint doesnt exist yet
|
|
||||||
await httpService.PostAsync("http://localhost:10000/user/logout", model);
|
await httpService.PostAsync("http://localhost:10000/user/logout", model);
|
||||||
|
await localStorage.RemoveItemAsync("authToken");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -8,7 +8,7 @@ using System.Text.Json;
|
|||||||
public interface IHttpService
|
public interface IHttpService
|
||||||
{
|
{
|
||||||
Task<T> GetAsync<T>(string endpoint);
|
Task<T> GetAsync<T>(string endpoint);
|
||||||
Task<HttpStatusCode> PostAsync(string endpoint, object value);
|
Task<string> PostAsync(string endpoint, object value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class HttpService : IHttpService {
|
public class HttpService : IHttpService {
|
||||||
@ -32,11 +32,9 @@ public class HttpService : IHttpService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<HttpStatusCode> PostAsync(string endpoint, object value){
|
public async Task<string> PostAsync(string endpoint, object value){
|
||||||
var request = await httpClient.PostAsJsonAsync(endpoint, value);
|
var response = await httpClient.PostAsJsonAsync(endpoint, value);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
request.EnsureSuccessStatusCode();
|
return await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
return request.StatusCode;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user