diff --git a/BlazorShop.Infrastructure/Persistence/ApplicationDbContextSeed.cs b/BlazorShop.Infrastructure/Persistence/ApplicationDbContextSeed.cs index e1ce79b1..78ca7ace 100644 --- a/BlazorShop.Infrastructure/Persistence/ApplicationDbContextSeed.cs +++ b/BlazorShop.Infrastructure/Persistence/ApplicationDbContextSeed.cs @@ -60,12 +60,14 @@ public static async Task SeedAdminUserAsync(UserManager userManager, RoleM { var admin = new User { - UserName = seedData.FirstName + "@" + seedData.LastName, + UserName = seedData.Username, Email = seedData.Email, FirstName = seedData.FirstName, LastName = seedData.LastName, IsActive = true, + SecurityStamp = Guid.NewGuid().ToString(), }; + var adminRole = roleManager.Roles.Where(x => x.Name == seedData.RoleName).FirstOrDefault(); if (adminRole == null) { @@ -74,7 +76,14 @@ public static async Task SeedAdminUserAsync(UserManager userManager, RoleM if (userManager.Users.All(u => u.Email != admin.Email)) { - await userManager.CreateAsync(admin, seedData.Password); + var result = await userManager.CreateAsync(admin, seedData.Password); + + if (result.Errors.Any()) + { + // likely password requirement errors + throw new Exception("Errors creating admin: " + string.Join(",", result.Errors.Select(x => x.Description))); + } + await userManager.AddToRoleAsync(admin, adminRole.Name); } } @@ -92,7 +101,8 @@ public static async Task SeedClothesDataAsync(ApplicationDbContext context) { Name = "Jeans 1", Description = "asdsad sdasd sad asd dsa", - Price = new decimal(344.00), Amount = 12, + Price = new decimal(344.00), + Amount = 12, ImageName = "buy-3", ImagePath = "buy-3.jpg", IsActive = true, diff --git a/BlazorShop.Infrastructure/Utils/AdminSeedModel.cs b/BlazorShop.Infrastructure/Utils/AdminSeedModel.cs index 659e5f1a..60dd06d1 100644 --- a/BlazorShop.Infrastructure/Utils/AdminSeedModel.cs +++ b/BlazorShop.Infrastructure/Utils/AdminSeedModel.cs @@ -9,6 +9,11 @@ namespace BlazorShop.Infrastructure.Utils /// public class AdminSeedModel { + /// + /// Gets or Sets the username. + /// + public string? Username { get; set; } + /// /// Gets or Sets the firstname. /// diff --git a/BlazorShop.WebApi/Program.cs b/BlazorShop.WebApi/Program.cs index 8f142b03..9708c252 100644 --- a/BlazorShop.WebApi/Program.cs +++ b/BlazorShop.WebApi/Program.cs @@ -21,9 +21,9 @@ options.AddPolicy(corsPolicy, builder => { builder.WithOrigins(allowedOrigins) - .AllowAnyHeader() - .AllowAnyMethod() - .AllowCredentials(); + .AllowAnyHeader() + .AllowAnyMethod() + .AllowCredentials(); }); }); @@ -41,10 +41,10 @@ // Add JWT TOKEN Settings builder.Services.AddAuthentication(opt => - { - opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; - opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - }) + { + opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + }) .AddJwtBearer(options => { options.Audience = builder.Configuration["JwtToken:Audience"]; @@ -188,4 +188,4 @@ { Log.Information("Shut down complete"); Log.CloseAndFlush(); -} \ No newline at end of file +} diff --git a/BlazorShop.WebApi/appsettings.json b/BlazorShop.WebApi/appsettings.json index 7ea301e4..0554208e 100644 --- a/BlazorShop.WebApi/appsettings.json +++ b/BlazorShop.WebApi/appsettings.json @@ -32,20 +32,21 @@ "RunSeedingOnStartup": "false" //"RolesSeedModel": { - // "AdminRoleName": "", - // "AdmintRoleNormalizedName": "", - // "UserRoleName": "", - // "UserRoleNormalizedName": "", - // "DefaultRoleName": "", - // "DefaultRoleNormalizedName": "" + // "AdminRoleName": "", + // "AdminRoleNormalizedName": "", + // "UserRoleName": "", + // "UserRoleNormalizedName": "", + // "DefaultRoleName": "", + // "DefaultRoleNormalizedName": "" //}, //"AdminSeedModel": { - // "FirstName": "", - // "LastName": "", - // "Email": "", - // "Password": "", - // "RoleName": "" - //}, + // "Username": "", + // "FirstName": "", + // "LastName": "", + // "Email": "", + // "Password": "", // caps, numeric and symbol required + // "RoleName": "" // use admin role set above + //} //"BusinessEmail": { // "Host": "smtp.gmail.com", // "Port": 587, diff --git a/README.md b/README.md index b088d5a1..ca5bb6f2 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,13 @@ * Video Demo Link: (In the Future) * Unit of Work Project to demonstrate the Unit of Work with Repository Pattern design pattern +# Setup Project + +* Run 'update-database' command to setup migrations. +* Uncomment and update the seed fields in appsettings.json. +* Run application to apply seeding. +* Empty the seed fields from appsettings.json and comment again. + # Web API functionalities * Worker Service Project - to deactivate a user subscription * Unit Test Project (Under Development)