@@ -32,7 +32,6 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
3232 throw new Error ( 'Auth configuration is required for email invite plugin' ) ;
3333 }
3434
35- // find field with name resourceConfig.emailField in adminforth.auth.usersResourceId and show error if it doesn't exist
3635 const authResource = adminforth . config . resources . find ( r => r . resourceId === adminforth . config . auth ! . usersResourceId ) ;
3736 if ( ! authResource ) {
3837 throw new Error ( `Resource with id config.auth.usersResourceId=${ adminforth . config . auth ! . usersResourceId } not found` ) ;
@@ -49,7 +48,6 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
4948 }
5049 this . emailField = emailField ;
5150
52- // Check for email confirmation field if specified
5351 if ( this . options . emailConfirmedField ) {
5452 const emailConfirmedField = authResource . columns . find ( f => f . name === this . options . emailConfirmedField ) ;
5553 if ( ! emailConfirmedField ) {
@@ -62,15 +60,13 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
6260 this . emailConfirmedField = emailConfirmedField ;
6361 }
6462
65- // Add hooks to handle user creation and invitation email
6663 if ( ! authResource . hooks ) {
6764 authResource . hooks = { } ;
6865 }
6966 if ( ! authResource . hooks . create ) {
7067 authResource . hooks . create = { } ;
7168 }
7269
73- // Add beforeSave hook to handle password field
7470 if ( ! authResource . hooks . create . beforeSave ) {
7571 authResource . hooks . create . beforeSave = [ ] ;
7672 }
@@ -79,7 +75,6 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
7975 }
8076 authResource . hooks . create . beforeSave . push ( this . handleUserCreation . bind ( this ) ) ;
8177
82- // Add afterSave hook to send invitation email
8378 if ( ! authResource . hooks . create . afterSave ) {
8479 authResource . hooks . create . afterSave = [ ] ;
8580 }
@@ -88,7 +83,6 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
8883 }
8984 authResource . hooks . create . afterSave . push ( this . sendInviteEmail . bind ( this ) ) ;
9085
91- // Add custom page for setting password
9286 adminforth . config . customization . customPages . push ( {
9387 path : '/set-password' ,
9488 component : {
@@ -102,7 +96,6 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
10296 }
10397
10498 validateConfigAfterDiscover ( adminforth : IAdminForth , resourceConfig : AdminForthResource ) {
105- // Validate the email adapter
10699 this . options . adapter . validate ( ) ;
107100 }
108101
@@ -123,12 +116,9 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
123116 delete record . password ;
124117 }
125118
126- // Set password_hash to a temporary placeholder (users will set password via email invite)
127119 const passwordHashFieldName = adminforth . config . auth ! . passwordHashField ;
128- // Generate a placeholder hash that will be replaced when user sets their password
129120 record [ passwordHashFieldName ] = await AdminForth . Utils . generatePasswordHash ( 'TEMP_INVITE_PLACEHOLDER_' + Date . now ( ) ) ;
130121
131- // Set email as unconfirmed if email confirmation is enabled
132122 if ( this . options . emailConfirmedField && this . emailConfirmedField ) {
133123 record [ this . emailConfirmedField . name ] = false ;
134124 }
@@ -158,7 +148,6 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
158148
159149 const brandName = adminforth . config . customization . brandName || 'Admin Panel' ;
160150
161- // Generate invite token
162151 const inviteToken = adminforth . auth . issueJWT (
163152 { email, recordId, inviteEmail : true } ,
164153 'inviteUser' ,
@@ -168,12 +157,10 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
168157 console . log ( 'Sending invite email to:' , email ) ;
169158 console . log ( 'Generated JWT token payload:' , { email, recordId, inviteEmail : true } ) ;
170159
171- // Build invite URL
172160 const host = extra ?. headers ?. host || 'localhost' ;
173161 const protocol = extra ?. headers ?. [ 'x-forwarded-proto' ] || 'http' ;
174162 const inviteUrl = `${ protocol } ://${ host } /set-password?token=${ inviteToken } ` ;
175163
176- // Prepare email content
177164 const emailSubject = `You're invited to ${ brandName } ` ;
178165 const emailText = `
179166 Dear user,
@@ -222,7 +209,7 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
222209 return { ok : true } ;
223210 } catch ( error ) {
224211 console . error ( 'Error sending invite email:' , error ) ;
225- return { ok : true } ; // Don't fail user creation for email issues
212+ return { ok : true } ;
226213 }
227214 }
228215
@@ -239,18 +226,14 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
239226 if ( ! decoded || ! decoded . inviteEmail ) {
240227 return { error : 'Invalid or expired invitation token' , ok : false } ;
241228 }
242-
243- console . log ( 'Decoded JWT token:' , decoded ) ;
244229 const { email, recordId } = decoded ;
245230
246- // Find the user record - try by recordId first, then by email as fallback
247231 let userRecord ;
248232
249233 if ( recordId ) {
250234 userRecord = await this . adminforth . resource ( this . authResource . resourceId ) . get ( recordId ) ;
251235 }
252236
253- // If not found by recordId or recordId is missing, try finding by email
254237 if ( ! userRecord && email ) {
255238 const records = await this . adminforth . resource ( this . authResource . resourceId ) . list (
256239 Filters . EQ ( this . options . emailField , email ) ,
@@ -263,35 +246,27 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
263246 return { error : 'User not found' , ok : false } ;
264247 }
265248
266- // Verify email matches (case-insensitive comparison)
267249 const userEmail = userRecord [ this . options . emailField ] ;
268250 const tokenEmail = email ;
269251
270- console . log ( 'Email verification - User record email:' , userEmail , 'Token email:' , tokenEmail ) ;
271-
272252 if ( ! userEmail || userEmail . toLowerCase ( ) !== tokenEmail . toLowerCase ( ) ) {
273253 return { error : 'Email mismatch' , ok : false } ;
274254 }
275255
276- // Hash the password
277256 const passwordHashFieldName = this . adminforth . config . auth . passwordHashField ;
278257 const newPasswordHash = await AdminForth . Utils . generatePasswordHash ( password ) ;
279258
280- // Get the primary key value for the user record
281259 const primaryKeyField = this . authResource . columns . find ( c => c . primaryKey ) ;
282260 const userRecordId = userRecord [ primaryKeyField ! . name ] ;
283261
284- // Prepare update object
285262 const updateData : any = {
286263 [ passwordHashFieldName ] : newPasswordHash
287264 } ;
288265
289- // Mark email as confirmed if email confirmation is enabled
290266 if ( this . options . emailConfirmedField && this . emailConfirmedField ) {
291267 updateData [ this . emailConfirmedField . name ] = true ;
292268 }
293269
294- // Update the user with the password hash and email confirmation
295270 await this . adminforth . resource ( this . authResource . resourceId ) . update ( userRecordId , updateData ) ;
296271
297272 console . log ( 'Password set successfully for user:' , email ) ;
0 commit comments