From 5314b4bd531c0a640944b6bd9dbef742ac64b84c Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Thu, 10 Nov 2022 09:08:31 +0200 Subject: [PATCH] fix: small fixes --- .../AddressManagementController.cs | 4 +-- .../Controllers/CityManagementController.cs | 4 +-- .../CountryManagementController.cs | 4 +-- .../Controllers/StateManagementController.cs | 4 +-- .../ApplicationDbContextModelSnapshot.cs | 26 +++++++++---------- Server/Program.cs | 18 ++++++++----- Server/Services/AuthenticationService.cs | 18 ++++++++++--- .../Requests/AuthenticationRequest.cs | 2 +- SharedModels/Requests/RegistrationRequest.cs | 10 +++++-- 9 files changed, 55 insertions(+), 35 deletions(-) diff --git a/Server/Controllers/AddressManagementController.cs b/Server/Controllers/AddressManagementController.cs index 2f7ad85..e0d2bf5 100644 --- a/Server/Controllers/AddressManagementController.cs +++ b/Server/Controllers/AddressManagementController.cs @@ -64,7 +64,7 @@ public class AddressManagementController : ControllerBase } [HttpPut("{id}")] - public async Task UpdateRoute(int id, UpdateAddressDto address) + public async Task UpdateAddress(int id, UpdateAddressDto address) { if (id != address.Id) { @@ -82,7 +82,7 @@ public class AddressManagementController : ControllerBase } [HttpDelete("{id}")] - public async Task DeleteRoute(int id) + public async Task DeleteAddress(int id) { if (!await _addressManagementService.IsAddressExists(id)) { diff --git a/Server/Controllers/CityManagementController.cs b/Server/Controllers/CityManagementController.cs index 31c0f7f..f8177e7 100644 --- a/Server/Controllers/CityManagementController.cs +++ b/Server/Controllers/CityManagementController.cs @@ -64,7 +64,7 @@ public class CityManagementController : ControllerBase } [HttpPut("{id}")] - public async Task UpdateRoute(int id, UpdateCityDto city) + public async Task UpdateCountry(int id, UpdateCityDto city) { if (id != city.Id) { @@ -82,7 +82,7 @@ public class CityManagementController : ControllerBase } [HttpDelete("{id}")] - public async Task DeleteRoute(int id) + public async Task DeleteCountry(int id) { if (!await _cityManagementService.IsCityExists(id)) { diff --git a/Server/Controllers/CountryManagementController.cs b/Server/Controllers/CountryManagementController.cs index c047746..fe6b01b 100644 --- a/Server/Controllers/CountryManagementController.cs +++ b/Server/Controllers/CountryManagementController.cs @@ -64,7 +64,7 @@ public class CountryManagementController : ControllerBase } [HttpPut("{id}")] - public async Task UpdateRoute(int id, UpdateCountryDto country) + public async Task UpdateCountry(int id, UpdateCountryDto country) { if (id != country.Id) { @@ -82,7 +82,7 @@ public class CountryManagementController : ControllerBase } [HttpDelete("{id}")] - public async Task DeleteRoute(int id) + public async Task DeleteCountry(int id) { if (!await _countryManagementService.IsCountryExists(id)) { diff --git a/Server/Controllers/StateManagementController.cs b/Server/Controllers/StateManagementController.cs index 29cab71..f43c637 100644 --- a/Server/Controllers/StateManagementController.cs +++ b/Server/Controllers/StateManagementController.cs @@ -64,7 +64,7 @@ public class StateManagementController : ControllerBase } [HttpPut("{id}")] - public async Task UpdateRoute(int id, UpdateStateDto state) + public async Task UpdateState(int id, UpdateStateDto state) { if (id != state.Id) { @@ -82,7 +82,7 @@ public class StateManagementController : ControllerBase } [HttpDelete("{id}")] - public async Task DeleteRoute(int id) + public async Task DeleteState(int id) { if (!await _stateManagementService.IsStateExists(id)) { diff --git a/Server/Migrations/ApplicationDbContextModelSnapshot.cs b/Server/Migrations/ApplicationDbContextModelSnapshot.cs index e03848c..1613970 100644 --- a/Server/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/Server/Migrations/ApplicationDbContextModelSnapshot.cs @@ -179,7 +179,7 @@ namespace Server.Migrations b.HasIndex("CityId"); - b.ToTable("Addresses"); + b.ToTable("Addresses", (string)null); }); modelBuilder.Entity("Server.Models.City", b => @@ -201,7 +201,7 @@ namespace Server.Migrations b.HasIndex("StateId"); - b.ToTable("Cities"); + b.ToTable("Cities", (string)null); }); modelBuilder.Entity("Server.Models.Company", b => @@ -224,7 +224,7 @@ namespace Server.Migrations b.HasIndex("OwnerId"); - b.ToTable("Companies"); + b.ToTable("Companies", (string)null); }); modelBuilder.Entity("Server.Models.Country", b => @@ -245,7 +245,7 @@ namespace Server.Migrations b.HasKey("Id"); - b.ToTable("Countries"); + b.ToTable("Countries", (string)null); }); modelBuilder.Entity("Server.Models.Review", b => @@ -267,7 +267,7 @@ namespace Server.Migrations b.HasIndex("VehicleEnrollmentId"); - b.ToTable("Reviews"); + b.ToTable("Reviews", (string)null); }); modelBuilder.Entity("Server.Models.Route", b => @@ -284,7 +284,7 @@ namespace Server.Migrations b.HasKey("Id"); - b.ToTable("Routes"); + b.ToTable("Routes", (string)null); }); modelBuilder.Entity("Server.Models.RouteAddress", b => @@ -311,7 +311,7 @@ namespace Server.Migrations b.HasIndex("AddressId"); - b.ToTable("RouteAddresses"); + b.ToTable("RouteAddresses", (string)null); }); modelBuilder.Entity("Server.Models.State", b => @@ -333,7 +333,7 @@ namespace Server.Migrations b.HasIndex("CountryId"); - b.ToTable("States"); + b.ToTable("States", (string)null); }); modelBuilder.Entity("Server.Models.Ticket", b => @@ -354,7 +354,7 @@ namespace Server.Migrations b.HasIndex("VehicleEnrollmentId"); - b.ToTable("Tickets"); + b.ToTable("Tickets", (string)null); }); modelBuilder.Entity("Server.Models.User", b => @@ -474,7 +474,7 @@ namespace Server.Migrations b.HasIndex("CompanyId"); - b.ToTable("Vehicles"); + b.ToTable("Vehicles", (string)null); }); modelBuilder.Entity("Server.Models.VehicleEnrollment", b => @@ -513,7 +513,7 @@ namespace Server.Migrations b.HasIndex("VehicleId"); - b.ToTable("VehicleEnrollments"); + b.ToTable("VehicleEnrollments", (string)null); }); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => @@ -670,7 +670,7 @@ namespace Server.Migrations modelBuilder.Entity("Server.Models.User", b => { - b.OwnsMany("Server.Models.RefreshToken", "RefreshTokens", b1 => + b.OwnsMany("Server.Models.User.RefreshTokens#Server.Models.RefreshToken", "RefreshTokens", b1 => { b1.Property("UserId") .HasColumnType("text"); @@ -696,7 +696,7 @@ namespace Server.Migrations b1.HasKey("UserId", "Id"); - b1.ToTable("RefreshToken"); + b1.ToTable("RefreshToken", (string)null); b1.WithOwner() .HasForeignKey("UserId"); diff --git a/Server/Program.cs b/Server/Program.cs index 023257e..3c5e85d 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -57,9 +57,6 @@ builder.Services.AddCors(options => { // Configuration from AppSettings builder.Services.Configure(builder.Configuration.GetSection("Jwt")); -// User Manager Service -builder.Services.AddIdentity().AddEntityFrameworkStores(); -builder.Services.AddScoped(); // Adding Authentication - JWT builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; @@ -86,6 +83,13 @@ builder.Services.AddAuthorization(); builder.Services.AddAutoMapper(typeof(MapperInitializer)); +builder.Services.AddIdentity(options => { + options.User.RequireUniqueEmail = true; + options.Password.RequiredLength = 8; + options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_."; +}).AddEntityFrameworkStores(); +builder.Services.AddScoped(); + builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -120,10 +124,10 @@ if (app.Environment.IsDevelopment()) app.UseHttpsRedirection(); // Data seeding -// using var scope = app.Services.CreateScope(); -// var userManager = (UserManager)scope.ServiceProvider.GetService(typeof(UserManager))!; -// var roleManager = (RoleManager)scope.ServiceProvider.GetService(typeof(RoleManager))!; -// await ApplicationDbContextSeed.SeedEssentialsAsync(userManager, roleManager); +using var scope = app.Services.CreateScope(); +var userManager = (UserManager)scope.ServiceProvider.GetService(typeof(UserManager))!; +var roleManager = (RoleManager)scope.ServiceProvider.GetService(typeof(RoleManager))!; +await ApplicationDbContextSeed.SeedEssentialsAsync(userManager, roleManager); app.MapControllers(); diff --git a/Server/Services/AuthenticationService.cs b/Server/Services/AuthenticationService.cs index 7f5e3c5..7578c91 100644 --- a/Server/Services/AuthenticationService.cs +++ b/Server/Services/AuthenticationService.cs @@ -31,7 +31,8 @@ public class AuthenticationService : IAuthenticationService public async Task<(bool succeeded, string message)> RegisterAsync(RegistrationRequest regRequest) { var userWithSameEmail = await _userManager.FindByEmailAsync(regRequest.Email); - if (userWithSameEmail != null) + var userWithSameUsername = await _userManager.FindByNameAsync(regRequest.Username); + if (userWithSameEmail != null || userWithSameUsername != null) { return (false, $"Email {regRequest.Email} is already registered."); } @@ -52,12 +53,21 @@ public class AuthenticationService : IAuthenticationService string? refreshToken)> AuthenticateAsync(AuthenticationRequest authRequest) { var authResponse = new AuthenticationResponse(); - - var user = await _userManager.FindByEmailAsync(authRequest.Email); + + User user; + + if (authRequest.EmailOrUsername.Contains("@")) + { + user = await _userManager.FindByEmailAsync(authRequest.EmailOrUsername); + } + else + { + user = await _userManager.FindByNameAsync(authRequest.EmailOrUsername); + } if (user == null) { - authResponse.Message = $"No accounts registered with {authRequest.Email}."; + authResponse.Message = $"No accounts registered with {authRequest.EmailOrUsername}."; return (false, authResponse, null); } diff --git a/SharedModels/Requests/AuthenticationRequest.cs b/SharedModels/Requests/AuthenticationRequest.cs index e2ec597..e4c7c22 100644 --- a/SharedModels/Requests/AuthenticationRequest.cs +++ b/SharedModels/Requests/AuthenticationRequest.cs @@ -5,7 +5,7 @@ namespace SharedModels.Requests; public class AuthenticationRequest { [Required] - public string Email { get; set; } = null!; + public string EmailOrUsername { get; set; } = null!; [Required] public string Password { get; set; } = null!; } \ No newline at end of file diff --git a/SharedModels/Requests/RegistrationRequest.cs b/SharedModels/Requests/RegistrationRequest.cs index 9f964e5..8f37b7b 100644 --- a/SharedModels/Requests/RegistrationRequest.cs +++ b/SharedModels/Requests/RegistrationRequest.cs @@ -4,9 +4,15 @@ namespace SharedModels.Requests; public class RegistrationRequest { + [Required(ErrorMessage = "Username is required")] public string Username { get; set; } = null!; - [Required] + [Required(ErrorMessage = "Email is required")] + [EmailAddress] public string Email { get; set; } = null!; - [Required] + [Required(ErrorMessage = "Password is required")] + [DataType(DataType.Password)] public string Password { get; set; } = null!; + [DataType(DataType.Password)] + [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] + public string ConfirmPassword { get; set; } = null!; } \ No newline at end of file