Recently I started making a project for my programming classes.
The project is called DarkBox (you can find more information about it on my GitHub)
During the development of the Login Form, I use a test profile to check if everything is working. When I use the email and password, the login doesn't redirect me to where I intended.
Here's the code from my controller:
//(POST)
[HttpPost]
public async Task<IActionResult> Login(string email, string password)
{
if (!ModelState.IsValid)
{
return View();
}
// Gets the user in DB
var user = await _context.Users
.FirstOrDefaultAsync(u => u.Email == email);
if (user == null || user.PasswordHash != password)
{
//Error Message
ModelState.AddModelError(string.Empty, "Email ou senha inválidos.");
ViewBag.Email = email; // Keep the email in view
return View();
}
// Stores the session
HttpContext.Session.SetString("UserEmail", user.Email);
return RedirectToAction("DashboardClient", "DashboardUser");
}
And here's the code for the login form:
<form method="post" action="Login">
<div class="d-flex flex-column align-items-center">
<br>
</div>
<h1 class="h3 mb-3 fw-normal text-center">Iniciar Sessão</h1>
<div class="form-floating">
<input type="email" class="form-control" id="floatingInput" placeholder="name@example.com">
<label for="floatingInput">Email</label>
</div>
<div class="form-floating">
<input type="password" class="form-control" id="floatingPassword" placeholder="Password">
<label for="floatingPassword">Palavra-Passe</label>
</div>
<div class="d-flex justify-content-between align-items-center my-3">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="remember-me" id="flexCheckDefault">
<label class="form-check-label" for="flexCheckDefault">
Lembrar-me
</label>
</div>
<a href="recuperar-senha.html" class="text-primary">Esqueceu a senha?</a>
</div>
<button class="btn btn-success w-100 py-2" type="submit">Iniciar Sessão</button>
<div class="">
<p class="mt-3 text-center">
Não tem conta? <a href="register.html" class="text-primary fw-bold">Criar Conta</a>
</p>
</div>
</form>
I'd appreciate all kinds of help, thank you!
Top comments (0)