fix: small fixes

This commit is contained in:
cuqmbr 2022-11-10 09:08:31 +02:00
parent 49064c8f7d
commit 5314b4bd53
9 changed files with 55 additions and 35 deletions

View File

@ -64,7 +64,7 @@ public class AddressManagementController : ControllerBase
} }
[HttpPut("{id}")] [HttpPut("{id}")]
public async Task<IActionResult> UpdateRoute(int id, UpdateAddressDto address) public async Task<IActionResult> UpdateAddress(int id, UpdateAddressDto address)
{ {
if (id != address.Id) if (id != address.Id)
{ {
@ -82,7 +82,7 @@ public class AddressManagementController : ControllerBase
} }
[HttpDelete("{id}")] [HttpDelete("{id}")]
public async Task<IActionResult> DeleteRoute(int id) public async Task<IActionResult> DeleteAddress(int id)
{ {
if (!await _addressManagementService.IsAddressExists(id)) if (!await _addressManagementService.IsAddressExists(id))
{ {

View File

@ -64,7 +64,7 @@ public class CityManagementController : ControllerBase
} }
[HttpPut("{id}")] [HttpPut("{id}")]
public async Task<IActionResult> UpdateRoute(int id, UpdateCityDto city) public async Task<IActionResult> UpdateCountry(int id, UpdateCityDto city)
{ {
if (id != city.Id) if (id != city.Id)
{ {
@ -82,7 +82,7 @@ public class CityManagementController : ControllerBase
} }
[HttpDelete("{id}")] [HttpDelete("{id}")]
public async Task<IActionResult> DeleteRoute(int id) public async Task<IActionResult> DeleteCountry(int id)
{ {
if (!await _cityManagementService.IsCityExists(id)) if (!await _cityManagementService.IsCityExists(id))
{ {

View File

@ -64,7 +64,7 @@ public class CountryManagementController : ControllerBase
} }
[HttpPut("{id}")] [HttpPut("{id}")]
public async Task<IActionResult> UpdateRoute(int id, UpdateCountryDto country) public async Task<IActionResult> UpdateCountry(int id, UpdateCountryDto country)
{ {
if (id != country.Id) if (id != country.Id)
{ {
@ -82,7 +82,7 @@ public class CountryManagementController : ControllerBase
} }
[HttpDelete("{id}")] [HttpDelete("{id}")]
public async Task<IActionResult> DeleteRoute(int id) public async Task<IActionResult> DeleteCountry(int id)
{ {
if (!await _countryManagementService.IsCountryExists(id)) if (!await _countryManagementService.IsCountryExists(id))
{ {

View File

@ -64,7 +64,7 @@ public class StateManagementController : ControllerBase
} }
[HttpPut("{id}")] [HttpPut("{id}")]
public async Task<IActionResult> UpdateRoute(int id, UpdateStateDto state) public async Task<IActionResult> UpdateState(int id, UpdateStateDto state)
{ {
if (id != state.Id) if (id != state.Id)
{ {
@ -82,7 +82,7 @@ public class StateManagementController : ControllerBase
} }
[HttpDelete("{id}")] [HttpDelete("{id}")]
public async Task<IActionResult> DeleteRoute(int id) public async Task<IActionResult> DeleteState(int id)
{ {
if (!await _stateManagementService.IsStateExists(id)) if (!await _stateManagementService.IsStateExists(id))
{ {

View File

@ -179,7 +179,7 @@ namespace Server.Migrations
b.HasIndex("CityId"); b.HasIndex("CityId");
b.ToTable("Addresses"); b.ToTable("Addresses", (string)null);
}); });
modelBuilder.Entity("Server.Models.City", b => modelBuilder.Entity("Server.Models.City", b =>
@ -201,7 +201,7 @@ namespace Server.Migrations
b.HasIndex("StateId"); b.HasIndex("StateId");
b.ToTable("Cities"); b.ToTable("Cities", (string)null);
}); });
modelBuilder.Entity("Server.Models.Company", b => modelBuilder.Entity("Server.Models.Company", b =>
@ -224,7 +224,7 @@ namespace Server.Migrations
b.HasIndex("OwnerId"); b.HasIndex("OwnerId");
b.ToTable("Companies"); b.ToTable("Companies", (string)null);
}); });
modelBuilder.Entity("Server.Models.Country", b => modelBuilder.Entity("Server.Models.Country", b =>
@ -245,7 +245,7 @@ namespace Server.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Countries"); b.ToTable("Countries", (string)null);
}); });
modelBuilder.Entity("Server.Models.Review", b => modelBuilder.Entity("Server.Models.Review", b =>
@ -267,7 +267,7 @@ namespace Server.Migrations
b.HasIndex("VehicleEnrollmentId"); b.HasIndex("VehicleEnrollmentId");
b.ToTable("Reviews"); b.ToTable("Reviews", (string)null);
}); });
modelBuilder.Entity("Server.Models.Route", b => modelBuilder.Entity("Server.Models.Route", b =>
@ -284,7 +284,7 @@ namespace Server.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Routes"); b.ToTable("Routes", (string)null);
}); });
modelBuilder.Entity("Server.Models.RouteAddress", b => modelBuilder.Entity("Server.Models.RouteAddress", b =>
@ -311,7 +311,7 @@ namespace Server.Migrations
b.HasIndex("AddressId"); b.HasIndex("AddressId");
b.ToTable("RouteAddresses"); b.ToTable("RouteAddresses", (string)null);
}); });
modelBuilder.Entity("Server.Models.State", b => modelBuilder.Entity("Server.Models.State", b =>
@ -333,7 +333,7 @@ namespace Server.Migrations
b.HasIndex("CountryId"); b.HasIndex("CountryId");
b.ToTable("States"); b.ToTable("States", (string)null);
}); });
modelBuilder.Entity("Server.Models.Ticket", b => modelBuilder.Entity("Server.Models.Ticket", b =>
@ -354,7 +354,7 @@ namespace Server.Migrations
b.HasIndex("VehicleEnrollmentId"); b.HasIndex("VehicleEnrollmentId");
b.ToTable("Tickets"); b.ToTable("Tickets", (string)null);
}); });
modelBuilder.Entity("Server.Models.User", b => modelBuilder.Entity("Server.Models.User", b =>
@ -474,7 +474,7 @@ namespace Server.Migrations
b.HasIndex("CompanyId"); b.HasIndex("CompanyId");
b.ToTable("Vehicles"); b.ToTable("Vehicles", (string)null);
}); });
modelBuilder.Entity("Server.Models.VehicleEnrollment", b => modelBuilder.Entity("Server.Models.VehicleEnrollment", b =>
@ -513,7 +513,7 @@ namespace Server.Migrations
b.HasIndex("VehicleId"); b.HasIndex("VehicleId");
b.ToTable("VehicleEnrollments"); b.ToTable("VehicleEnrollments", (string)null);
}); });
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b => modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
@ -670,7 +670,7 @@ namespace Server.Migrations
modelBuilder.Entity("Server.Models.User", b => 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<string>("UserId") b1.Property<string>("UserId")
.HasColumnType("text"); .HasColumnType("text");
@ -696,7 +696,7 @@ namespace Server.Migrations
b1.HasKey("UserId", "Id"); b1.HasKey("UserId", "Id");
b1.ToTable("RefreshToken"); b1.ToTable("RefreshToken", (string)null);
b1.WithOwner() b1.WithOwner()
.HasForeignKey("UserId"); .HasForeignKey("UserId");

View File

@ -57,9 +57,6 @@ builder.Services.AddCors(options => {
// Configuration from AppSettings // Configuration from AppSettings
builder.Services.Configure<Jwt>(builder.Configuration.GetSection("Jwt")); builder.Services.Configure<Jwt>(builder.Configuration.GetSection("Jwt"));
// User Manager Service
builder.Services.AddIdentity<User, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddScoped<IAuthenticationService, AuthenticationService>();
// Adding Authentication - JWT // Adding Authentication - JWT
builder.Services.AddAuthentication(options => { builder.Services.AddAuthentication(options => {
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
@ -86,6 +83,13 @@ builder.Services.AddAuthorization();
builder.Services.AddAutoMapper(typeof(MapperInitializer)); builder.Services.AddAutoMapper(typeof(MapperInitializer));
builder.Services.AddIdentity<User, IdentityRole>(options => {
options.User.RequireUniqueEmail = true;
options.Password.RequiredLength = 8;
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_.";
}).AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddScoped<IAuthenticationService, AuthenticationService>();
builder.Services.AddScoped<ICountryManagementService, CountryManagementService>(); builder.Services.AddScoped<ICountryManagementService, CountryManagementService>();
builder.Services.AddScoped<IStateManagementService, StateManagementService>(); builder.Services.AddScoped<IStateManagementService, StateManagementService>();
builder.Services.AddScoped<ICityManagementService, CityManagementService>(); builder.Services.AddScoped<ICityManagementService, CityManagementService>();
@ -120,10 +124,10 @@ if (app.Environment.IsDevelopment())
app.UseHttpsRedirection(); app.UseHttpsRedirection();
// Data seeding // Data seeding
// using var scope = app.Services.CreateScope(); using var scope = app.Services.CreateScope();
// var userManager = (UserManager<User>)scope.ServiceProvider.GetService(typeof(UserManager<User>))!; var userManager = (UserManager<User>)scope.ServiceProvider.GetService(typeof(UserManager<User>))!;
// var roleManager = (RoleManager<IdentityRole>)scope.ServiceProvider.GetService(typeof(RoleManager<IdentityRole>))!; var roleManager = (RoleManager<IdentityRole>)scope.ServiceProvider.GetService(typeof(RoleManager<IdentityRole>))!;
// await ApplicationDbContextSeed.SeedEssentialsAsync(userManager, roleManager); await ApplicationDbContextSeed.SeedEssentialsAsync(userManager, roleManager);
app.MapControllers(); app.MapControllers();

View File

@ -31,7 +31,8 @@ public class AuthenticationService : IAuthenticationService
public async Task<(bool succeeded, string message)> RegisterAsync(RegistrationRequest regRequest) public async Task<(bool succeeded, string message)> RegisterAsync(RegistrationRequest regRequest)
{ {
var userWithSameEmail = await _userManager.FindByEmailAsync(regRequest.Email); 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."); return (false, $"Email {regRequest.Email} is already registered.");
} }
@ -52,12 +53,21 @@ public class AuthenticationService : IAuthenticationService
string? refreshToken)> AuthenticateAsync(AuthenticationRequest authRequest) string? refreshToken)> AuthenticateAsync(AuthenticationRequest authRequest)
{ {
var authResponse = new AuthenticationResponse(); 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) if (user == null)
{ {
authResponse.Message = $"No accounts registered with {authRequest.Email}."; authResponse.Message = $"No accounts registered with {authRequest.EmailOrUsername}.";
return (false, authResponse, null); return (false, authResponse, null);
} }

View File

@ -5,7 +5,7 @@ namespace SharedModels.Requests;
public class AuthenticationRequest public class AuthenticationRequest
{ {
[Required] [Required]
public string Email { get; set; } = null!; public string EmailOrUsername { get; set; } = null!;
[Required] [Required]
public string Password { get; set; } = null!; public string Password { get; set; } = null!;
} }

View File

@ -4,9 +4,15 @@ namespace SharedModels.Requests;
public class RegistrationRequest public class RegistrationRequest
{ {
[Required(ErrorMessage = "Username is required")]
public string Username { get; set; } = null!; public string Username { get; set; } = null!;
[Required] [Required(ErrorMessage = "Email is required")]
[EmailAddress]
public string Email { get; set; } = null!; public string Email { get; set; } = null!;
[Required] [Required(ErrorMessage = "Password is required")]
[DataType(DataType.Password)]
public string Password { get; set; } = null!; 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!;
} }