Skip to content

Commit e63b9de

Browse files
Add a message for login failed and a couple comments
1 parent 2ab87fd commit e63b9de

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

BlazorWasmOsmOauth/Layout/MainLayout.razor.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public partial class MainLayout(AppConfig config, NavigationManager navManager,
1818

1919
private async Task GoToLogin()
2020
{
21+
// Note: This is a simple example, in a real application you'd probably want
22+
// to use a more secure method of generating these values.
23+
2124
Guid state = Guid.NewGuid();
2225
await localStorageService.SetItemAsync(LocalStorageService.OsmStateKey, state, CancellationToken.None);
2326

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
@page "/oauth2-redirect"
22

33
<PageTitle>Auth Complete</PageTitle>
4+
5+
<!-- Nothing to really display here, the code-behind for this page will handle everything, then redirect to home -->

BlazorWasmOsmOauth/Pages/AuthCompletePage.razor.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,8 @@ private async Task GetToken()
5252
return;
5353
}
5454

55-
if (State == null
56-
|| State != await localStorageService.GetItemAsync<Guid>(LocalStorageService.OsmStateKey, CancellationToken.None))
55+
if (!await ValidateState())
5756
{
58-
// Wait for the dialog to close, then redirect to the home page
59-
IDialogReference dialogReference = await dialogService.ShowErrorAsync("The state value supplied by OSM is invalid.", title: "Invalid State");
60-
await dialogReference.Result;
61-
6257
navManager.NavigateTo("/");
6358
return;
6459
}
@@ -77,11 +72,36 @@ private async Task GetToken()
7772

7873
TokenResponse? tokenResp = await osmClient.GetTokenAsync(Code, config.RedirectUri, config.ClientId, pkce);
7974

80-
if (tokenResp != null)
75+
if (tokenResp == null)
8176
{
82-
await authenticationStateProvider.SetCurrentUserAsync(tokenResp, CancellationToken.None);
77+
// Wait for the dialog to close, then redirect to the home page
78+
IDialogReference dialogReference = await dialogService.ShowErrorAsync("The OSM login failed.", title: "Login failed");
79+
await dialogReference.Result;
80+
81+
navManager.NavigateTo("/");
82+
return;
8383
}
8484

85+
await authenticationStateProvider.SetCurrentUserAsync(tokenResp, CancellationToken.None);
86+
8587
navManager.NavigateTo("/");
8688
}
89+
90+
private async Task<bool> ValidateState()
91+
{
92+
Guid? stateFromStorage = await localStorageService.GetItemAsync<Guid>(LocalStorageService.OsmStateKey, CancellationToken.None);
93+
if (State == null
94+
|| stateFromStorage == null
95+
|| State != stateFromStorage)
96+
{
97+
// Wait for the dialog to close, then return
98+
IDialogReference dialogReference = await dialogService.ShowErrorAsync("The state value supplied by OSM is invalid.", title: "Invalid State");
99+
await dialogReference.Result;
100+
101+
102+
return false;
103+
}
104+
105+
return true;
106+
}
87107
}

BlazorWasmOsmOauth/Pages/HomePage.razor

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
<PageTitle>Blazor WASM OSM Oauth2 Example</PageTitle>
44

5-
65
<AuthorizeView>
76
<Authorized>
87
<div>

BlazorWasmOsmOauth/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public static async Task Main(string[] args)
5858
config.RedirectUri = $"http{(thisHost == "127.0.0.1:8123" ? string.Empty : "s")}://{thisHost}/oauth2-redirect";
5959

6060
config.SourceRevisionId = Assembly.GetEntryAssembly()?
61-
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
62-
// For example: 0.0.1+b9d1873a
61+
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
62+
// For example: 0.0.1+b9d1873a
6363
?.InformationalVersion.Split('+')[1] ?? "ERROR";
6464

6565
builder.Services.AddSingleton(config);

0 commit comments

Comments
 (0)