diff --git a/package-lock.json b/package-lock.json index 6947f9f5..243195ce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15498,7 +15498,7 @@ }, "plugins/dotnet-auth-core-identity": { "name": "@amplication/plugin-dotnet-auth-core-identity", - "version": "0.1.2", + "version": "0.1.3", "license": "Apache-2.0", "devDependencies": { "@amplication/code-gen-types": "2.0.37", diff --git a/plugins/dotnet-auth-core-identity/package.json b/plugins/dotnet-auth-core-identity/package.json index f36e4246..bcc91a9b 100644 --- a/plugins/dotnet-auth-core-identity/package.json +++ b/plugins/dotnet-auth-core-identity/package.json @@ -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": {}, diff --git a/plugins/dotnet-auth-core-identity/src/core/create-seed-development-data.ts b/plugins/dotnet-auth-core-identity/src/core/create-seed-development-data.ts index a1107c84..248a0814 100644 --- a/plugins/dotnet-auth-core-identity/src/core/create-seed-development-data.ts +++ b/plugins/dotnet-auth-core-identity/src/core/create-seed-development-data.ts @@ -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(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(); + 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(); - var hashed = password.HashPassword(user, passwordValue); - user.PasswordHash = hashed; - var userStore = new UserStore(context); - await userStore.CreateAsync(user); - var _roleManager = serviceProvider.GetRequiredService>(); - - foreach (var role in amplicationRoles) - { - await userStore.AddToRoleAsync(user, _roleManager.NormalizeKey(role)); - } - + var _roleManager = serviceProvider.GetRequiredService>(); + foreach (var role in amplicationRoles) + { + await userStore.AddToRoleAsync(user, _roleManager.NormalizeKey(role)); + } await context.SaveChangesAsync();`, });