Refactor JWT token generation and update password handling logic; add exception handling behavior
This commit is contained in:
@@ -11,12 +11,30 @@ public class ValidationException : Exception
|
||||
}
|
||||
|
||||
public ValidationException(IEnumerable<ValidationFailure> failures)
|
||||
: this()
|
||||
: base(BuildMessage(failures, out var errors))
|
||||
{
|
||||
Errors = failures
|
||||
.GroupBy(e => e.PropertyName, e => e.ErrorMessage)
|
||||
.ToDictionary(failureGroup => failureGroup.Key, failureGroup => failureGroup.ToArray());
|
||||
Errors = errors;
|
||||
}
|
||||
|
||||
public IDictionary<string, string[]> Errors { get; }
|
||||
|
||||
private static string BuildMessage(IEnumerable<ValidationFailure> failures, out IDictionary<string, string[]> errors)
|
||||
{
|
||||
var list = failures?.Where(f => !string.IsNullOrWhiteSpace(f.ErrorMessage)).ToList() ?? new List<ValidationFailure>();
|
||||
|
||||
errors = list
|
||||
.GroupBy(e => e.PropertyName, e => e.ErrorMessage)
|
||||
.ToDictionary(failureGroup => failureGroup.Key, failureGroup => failureGroup.ToArray());
|
||||
|
||||
var items = list
|
||||
.Select(f => string.IsNullOrWhiteSpace(f.PropertyName)
|
||||
? f.ErrorMessage
|
||||
: $"{f.PropertyName}: {f.ErrorMessage}")
|
||||
.Where(s => !string.IsNullOrWhiteSpace(s))
|
||||
.ToArray();
|
||||
|
||||
return items.Length > 0
|
||||
? string.Join(" | ", items)
|
||||
: "One or more validation failures have occurred.";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
namespace CMSMicroservice.Application.Common.Interfaces;
|
||||
public interface IGenerateJwtToken
|
||||
{
|
||||
Task<string> GenerateJwtToken(User user);
|
||||
Task<string> GenerateJwtToken(User user,int? expiryDays=null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user