koel/app/Console/Commands/GenerateJwtSecretCommand.php

34 lines
801 B
PHP
Raw Normal View History

2016-01-15 02:16:58 +00:00
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
2016-01-15 07:28:34 +00:00
use Illuminate\Support\Str;
2018-08-19 20:48:47 +00:00
use Jackiedo\DotenvEditor\DotenvEditor;
2016-01-15 02:16:58 +00:00
2018-08-19 20:48:47 +00:00
class GenerateJwtSecretCommand extends Command
2016-01-15 02:16:58 +00:00
{
protected $name = 'koel:generate-jwt-secret';
protected $description = 'Set the JWTAuth secret key used to sign the tokens';
2018-08-19 20:48:47 +00:00
private $dotenvEditor;
public function __construct(DotenvEditor $dotenvEditor)
{
parent::__construct();
$this->dotenvEditor = $dotenvEditor;
}
2016-01-15 02:16:58 +00:00
2018-08-24 15:27:19 +00:00
public function handle(): void
2016-01-15 02:16:58 +00:00
{
2017-12-03 16:54:11 +00:00
if (config('jwt.secret')) {
$this->comment('JWT secret exists -- skipping');
2016-01-15 02:16:58 +00:00
2017-12-03 16:54:11 +00:00
return;
2016-01-15 02:16:58 +00:00
}
2016-01-31 13:46:05 +00:00
2017-12-03 16:54:11 +00:00
$this->info('Generating JWT secret');
2018-08-19 20:48:47 +00:00
$this->dotenvEditor->setKey('JWT_SECRET', Str::random(32))->save();
2016-01-15 02:16:58 +00:00
}
}