Skip to content

Commit

Permalink
Update - Meilleur prise en charge des erreurs sur l'authentification
Browse files Browse the repository at this point in the history
  • Loading branch information
noelmugnier committed Nov 8, 2020
1 parent 5ab3b13 commit f2f6610
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 34 deletions.
20 changes: 10 additions & 10 deletions Sheaft.Identity/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace Sheaft.Identity.Controllers
{
Expand Down Expand Up @@ -268,7 +269,7 @@ public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model, C
}

var user = await _userManager.FindByEmailAsync(model.UserName);
if (user == null || !user.EmailConfirmed)
if (user == null)
{
ModelState.AddModelError("", "Utilisateur introuvable");
return View(model);
Expand Down Expand Up @@ -308,17 +309,18 @@ public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model, C
public async Task<IActionResult> ConfirmEmail(string userId, string token, string returnUrl = null)
{
if (userId == null || token == null)
{
return View("Error");
}
return RedirectToAction("Error", "Home", new { errorMessage = HttpUtility.UrlEncode("Le lien de confirmation est invalide.") });

var user = await _userManager.FindByIdAsync(userId);
if (user == null)
throw new Exception("Utilisateur introuvable, le lien que vous avez utilisé est peut-être invalide, réessayer de relancer la procédure de vérification d'email ou contactez notre support.");
return RedirectToAction("Error", "Home", new { errorMessage = HttpUtility.UrlEncode("Utilisateur introuvable, le lien que vous avez utilisé est peut-être invalide.") });

if (user.EmailConfirmed)
return View();

var result = await _userManager.ConfirmEmailAsync(user, token);
if (!result.Succeeded)
throw new Exception("Le code de vérification est invalide ou expiré, réessayer de relancer la procédure de vérification d'email ou contactez notre support.");
return RedirectToAction("Error", "Home", new { errorMessage = HttpUtility.UrlEncode("Le code de vérification est invalide ou expiré.") });

return View();
}
Expand All @@ -328,13 +330,11 @@ public async Task<IActionResult> ConfirmEmail(string userId, string token, strin
public async Task<IActionResult> ResetPassword(string userId, string token, string returnUrl = null)
{
if (userId == null || token == null)
{
return View("Error");
}
return RedirectToAction("Error", "Home", new { errorMessage = HttpUtility.UrlEncode("Le lien de réinitialisation de mot de passe est invalide.") });

var user = await _userManager.FindByIdAsync(userId);
if (user == null)
throw new Exception("Utilisateur introuvable, le lien que vous avez utilisé est peut-être invalide, réessayer de relancer la procédure de récupération de mot de passe ou contactez notre support.");
return RedirectToAction("Error", "Home", new { errorMessage = HttpUtility.UrlEncode("Utilisateur introuvable, le lien que vous avez utilisé est peut-être invalide.") });

var vm = new ResetPasswordViewModel
{
Expand Down
9 changes: 7 additions & 2 deletions Sheaft.Identity/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Sheaft.Identity.Security;
using Sheaft.Identity.ViewModels;
using System.Threading.Tasks;
using System.Web;

namespace Sheaft.Identity.Controllers
{
Expand Down Expand Up @@ -39,10 +40,9 @@ public IActionResult Index()
/// <summary>
/// Shows the error page
/// </summary>
public async Task<IActionResult> Error(string errorId)
public async Task<IActionResult> Error(string errorId, string errorMessage = null)
{
var vm = new ErrorViewModel();

// retrieve error details from identityserver
var message = await _interaction.GetErrorContextAsync(errorId);
if (message != null)
Expand All @@ -55,7 +55,12 @@ public async Task<IActionResult> Error(string errorId)
message.ErrorDescription = null;
}
}
else
{
vm.Error = new IdentityServer4.Models.ErrorMessage { Error = HttpUtility.UrlDecode(errorMessage), RequestId = HttpContext.TraceIdentifier };
}

_logger.LogError(vm.Error.Error);
return View("Error", vm);
}
}
Expand Down
35 changes: 16 additions & 19 deletions Sheaft.Identity/Views/Shared/Error.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,31 @@
}

<div class="error-page">
<div class="lead">
<h1>Error</h1>
</div>
<br />
<br />

<div class="row">
<div class="col-sm-6">
<div class="row text-center">
<div class="col-sm-6 content">
<div class="alert alert-danger">
Sorry, there was an error
Nous sommes d&eacute;sol&eacute;, une erreur est survenue. R&eacute;essayez et si le probl&egrave;me persiste, contactez le support par email: <a href="mailto:[email protected]">support@sheaft.com</a>

@if (error != null)
{
<strong>
<em>
: @error
</em>
</strong>

if (errorDescription != null)
{
<div>@errorDescription</div>
}
}
<br />
<br />
<strong>
<em>
@error
</em>
</strong> if (errorDescription != null)
{
<div>@errorDescription</div>}
}
</div>

@if (request_id != null)
{
<div class="request-id">Request Id: @request_id</div>
}
<div class="request-id">Identifiant de requ&ecirc;te : @request_id</div> }
</div>
</div>
</div>
2 changes: 0 additions & 2 deletions Sheaft.Identity/wwwroot/css/login.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


@media (min-width: 769px) {
.login-container, .right-panel {
display: flex;
Expand Down
7 changes: 6 additions & 1 deletion Sheaft.Identity/wwwroot/css/site.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
body {
.error-page .content {
margin: auto !important;
padding:0;
}

body {
height: 100vh;
background-color: #fbfbfb;
font-family: "Roboto", sans-serif;
Expand Down

0 comments on commit f2f6610

Please sign in to comment.