|
1 | 1 | using Microsoft.AspNetCore.Components; |
2 | 2 | using Microsoft.AspNetCore.Components.Web; |
3 | | -using Microsoft.AspNetCore.Identity; |
4 | 3 |
|
5 | 4 | namespace WingtipToys.Account |
6 | 5 | { |
7 | 6 | public partial class Login : ComponentBase |
8 | 7 | { |
9 | | - [Inject] private SignInManager<IdentityUser> SignInManager { get; set; } = default!; |
10 | 8 | [Inject] private NavigationManager Navigation { get; set; } = default!; |
11 | 9 |
|
| 10 | + [SupplyParameterFromQuery(Name = "error")] |
| 11 | + public string? ErrorParam { get; set; } |
| 12 | + |
12 | 13 | private string email = ""; |
13 | 14 | private string password = ""; |
14 | 15 | private string errorMessage = ""; |
15 | 16 |
|
16 | | - private async Task HandleLogin(MouseEventArgs args) |
| 17 | + protected override void OnParametersSet() |
| 18 | + { |
| 19 | + if (!string.IsNullOrEmpty(ErrorParam)) |
| 20 | + { |
| 21 | + errorMessage = ErrorParam; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + private void HandleLogin(MouseEventArgs args) |
17 | 26 | { |
18 | 27 | if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password)) |
19 | 28 | { |
20 | 29 | errorMessage = "Email and password are required."; |
21 | 30 | return; |
22 | 31 | } |
23 | 32 |
|
24 | | - var result = await SignInManager.PasswordSignInAsync(email, password, isPersistent: false, lockoutOnFailure: false); |
25 | | - |
26 | | - if (result.Succeeded) |
27 | | - { |
28 | | - Navigation.NavigateTo("/", forceLoad: true); |
29 | | - } |
30 | | - else if (result.IsLockedOut) |
31 | | - { |
32 | | - Navigation.NavigateTo("/Account/Lockout"); |
33 | | - } |
34 | | - else |
35 | | - { |
36 | | - errorMessage = "Invalid login attempt."; |
37 | | - } |
| 33 | + // Navigate to HTTP endpoint so SignInManager can set cookies via the HTTP response |
| 34 | + var loginUrl = $"/Account/PerformLogin?email={Uri.EscapeDataString(email)}&password={Uri.EscapeDataString(password)}"; |
| 35 | + Navigation.NavigateTo(loginUrl, forceLoad: true); |
38 | 36 | } |
39 | 37 | } |
40 | 38 | } |
0 commit comments