LoginSignup
2
0

More than 1 year has passed since last update.

Laravel9でJWT認証のインストールエラー

Last updated at Posted at 2022-03-28

概要

Laravel9の環境にJWT認証を導入しょうとしたらエラーが発生したのでメモしておく。

$ composer require tymon/jwt-auth
  ...
  Problem 1
    - Root composer.json requires tymon/jwt-auth ^0.5.12 -> satisfiable by tymon/jwt-auth[0.5.12].
    - tymon/jwt-auth 0.5.12 requires illuminate/support ~5.0 -> found illuminate/support[v5.0.0, ..., 5.8.x-dev] but these were not loaded, likely because it conflicts with another require.
  ...
Installation failed, reverting ./composer.json and ./composer.lock to their original content.

どうやら「illuminate/support」のバージョンが条件に合わないって言われてそう...

tymon/jwt-auth 0.5.12 requires illuminate/support ~5.0 ...

とりあえず、現在インストールされている「illuminate/support」を確認してみる。

composer.lock
"require": {
    ...
    "illuminate/support": "^6|^7|^8|^9",
    ...
},

ここが問題っぽいですね。

tymon/jwt-authを試す

Google先生に解決策を聞いてみると下記サイトがヒットする。

試してみるが...

$ composer require tymon/jwt-auth --ignore-platform-reqs
  ...
  Problem 1
    - tymon/jwt-auth[dev-develop, 1.0.1, ..., 1.0.2] require illuminate/auth ^5.2|^6|^7|^8 -> found illuminate/auth[v5.2.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev, v8.0.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require.
  ...
You can also try re-running composer require with an explicit version constraint, 
e.g. "composer require tymon/jwt-auth:*" to figure out if any version is installable, 
or "composer require tymon/jwt-auth:^2.1" if you know which you need.

駄目なようです。

コメントを見てみると...「tymon/jwt-auth:^2.1」との記載があるので試してみる。

$ composer require tymon/jwt-auth:^2.1
  ...
  Problem 1
    - Root composer.json requires tymon/jwt-auth ^2.1, found tymon/jwt-auth[dev-dependabot/composer/yoast/phpunit-polyfills-tw-1.0.0, dev-dependabot/add-v2-config-file, dev-master, dev-develop, 0.1.0, ..., 0.5.12, 1.0.0-alpha1, ..., 1.0.x-dev (alias of dev-develop), 2.0.x-dev] but it does not match the constraint.

これも駄目なようです。

tomfordrumm/jwt-authを試す

他に下記の記事も見かけました。

ライブラリが「tymon」から「tomfordrumm」に変わっているがこれを試してみる。

$ composer require tomfordrumm/jwt-auth:dev-develop
  ...
  - Installing tomfordrumm/jwt-auth (dev-develop 63522f2): Extracting archive
32 package suggestions were added by new dependencies, use `composer suggest` to see details.

「dev-develop」バージョンなら、インストールが成功するようです。

Packagistで確認する

下記の2つの開発バージョンがありそう。

ただし、「2.0.x-dev」は、現時点では PHP7.4まで しか対応してなさそう...

 % docker-compose exec php composer require tomfordrumm/jwt-auth:2.0.x-dev
  ...
  Problem 1
    - Root composer.json requires tomfordrumm/jwt-auth 2.0.x-dev -> satisfiable by tomfordrumm/jwt-auth[2.0.x-dev].
    - tomfordrumm/jwt-auth 2.0.x-dev requires php ^7.4 -> your php version (8.1.3) does not satisfy that requirement.

試している環境は、PHP8.1.3のため、この環境にはインストールできない。

$ php -v
PHP 8.1.3 (cli) ...

ということで「dev-develop」をとりあえず使ってみる。

Laravel設定

下記ファイルを修正してプロバイダー登録する。

config/app.php
'providers' => [
    ...
    Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
]  

下記コマンドで設定ファイル(config/jwt.php)を追加する。

$ php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider" 

Copied File [/vendor/tomfordrumm/jwt-auth/config/config.php] To [/config/jwt.php]
Publishing complete.

下記コマンドで.envファイルにJWTのシークレットキーを自動作成する。

$ php artisan jwt:secret

jwt-auth secret [*****************************] set successfully.

まとめ

「dev-develop」バージョンを指定したらインストールは可能 なようです。
ひとまずこれで開発作業続けてみます。

参考サイト

以上

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0