Skip to content

Commit

Permalink
feat(dotnet-auth): add seed dev user existence before creation and ne…
Browse files Browse the repository at this point in the history
…w plugin settings for email and password
  • Loading branch information
overbit committed Jun 13, 2024
1 parent f3f1f7a commit d8d919f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
5 changes: 4 additions & 1 deletion plugins/dotnet-auth-core-identity/.amplicationrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"settings": {}
"settings": {
"seedUserEmail": "[email protected]",
"seedUserPassword": "P@ssw0rd!"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { dotnetTypes } from "@amplication/code-gen-types";
import { CodeBlock, CsharpSupport } from "@amplication/csharp-ast";
import { getPluginSettings } from "../utils";

export function CreateSeedDevelopmentDataBody(
resourceName: string,
context: dotnetTypes.DsgContext
): CodeBlock {
const { seedUserEmail, seedUserPassword } = getPluginSettings(
context.pluginInstallations
);

export function CreateSeedDevelopmentDataBody(resourceName: string): CodeBlock {
return new CodeBlock({
references: [
CsharpSupport.classReference({
Expand All @@ -24,11 +33,18 @@ export function CreateSeedDevelopmentDataBody(resourceName: string): CodeBlock {
.Select(x => x.Value.ToString())
.ToArray();
var user = new IdentityUser { Email = "[email protected]", UserName = "admin" };
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, "P@ssw0rd!");
var hashed = password.HashPassword(user, passwordValue);
user.PasswordHash = hashed;
var userStore = new UserStore<IdentityUser>(context);
await userStore.CreateAsync(user);
Expand Down
2 changes: 1 addition & 1 deletion plugins/dotnet-auth-core-identity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin {
name: "SeedDevUser",
access: "public",
isAsync: true,
body: CreateSeedDevelopmentDataBody(resourceName),
body: CreateSeedDevelopmentDataBody(resourceName, context),
type: MethodType.STATIC,
parameters: [
CsharpSupport.parameter({
Expand Down
4 changes: 4 additions & 0 deletions plugins/dotnet-auth-core-identity/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Settings {
seedUserEmail: string;
seedUserPassword: string;
}
21 changes: 21 additions & 0 deletions plugins/dotnet-auth-core-identity/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { PluginInstallation } from "@amplication/code-gen-types";
import { name as PackageName } from "../package.json";
import { Settings } from "./types";
import defaultSettings from "../.amplicationrc.json";

export const getPluginSettings = (
pluginInstallations: PluginInstallation[]
): Settings => {
const plugin = pluginInstallations.find(
(plugin) => plugin.npm === PackageName
);

const userSettings = plugin?.settings ?? {};

const settings: Settings = {
...defaultSettings.settings,
...userSettings,
};

return settings;
};

0 comments on commit d8d919f

Please sign in to comment.