-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: Migrate auth0 to separate plugin #549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
65bc671
89ad6d2
d146d82
4a440c6
78327b6
a6e1b18
e7f51b0
a51ed42
017b864
8c74cb1
393a4dd
ae29b1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| '@halfdomelabs/baseplate-plugin-storage': patch | ||
| '@halfdomelabs/project-builder-common': patch | ||
| '@halfdomelabs/project-builder-server': patch | ||
| '@halfdomelabs/baseplate-plugin-auth': patch | ||
| '@halfdomelabs/project-builder-lib': patch | ||
| '@halfdomelabs/project-builder-web': patch | ||
| '@halfdomelabs/react-generators': patch | ||
| --- | ||
|
|
||
| Split out auth0 functionality into separate plugin |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { getLatestMigrationVersion } from '@src/migrations/index.js'; | ||
| import { | ||
| PluginImplementationStore, | ||
| zPluginWrapper, | ||
| } from '@src/plugins/index.js'; | ||
| import { deserializeSchemaWithReferences } from '@src/references/deserialize-schema.js'; | ||
| import { | ||
| type ProjectDefinition, | ||
| projectDefinitionSchema, | ||
| } from '@src/schema/project-definition.js'; | ||
|
|
||
| import { ProjectDefinitionContainer } from './project-definition-container.js'; | ||
|
|
||
| export function createTestProjectDefinition( | ||
| input: Partial<ProjectDefinition> = {}, | ||
| ): ProjectDefinition { | ||
| return { | ||
| name: 'test-project', | ||
| features: [], | ||
| version: '1.0.0', | ||
| cliVersion: '1.0.0', | ||
| portOffset: 3000, | ||
| apps: [], | ||
| models: [], | ||
| isInitialized: true, | ||
| schemaVersion: getLatestMigrationVersion(), | ||
| ...input, | ||
| }; | ||
| } | ||
|
|
||
| export function createTestProjectDefinitionContainer( | ||
| input: Partial<ProjectDefinition> = {}, | ||
| ): ProjectDefinitionContainer { | ||
| const pluginStore = { | ||
| availablePlugins: [], | ||
| }; | ||
| const pluginImplementationStore = new PluginImplementationStore({}); | ||
| const schemaWithPlugins = zPluginWrapper( | ||
| projectDefinitionSchema, | ||
| pluginImplementationStore, | ||
| ); | ||
| const resolvedRefPayload = deserializeSchemaWithReferences( | ||
| schemaWithPlugins, | ||
| createTestProjectDefinition(input), | ||
| ); | ||
| return new ProjectDefinitionContainer( | ||
| resolvedRefPayload, | ||
| { pluginStore }, | ||
| pluginImplementationStore, | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,90 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { createSchemaMigration } from './types.js'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface OldConfig { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auth?: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userModelRef: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userRoleModelRef: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| useAuth0: boolean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| authFeatureRef: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| accountsFeatureRef: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| passwordProvider?: boolean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| roles: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| comment: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| builtIn: boolean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugins?: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| packageName: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| version: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| config?: unknown; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface NewConfig { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auth?: undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugins?: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| packageName: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| version: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| config?: unknown; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const migration012MigrateAuthConfig = createSchemaMigration< | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| OldConfig, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NewConfig | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| >({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| version: 12, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: 'migrateAuthConfig', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: 'Migrate auth config to auth0 plugin config', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| migrate: (config) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!config.auth?.useAuth0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...config, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auth: undefined, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const auth0PluginId = 'plugin:halfdomelabs_baseplate-plugin-auth_auth0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const auth0Config = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userAccountModelRef: config.auth.userModelRef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| authFeatureRef: config.auth.authFeatureRef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| roles: config.auth.roles.map((role) => ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: role.id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: role.name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| comment: role.comment, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| builtIn: role.builtIn, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const plugins = config.plugins ?? []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const pluginIndex = plugins.findIndex((p) => p.id === auth0PluginId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (pluginIndex === -1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugins.push({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: auth0PluginId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: 'auth0', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| packageName: '@halfdomelabs/baseplate-plugin-auth', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| version: '0.1.0', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| config: auth0Config, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugins[pluginIndex] = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...plugins[pluginIndex], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| config: auth0Config, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+66
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid mutating the original
-const plugins = config.plugins ?? [];
+const plugins = [...(config.plugins ?? [])];This copy costs virtually nothing but guarantees immutability within the 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...config, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auth: undefined, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugins, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import type { AuthRole } from '@src/schema/index.js'; | ||
| import type { ProjectDefinition } from '@src/schema/project-definition.js'; | ||
|
|
||
| import type { PluginSpecImplementation } from './types.js'; | ||
|
|
||
| import { createPluginSpec } from './types.js'; | ||
|
|
||
| type UserAccountModelGetter = (definition: ProjectDefinition) => string; | ||
| type AuthRolesGetter = (definition: ProjectDefinition) => AuthRole[]; | ||
|
|
||
| /** | ||
| * Spec for allowing plugins to declare standard auth configurations | ||
| */ | ||
| export interface AuthConfigSpec extends PluginSpecImplementation { | ||
| getUserAccountModel: UserAccountModelGetter; | ||
| getAuthRoles: AuthRolesGetter; | ||
| } | ||
|
|
||
| /** | ||
| * Spec for adding config component for plugin | ||
| */ | ||
| export const authConfigSpec = createPluginSpec<AuthConfigSpec>( | ||
| 'core/auth-config', | ||
| {}, | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './auth-config-spec.js'; | ||
| export * from './config-spec.js'; | ||
| export * from './types.js'; | ||
| export * from './web-config-spec.js'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Prefer deleting the legacy
authkey instead of assigningundefinedAssigning
undefinedleaves the key present on the object, which can triphasOwnPropertychecks and other truthy evaluations.A safer pattern is:
📝 Committable suggestion
🤖 Prompt for AI Agents