Skip to content

Commit

Permalink
Merge pull request #405 from amplication/dotnet-auth-seed-fix
Browse files Browse the repository at this point in the history
fix(dotnet-auth-core-identity): fix seed script to only create the initial user once
  • Loading branch information
morhag90 committed Sep 18, 2024
2 parents 856a6d6 + 2cea789 commit 081d50d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/dotnet-auth-core-identity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@amplication/plugin-dotnet-auth-core-identity",
"version": "0.1.2",
"version": "0.1.3",
"description": "Add Authentication and Authorization to your .NET Services",
"main": "dist/index.js",
"nx": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,41 @@ export function CreateSeedDevelopmentDataBody(
namespace: `${resourceName}.Infrastructure`,
}),
],
code: `var context = serviceProvider.GetRequiredService<${resourceName}DbContext>();
code: `
var context = serviceProvider.GetRequiredService<${resourceName}DbContext>();
var userStore = new UserStore<IdentityUser>(context);
var usernameValue = "${seedUserEmail}";
var passwordValue = "${seedUserPassword}";
var existingUser = await userStore.FindByEmailAsync(usernameValue);
if (existingUser != null)
{
return;
}
var user = new IdentityUser
{
Email = usernameValue,
UserName = usernameValue,
NormalizedUserName = usernameValue.ToUpperInvariant(),
NormalizedEmail = usernameValue.ToUpperInvariant(),
};
var password = new PasswordHasher<IdentityUser>();
var hashed = password.HashPassword(user, passwordValue);
user.PasswordHash = hashed;
await userStore.CreateAsync(user);
var amplicationRoles = configuration
.GetSection("AmplicationRoles")
.AsEnumerable()
.Where(x => x.Value != null)
.Select(x => x.Value.ToString())
.ToArray();
var usernameValue = "${seedUserEmail}";
var passwordValue = "${seedUserPassword}";
var user = new IdentityUser
{
Email = usernameValue,
UserName = usernameValue,
NormalizedUserName = usernameValue.ToUpperInvariant(),
NormalizedEmail = usernameValue.ToUpperInvariant(),
};
var password = new PasswordHasher<IdentityUser>();
var hashed = password.HashPassword(user, passwordValue);
user.PasswordHash = hashed;
var userStore = new UserStore<IdentityUser>(context);
await userStore.CreateAsync(user);
var _roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
foreach (var role in amplicationRoles)
{
await userStore.AddToRoleAsync(user, _roleManager.NormalizeKey(role));
}
var _roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
foreach (var role in amplicationRoles)
{
await userStore.AddToRoleAsync(user, _roleManager.NormalizeKey(role));
}
await context.SaveChangesAsync();`,
});
Expand Down

0 comments on commit 081d50d

Please sign in to comment.