auth changes
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using FluentValidation;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Shared.Models.Accounts.Authenticate;
|
||||
|
||||
namespace WatchIt.WebAPI.Validators.Accounts
|
||||
{
|
||||
public class AuthenticateRequestValidator : AbstractValidator<AuthenticateRequest>
|
||||
{
|
||||
#region CONSTRUCTOR
|
||||
|
||||
public AuthenticateRequestValidator(DatabaseContext database)
|
||||
{
|
||||
RuleFor(x => x.UsernameOrEmail).NotEmpty();
|
||||
RuleFor(x => x.Password).NotEmpty();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using FluentValidation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Shared.Models.Accounts.Register;
|
||||
|
||||
namespace WatchIt.WebAPI.Validators.Accounts
|
||||
{
|
||||
public class RegisterRequestValidator : AbstractValidator<RegisterRequest>
|
||||
{
|
||||
#region CONSTRUCTOR
|
||||
|
||||
public RegisterRequestValidator(DatabaseContext database)
|
||||
{
|
||||
RuleFor(x => x.Username).MinimumLength(5)
|
||||
.MaximumLength(50)
|
||||
.CannotBeIn(database.Accounts, x => x.Username).WithMessage("Username was already used");
|
||||
RuleFor(x => x.Email).EmailAddress()
|
||||
.CannotBeIn(database.Accounts, x => x.Email).WithMessage("Email was already used");
|
||||
RuleFor(x => x.Password).MinimumLength(8)
|
||||
.Must(x => x.Any(c => Char.IsUpper(c))).WithMessage("Password must contain at least one uppercase letter.")
|
||||
.Must(x => x.Any(c => Char.IsLower(c))).WithMessage("Password must contain at least one lowercase letter.")
|
||||
.Must(x => x.Any(c => Char.IsDigit(c))).WithMessage("Password must contain at least one digit.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
18
WatchIt.WebAPI/WatchIt.WebAPI.Validators/CustomValidators.cs
Normal file
18
WatchIt.WebAPI/WatchIt.WebAPI.Validators/CustomValidators.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using FluentValidation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WatchIt.WebAPI.Validators
|
||||
{
|
||||
public static class CustomValidators
|
||||
{
|
||||
public static IRuleBuilderOptions<T, TProperty> CannotBeIn<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, IEnumerable<TProperty> collection) => ruleBuilder.Must(x => !collection.Any(e => Equals(e, x)));
|
||||
public static IRuleBuilderOptions<T, TProperty> CannotBeIn<T, TProperty, TCollectionType>(this IRuleBuilder<T, TProperty> ruleBuilder, IEnumerable<TCollectionType> collection, Func<TCollectionType, TProperty> propertyFunc) => ruleBuilder.Must(x => !collection.Select(propertyFunc).Any(e => Equals(e, x)));
|
||||
public static IRuleBuilderOptions<T, TProperty> MustBeIn<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, IEnumerable<TProperty> collection) => ruleBuilder.Must(x => collection.Any(e => Equals(e, x)));
|
||||
public static IRuleBuilderOptions<T, TProperty> MustBeIn<T, TProperty, TCollectionType>(this IRuleBuilder<T, TProperty> ruleBuilder, IEnumerable<TCollectionType> collection, Func<TCollectionType, TProperty> propertyFunc) => ruleBuilder.Must(x => collection.Select(propertyFunc).Any(e => Equals(e, x)));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentValidation" Version="11.9.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
|
||||
<ProjectReference Include="..\..\WatchIt.Shared\WatchIt.Shared.Models\WatchIt.Shared.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user