1212 */
1313
1414import type { PluginDefinition , PluginContextData , ObjectStackManifest } from '@objectstack/spec/system' ;
15- import { getBetterAuth , BetterAuthConfig } from './auth-client' ;
15+ import { getBetterAuth , resetAuthInstance , BetterAuthConfig } from './auth-client' ;
16+
17+ /**
18+ * Extended app context with router and eventBus
19+ */
20+ interface ExtendedAppContext {
21+ router ?: {
22+ all ?: ( path : string , handler : ( req : any , res : any ) => any ) => void ;
23+ } ;
24+ eventBus ?: {
25+ emit ?: ( event : string , data : any ) => void ;
26+ } ;
27+ }
1628
1729/**
1830 * Plugin Manifest
@@ -83,9 +95,9 @@ export const createBetterAuthPlugin = (options: BetterAuthPluginOptions = {}): P
8395 const handler = toNodeHandler ( auth ) ;
8496
8597 // Mount the auth handler on all /api/auth/* routes
86- const router = ( context . app as any ) . router ;
87- if ( router && typeof router . all === 'function' ) {
88- router . all ( '/api/auth/*' , async ( req : any , res : any ) => {
98+ const app = context . app as ExtendedAppContext ;
99+ if ( app . router && typeof app . router . all === 'function' ) {
100+ app . router . all ( '/api/auth/*' , async ( req : any , res : any ) => {
89101 // Pass the request to Better-Auth handler
90102 return handler ( req , res ) ;
91103 } ) ;
@@ -95,18 +107,18 @@ export const createBetterAuthPlugin = (options: BetterAuthPluginOptions = {}): P
95107 }
96108
97109 // Emit plugin enabled event
98- const eventBus = ( context . app as any ) . eventBus ;
99- if ( eventBus && typeof eventBus . emit === 'function' ) {
100- eventBus . emit ( 'plugin.enabled' , {
110+ if ( app . eventBus && typeof app . eventBus . emit === 'function' ) {
111+ app . eventBus . emit ( 'plugin.enabled' , {
101112 pluginId : BetterAuthManifest . id ,
102113 timestamp : new Date ( ) . toISOString ( )
103114 } ) ;
104115 }
105116
106117 context . logger . info ( '[Better-Auth Plugin] Enabled successfully' ) ;
107118 } catch ( error ) {
108- context . logger . error ( '[Better-Auth Plugin] Failed to enable:' , error ) ;
109- throw error ;
119+ const errorMessage = error instanceof Error ? error . message : 'Unknown error' ;
120+ context . logger . error ( `[Better-Auth Plugin] Failed to enable: ${ errorMessage } ` , error ) ;
121+ throw new Error ( `Better-Auth Plugin initialization failed: ${ errorMessage } ` ) ;
110122 }
111123 } ,
112124
@@ -116,19 +128,27 @@ export const createBetterAuthPlugin = (options: BetterAuthPluginOptions = {}): P
116128 async onDisable ( context : PluginContextData ) {
117129 context . logger . info ( '[Better-Auth Plugin] Disabling...' ) ;
118130
119- // Store last disabled timestamp
120- await context . storage . set ( 'last_disabled' , new Date ( ) . toISOString ( ) ) ;
121-
122- // Emit plugin disabled event
123- const eventBus = ( context . app as any ) . eventBus ;
124- if ( eventBus && typeof eventBus . emit === 'function' ) {
125- eventBus . emit ( 'plugin.disabled' , {
126- pluginId : BetterAuthManifest . id ,
127- timestamp : new Date ( ) . toISOString ( )
128- } ) ;
131+ try {
132+ // Close database connections and reset auth instance
133+ await resetAuthInstance ( ) ;
134+
135+ // Store last disabled timestamp
136+ await context . storage . set ( 'last_disabled' , new Date ( ) . toISOString ( ) ) ;
137+
138+ // Emit plugin disabled event
139+ const app = context . app as ExtendedAppContext ;
140+ if ( app . eventBus && typeof app . eventBus . emit === 'function' ) {
141+ app . eventBus . emit ( 'plugin.disabled' , {
142+ pluginId : BetterAuthManifest . id ,
143+ timestamp : new Date ( ) . toISOString ( )
144+ } ) ;
145+ }
146+
147+ context . logger . info ( '[Better-Auth Plugin] Disabled successfully' ) ;
148+ } catch ( error ) {
149+ context . logger . error ( '[Better-Auth Plugin] Error during disable:' , error ) ;
150+ throw error ;
129151 }
130-
131- context . logger . info ( '[Better-Auth Plugin] Disabled' ) ;
132152 } ,
133153
134154 /**
@@ -137,15 +157,23 @@ export const createBetterAuthPlugin = (options: BetterAuthPluginOptions = {}): P
137157 async onUninstall ( context : PluginContextData ) {
138158 context . logger . info ( '[Better-Auth Plugin] Uninstalling...' ) ;
139159
140- // Cleanup plugin storage
141- await context . storage . delete ( 'install_date' ) ;
142- await context . storage . delete ( 'last_disabled' ) ;
143- await context . storage . delete ( 'config' ) ;
144-
145- // Note: User data in the database is NOT automatically deleted
146- // This should be handled by the administrator
147-
148- context . logger . warn ( '[Better-Auth Plugin] Uninstalled - User authentication data preserved in database' ) ;
160+ try {
161+ // Close database connections and reset auth instance
162+ await resetAuthInstance ( ) ;
163+
164+ // Cleanup plugin storage
165+ await context . storage . delete ( 'install_date' ) ;
166+ await context . storage . delete ( 'last_disabled' ) ;
167+ await context . storage . delete ( 'config' ) ;
168+
169+ // Note: User data in the database is NOT automatically deleted
170+ // This should be handled by the administrator
171+
172+ context . logger . warn ( '[Better-Auth Plugin] Uninstalled - User authentication data preserved in database' ) ;
173+ } catch ( error ) {
174+ context . logger . error ( '[Better-Auth Plugin] Error during uninstall:' , error ) ;
175+ throw error ;
176+ }
149177 } ,
150178 } ;
151179} ;
0 commit comments