aiblog/app/Console/Commands/GenerateUserToken.php
2024-11-17 11:30:01 +08:00

29 lines
No EOL
723 B
PHP

<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
class GenerateUserToken extends Command
{
protected $signature = 'user:token {email}';
protected $description = 'Generate API token for a user';
public function handle(): void
{
$email = $this->argument('email');
$user = User::where('email', $email)->first();
if (!$user) {
$this->error("User not found with email: {$email}");
return;
}
$token = $user->createToken('cli-token')->plainTextToken;
$this->info("Token generated successfully for user: {$user->name}");
$this->info("Token: {$token}");
}
}