Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @page "/login"
- @using Microsoft.AspNetCore.Components.Authorization
- @inject AuthenticationStateProvider AuthenticationStateProvider
- @inject NavigationManager NavigationManager
- <h3>Login</h3>
- <EditForm Model="loginModel" OnValidSubmit="HandleValidSubmit">
- <DataAnnotationsValidator />
- <div class="form-group">
- <label for="email">Email:</label>
- <InputText id="email" class="form-control" @bind-Value="loginModel.Email" />
- <ValidationMessage For="@(() => loginModel.Email)" />
- </div>
- <div class="form-group">
- <label for="password">Password:</label>
- <InputText id="password" class="form-control" type="password" @bind-Value="loginModel.Password" />
- <ValidationMessage For="@(() => loginModel.Password)" />
- </div>
- <button type="submit" class="btn btn-primary">Login</button>
- @if (error != null)
- {
- <div class="alert alert-danger mt-3" role="alert">
- @error
- </div>
- }
- </EditForm>
- @code {
- private LoginModel loginModel = new LoginModel();
- private string error;
- // Replace with the authentication logic for your application
- private async Task HandleValidSubmit()
- {
- // Authenticate the user with your existing authentication system.
- var isAuthenticated = await AuthenticateUser(loginModel.Email, loginModel.Password);
- if (isAuthenticated)
- {
- var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
- if (authState.User.Identity.IsAuthenticated)
- {
- NavigationManager.NavigateTo("/");
- }
- }
- else
- {
- error = "Invalid email or password";
- }
- }
- private async Task<bool> AuthenticateUser(string email, string password)
- {
- // Replace with your authentication logic
- // Authenticate the user using your existing authentication system
- // and return true if the user is authenticated, false otherwise.
- return false;
- }
- public class LoginModel
- {
- [Required, EmailAddress]
- public string Email { get; set; }
- [Required, MinLength(6)]
- public string Password { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement