Browse Source

Troca de senha.

Júlio César de Carvalho 4 months ago
parent
commit
d133a7a580

+ 7 - 8
ConcursoProjetos/Components/Account/Pages/ResetPassword.razor

@@ -9,10 +9,9 @@
 @inject IdentityRedirectManager RedirectManager
 @inject UserManager<ApplicationUser> UserManager
 
-<PageTitle>Reset password</PageTitle>
+<PageTitle>Troca de Senha</PageTitle>
 
-<h1>Reset password</h1>
-<h2>Reset your password.</h2>
+<h2>Troca de Senha</h2>
 <hr />
 <div class="row">
     <div class="col-md-4">
@@ -24,20 +23,20 @@
             <input type="hidden" name="Input.Code" value="@Input.Code" />
             <div class="form-floating mb-3">
                 <InputText @bind-Value="Input.Email" class="form-control" autocomplete="username" aria-required="true" placeholder="name@example.com" />
-                <label for="email" class="form-label">Email</label>
+                <label for="email" class="form-label">E-mail</label>
                 <ValidationMessage For="() => Input.Email" class="text-danger" />
             </div>
             <div class="form-floating mb-3">
-                <InputText type="password" @bind-Value="Input.Password" class="form-control" autocomplete="new-password" aria-required="true" placeholder="Please enter your password." />
-                <label for="password" class="form-label">Password</label>
+                <InputText type="password" @bind-Value="Input.Password" class="form-control" autocomplete="new-password" aria-required="true" placeholder="Por favor, entre com sua senha." />
+                <label for="password" class="form-label">Senha</label>
                 <ValidationMessage For="() => Input.Password" class="text-danger" />
             </div>
             <div class="form-floating mb-3">
-                <InputText type="password" @bind-Value="Input.ConfirmPassword" class="form-control" autocomplete="new-password" aria-required="true" placeholder="Please confirm your password." />
+                <InputText type="password" @bind-Value="Input.ConfirmPassword" class="form-control" autocomplete="new-password" aria-required="true" placeholder="Por favor, confirme seu nova senha." />
                 <label for="confirm-password" class="form-label">Confirm password</label>
                 <ValidationMessage For="() => Input.ConfirmPassword" class="text-danger" />
             </div>
-            <button type="submit" class="w-100 btn btn-lg btn-primary">Reset</button>
+            <button type="submit" class="w-100 btn btn-lg btn-primary">Trocar</button>
         </EditForm>
     </div>
 </div>

+ 3 - 3
ConcursoProjetos/Components/Account/Pages/ResetPasswordConfirmation.razor

@@ -1,7 +1,7 @@
 @page "/Account/ResetPasswordConfirmation"
-<PageTitle>Reset password confirmation</PageTitle>
+<PageTitle>Confirmação de Troca de Senha</PageTitle>
 
-<h1>Reset password confirmation</h1>
+<h1>Confirmação de Troca de Senha</h1>
 <p>
-    Your password has been reset. Please <a href="Account/Login">click here to log in</a>.
+    Sua senha foi alterada. Por favor, <a href="Account/Login">clique aqui para logar</a>.
 </p>

+ 44 - 5
ConcursoProjetos/Components/Layout/NavMenu.razor

@@ -1,6 +1,11 @@
-@implements IDisposable
+@using Microsoft.AspNetCore.Identity
+@using Microsoft.AspNetCore.Components.Authorization
+@implements IDisposable
 
 @inject NavigationManager NavigationManager
+@inject UserManager<ApplicationUser> _userManager
+@inject AuthenticationStateProvider AuthenticationStateProvider
+
 
 <div style="padding: 2em;"><img src="/img/agehab.svg" class="img-fluid" /></div>
 
@@ -35,15 +40,16 @@
                     </form>
                 </div>
 
-         @*        <div class="nav-item px-3">
-                    <form action="Account/ResetPassword" method="post">
+                <div class="nav-item px-3">
+                    <form action="Account/ResetPassword" method="get">
                         <AntiforgeryToken />
                         <input type="hidden" name="ReturnUrl" value="@currentUrl" />
+                        <input type="hidden" name="code" value="@encodedCode" />
                         <button type="submit" class="nav-link">
                             <span class="bi bi-arrow-bar-left-nav-menu" aria-hidden="true"></span> Trocar Senha
                         </button>
                     </form>
-                </div> *@
+                </div>
 
             </Authorized>
             <NotAuthorized>
@@ -74,22 +80,55 @@
 
 @code {
     private string? currentUrl;
+    private string encodedCode = string.Empty;
+    private string? userEmail;
 
-    protected override void OnInitialized()
+
+    protected override async Task OnInitializedAsync()
     {
         currentUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
         NavigationManager.LocationChanged += OnLocationChanged;
+
+
+        // Obter usuário autenticado.
+        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
+        var user = authState.User;
+
+        if (user.Identity?.IsAuthenticated == true)
+        {
+            userEmail = user.FindFirst(c => c.Type == System.Security.Claims.ClaimTypes.Email)?.Value;
+
+            // Gerar código para o usuário autenticado.
+            encodedCode = await GenerateResetCodeAsync(userEmail);
+        }
     }
 
+    private async Task<string> GenerateResetCodeAsync(string? email)
+    {
+        if (string.IsNullOrEmpty(email))
+            return string.Empty;
+
+        var user = await _userManager.FindByEmailAsync(email);
+        if (user == null)
+            return string.Empty;
+
+        var code = await _userManager.GeneratePasswordResetTokenAsync(user);
+
+        return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(code));
+    }
+
+
     private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
     {
         currentUrl = NavigationManager.ToBaseRelativePath(e.Location);
         StateHasChanged();
     }
 
+
     public void Dispose()
     {
         NavigationManager.LocationChanged -= OnLocationChanged;
     }
+
 }