@@ -172,6 +172,10 @@ public function handle(): void
172172 }
173173
174174 if ($ this ->confirm ('Do you want to generate an admin user? ' , true )) {
175+ if ($ this ->authType === AuthTypeEnum::Clerk) {
176+ $ this ->publishAdminsTableMigration ();
177+ }
178+
175179 $ this ->createAdminUser ($ kebabName );
176180 }
177181
@@ -317,29 +321,30 @@ protected function runMigrations(): void
317321 shell_exec ('php artisan migrate --ansi ' );
318322 }
319323
320- protected function createAdminUser (string $ kebabName ): void
324+ protected function createAdminUser (string $ kebabName, string $ serviceKey = '' , string $ serviceName = '' ): array
321325 {
326+ $ adminEmail = when (empty ($ serviceKey ), "admin@ {$ kebabName }.com " , "admin. {$ serviceKey }@ {$ kebabName }.com " );
322327 $ defaultPassword = substr (md5 (uniqid ()), 0 , 8 );
323328
324- $ this ->adminCredentials = [
325- 'email ' => $ this ->ask ('Please enter an admin email ' , "admin@ {$ kebabName }.com " ),
326- 'password ' => $ this ->ask ('Please enter an admin password ' , $ defaultPassword ),
329+ $ serviceLabel = when (!empty ($ serviceName ), " for {$ serviceName }" );
330+
331+ $ adminCredentials = [
332+ 'email ' => $ this ->ask ("Please enter admin email {$ serviceLabel }" , $ adminEmail ),
333+ 'password ' => $ this ->ask ("Please enter admin password {$ serviceLabel }" , $ defaultPassword ),
327334 ];
328335
329- if ($ this ->authType === AuthTypeEnum::Clerk) {
330- $ this ->publishMigration (
331- view: view ('initializator::admins_create_table ' )->with ($ this ->adminCredentials ),
332- migrationName: 'admins_create_table ' ,
333- );
334- } else {
335- $ this ->adminCredentials ['name ' ] = $ this ->ask ('Please enter an admin name ' , 'Admin ' );
336- $ this ->adminCredentials ['role_id ' ] = $ this ->ask ('Please enter an admin role id ' , RoleEnum::Admin->value );
336+ if ($ this ->authType === AuthTypeEnum::None) {
337+ $ adminCredentials ['name ' ] = $ this ->ask ("Please enter admin name {$ serviceLabel }" , "{$ serviceName } Admin " );
338+ $ adminCredentials ['role_id ' ] = $ this ->ask ("Please enter admin role id {$ serviceLabel }" , RoleEnum::Admin->value );
339+ }
337340
338- $ this ->publishMigration (
339- view: view ('initializator::add_default_user ' )->with ($ this ->adminCredentials ),
340- migrationName: 'add_default_user ' ,
341- );
341+ if (empty ($ serviceName )) {
342+ $ this ->adminCredentials = $ adminCredentials ;
342343 }
344+
345+ $ this ->publishAdminMigration ($ adminCredentials , $ serviceKey );
346+
347+ return $ adminCredentials ;
343348 }
344349
345350 protected function fillReadme (): void
@@ -459,17 +464,17 @@ protected function fillCredentialsAndAccess(string $kebabName): void
459464 }
460465
461466 if (!empty ($ this ->adminCredentials ) && $ this ->confirm ("Is {$ title }'s admin the same as default one? " , true )) {
462- $ email = $ this ->adminCredentials ['email ' ];
463- $ password = $ this ->adminCredentials ['password ' ];
467+ $ adminCredentials = $ this ->adminCredentials ;
464468 } else {
465- $ defaultPassword = substr (md5 (uniqid ()), 0 , 8 );
469+ if ($ this ->authType === AuthTypeEnum::Clerk && !$ this ->isMigrationExists ('admins_create_table ' )) {
470+ $ this ->publishAdminsTableMigration ();
471+ }
466472
467- $ email = $ this ->ask ("Please enter a {$ title }'s admin email " , "admin@ {$ kebabName }.com " );
468- $ password = $ this ->ask ("Please enter a {$ title }'s admin password " , $ defaultPassword );
473+ $ adminCredentials = $ this ->createAdminUser ($ kebabName , $ key , $ title );
469474 }
470475
471- $ this ->setReadmeValue ($ filePart , "{$ key }_email " , $ email );
472- $ this ->setReadmeValue ($ filePart , "{$ key }_password " , $ password );
476+ $ this ->setReadmeValue ($ filePart , "{$ key }_email " , $ adminCredentials [ ' email ' ] );
477+ $ this ->setReadmeValue ($ filePart , "{$ key }_password " , $ adminCredentials [ ' password ' ] );
473478 $ this ->removeTag ($ filePart , "{$ key }_credentials " );
474479 }
475480
@@ -652,4 +657,31 @@ protected function changeMiddlewareForTelescopeAuthorization(): void
652657
653658 $ config ->write ();
654659 }
660+
661+ protected function publishAdminMigration (array $ adminCredentials , ?string $ serviceKey ): void
662+ {
663+ $ migrationName = (empty ($ serviceKey )) ? 'add_default_admin ' : "add_ {$ serviceKey }_admin " ;
664+
665+ $ viewName = ($ this ->authType === AuthTypeEnum::Clerk)
666+ ? 'initializator::admins_add_additional_admin '
667+ : 'initializator::add_default_user ' ;
668+
669+ $ this ->publishMigration (
670+ view: view ($ viewName )->with ($ adminCredentials ),
671+ migrationName: $ migrationName ,
672+ );
673+ }
674+
675+ protected function isMigrationExists (string $ migrationName ): bool
676+ {
677+ return !empty (glob (base_path ("database/migrations/*_ {$ migrationName }.php " )));
678+ }
679+
680+ protected function publishAdminsTableMigration (): void
681+ {
682+ $ this ->publishMigration (
683+ view: view ('initializator::admins_create_table ' ),
684+ migrationName: 'admins_create_table ' ,
685+ );
686+ }
655687}
0 commit comments