55use Illuminate \Console \Command ;
66use Symfony \Component \Console \Attribute \AsCommand ;
77
8- #[AsCommand('very-basic-auth:password- generate ' )]
9- class PasswordGenerateCommand extends Command
8+ #[AsCommand('very-basic-auth:generate-password ' )]
9+ class GeneratePassword extends Command
1010{
1111 /**
1212 * The name and signature of the console command.
1313 *
1414 * @var string
1515 */
16- protected $ signature = 'very-basic-auth:password- generate ' ;
16+ protected $ signature = 'very-basic-auth:generate-password ' ;
1717
1818 /**
1919 * The console command description.
@@ -29,31 +29,59 @@ class PasswordGenerateCommand extends Command
2929 */
3030 public function handle (): int
3131 {
32- $ approved = false ;
32+ $ password = $ this -> promptForValidPassword () ;
3333
34- do {
35- $ password = $ this ->secret ('Please enter a password for the very basic auth ' );
34+ if ($ this ->writeNewEnvironmentFileWith ($ this ->hashPassword ($ password ))) {
35+ $ this ->info ('The password has been set successfully. ' );
36+ }
37+
38+ return static ::SUCCESS ;
39+ }
40+
41+ /**
42+ * Keep asking for a password until a valid one is provided.
43+ */
44+ protected function promptForValidPassword (): string
45+ {
46+ while (true ) {
47+ $ password = (string ) $ this ->secret ('Please enter a password for the very basic auth ' );
3648
37- if (empty ( $ password ) || strlen ($ password ) < 8 ) {
49+ if (! $ this -> isPasswordAccepted ($ password )) {
3850 $ this ->error ('The password must be at least 8 characters. ' );
3951 continue ;
4052 }
4153
42- $ confirmation = $ this ->secret ('Please confirm your password ' );
43-
44- if ($ password !== $ confirmation ) {
54+ if (!$ this ->passwordsMatch ($ password )) {
4555 $ this ->error ('The passwords do not match. Please try again. ' );
46- $ approved = false ;
47- } else {
48- $ approved = true ;
56+ continue ;
4957 }
50- } while (!$ approved );
5158
52- if ($ this ->writeNewEnvironmentFileWith (app ()->make ('hash ' )->make ($ password ))) {
53- $ this ->info ('The password has been set successfully. ' );
59+ return $ password ;
5460 }
61+ }
5562
56- return static ::SUCCESS ;
63+ /**
64+ * Determine if the provided password fulfills minimum requirements.
65+ */
66+ protected function isPasswordAccepted (?string $ password ): bool
67+ {
68+ return !empty ($ password ) && strlen ($ password ) >= 8 ;
69+ }
70+
71+ /**
72+ * Ask for confirmation and ensure it matches.
73+ */
74+ protected function passwordsMatch (string $ password ): bool
75+ {
76+ return $ password === (string ) $ this ->secret ('Please confirm your password ' );
77+ }
78+
79+ /**
80+ * Hash the password using Laravel's configured hasher.
81+ */
82+ protected function hashPassword (string $ password ): string
83+ {
84+ return app ()->make ('hash ' )->make ($ password );
5785 }
5886
5987 /**
0 commit comments