Skip to content

Commit 74d7602

Browse files
committed
Trying to fix the logout.
1 parent 73b1e45 commit 74d7602

7 files changed

Lines changed: 22 additions & 28 deletions

File tree

CashFlowAnalyzer.Client/Layout/MainLayout.razor

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
@inherits LayoutComponentBase
2-
@* Issue: Cannot provide a value for property 'AccountService' on type
3-
'CashFlowAnalyzer.Client.Layout.MainLayout'. There is no registered
4-
service of type 'CashFlowAnalyzer.Client.Services.AccountService'. *@
52
@inject NavigationManager Navigation
63
@inject IAccountService AccountService
7-
@inject AuthenticationInfoProvider AuthenticationInfoProvider
84

95
<div class="page">
106
<div class="sidebar">
@@ -37,9 +33,10 @@ service of type 'CashFlowAnalyzer.Client.Services.AccountService'. *@
3733

3834
protected override async Task OnAfterRenderAsync(bool firstRender)
3935
{
36+
// issue: this does not get triggered if we redirected from login page or it's the server rendering
4037
if (firstRender)
4138
{
42-
isAuthenticated = await AuthenticationInfoProvider.IsAuthenticated();
39+
isAuthenticated = await AccountService.IsAuthenticated();
4340
if (isAuthenticated)
4441
{
4542
StateHasChanged();
@@ -51,6 +48,7 @@ service of type 'CashFlowAnalyzer.Client.Services.AccountService'. *@
5148
private async Task Logout()
5249
{
5350
await AccountService.Logout();
51+
isAuthenticated = false;
5452
Navigation.NavigateTo("/Login");
5553
}
5654
}

CashFlowAnalyzer.Client/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
builder.Services.AddScoped<IAccountService, AccountService>();
1616
builder.Services.AddScoped<SpreadsheetReader>();
1717
builder.Services.AddScoped<IFinancialDataService, FinancialDataService>();
18-
builder.Services.AddScoped<AuthenticationInfoProvider>();
1918
builder.Services.AddBlazorBootstrap();
2019

2120
// Supply HttpClient instances that include access tokens when making requests to the server project

CashFlowAnalyzer.Client/Services/Account/AccountService.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,25 @@ public async Task<AccountServiceResult> Register(RegisterModel model)
4040
return await SendRequest(request);
4141
}
4242

43+
public async Task<bool> IsAuthenticated()
44+
{
45+
var response = await _http.GetAsync($"{baseAddress}/api/auth/isAuthenticated");
46+
if (response.IsSuccessStatusCode)
47+
{
48+
return await response.Content.ReadFromJsonAsync<bool>();
49+
}
50+
return false;
51+
}
52+
4353
private async Task<AccountServiceResult> SendRequest(HttpRequestMessage request)
4454
{
4555
using var response = await _http.SendAsync(request);
4656
if (response.StatusCode == HttpStatusCode.OK)
4757
{
48-
_log.LogInformation("Registration was successful");
58+
_log.LogInformation("Request was successful");
4959
return new AccountServiceResult() { Success = true };
5060
}
51-
_log.LogWarning("Registration failed");
61+
_log.LogWarning("Request failed");
5262
var errors = await response.Content.ReadFromJsonAsync<List<string>>();
5363
return new AccountServiceResult() { Success = false, Errors = errors };
5464
}

CashFlowAnalyzer.Client/Services/Account/AuthenticationInfoProvider.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

CashFlowAnalyzer.Client/Services/Account/IAccountService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public interface IAccountService
77
Task<AccountServiceResult> Login(LoginModel model);
88
Task<AccountServiceResult> Logout();
99
Task<AccountServiceResult> Register(RegisterModel model);
10+
Task<bool> IsAuthenticated();
1011
}

CashFlowAnalyzer/Controllers/AuthController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public AuthController(SignInManager<ApplicationUser> signInManager, UserManager<
1717
_userManager = userManager;
1818
}
1919

20+
[HttpGet("isAuthenticated")]
21+
public IActionResult IsAuthenticated()
22+
{
23+
return Ok(User.Identity.IsAuthenticated);
24+
}
25+
2026
[HttpPost("login")]
2127
public async Task<IActionResult> Login([FromBody] LoginModel model)
2228
{

CashFlowAnalyzer/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
builder.Services.AddScoped<IAccountService, AccountService>();
1313
builder.Services.AddScoped<SpreadsheetReader>();
1414
builder.Services.AddScoped<IFinancialDataService, FinancialDataService>();
15-
builder.Services.AddScoped<AuthenticationInfoProvider>();
1615
builder.Services.AddScoped<DatabaseService>();
1716

1817
// Add services to the container.

0 commit comments

Comments
 (0)