-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dotnet-auth): add seed dev user existence before creation and ne…
…w plugin settings for email and password
- Loading branch information
Showing
5 changed files
with
50 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"settings": {} | ||
"settings": { | ||
"seedUserEmail": "[email protected]", | ||
"seedUserPassword": "P@ssw0rd!" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface Settings { | ||
seedUserEmail: string; | ||
seedUserPassword: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |