LaravelにFirebaseの環境をセットアップしてみた

パッケージのインストール

①Laravelのプロジェクトフォルダ配下にディレクトリ移動する。

②必要なパッケージをインストールする。以下のコマンドを打つ。

composer require kreait/firebase-php
composer require kreait/laravel-firebase

あれ!?エラーが出てしまった。。。

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - lcobucci/jwt[4.1.5, ..., 4.2.x-dev] require ext-sodium * -> it is missing from your system. Install or enable PHP's sodium extension.
    - kreait/firebase-php[6.4.1, ..., 6.x-dev] require guzzlehttp/guzzle ^7.4.4 -> found guzzlehttp/guzzle[dev-master, 7.4.4, 7.4.x-dev (alias of dev-master)] but the package is fixed to 7.4.1 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.
    - kreait/laravel-firebase 4.1.0 requires kreait/firebase-php ^6.0 -> satisfiable by kreait/firebase-php[6.0.0, ..., 6.x-dev].
    - kreait/firebase-php[6.0.0, ..., 6.4.0] require lcobucci/jwt ^4.1 -> satisfiable by lcobucci/jwt[4.1.0, ..., 4.2.x-dev].
    - Root composer.json requires kreait/laravel-firebase ^4.1 -> satisfiable by kreait/laravel-firebase[4.1.0].

To enable extensions, verify that they are enabled in your .ini files:
    - C:\xampp\php\php.ini
You can also run `php --ini` in a terminal to see which files are used by PHP in CLI mode.
Alternatively, you can run Composer with `--ignore-platform-req=ext-sodium` to temporarily ignore these required extensions.

Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.
You can also try re-running composer require with an explicit version constraint, e.g. "composer require kreait/laravel-firebase:*" to figure out if any version is installable, or "composer require kreait/laravel-firebase:^2.1" if you know which you need.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

③「lcobucci/jwt」と「kreait/firebase-php」が足りないということだったのでインストールしていく。まずは、php.iniの「;extension=sodium」をコメントアウトする。(lcobucci/jwtdの4.1で必須らしいので)

④以下のコマンドを打つ。

composer require lcobucci/jwt:"4.1.x"

⑤再度、②のコマンドを試してみる。インストール成功。

CONFIGファイルとかの設定

①config/app.phpの「Class Aliases」の配列に以下を追加する。

'ServiceProvider' => Kreait\Laravel\Firebase\ServiceProvider::class,

②Firebaseにログイン→プロジェクトの設定→マイアプリ→Webアプリ→Configからjsonファイルをダウンロードする。

③ダウンロードしたjsonをLaravelのプロジェクトルートフォルダに配置。(.envと同階層。)

③.envを開き、以下の設定項目を追加。

FIREBASE_CREDENTIALS=(ファイル名).json

④以下のコマンドを打つ!「\vendor\kreait\laravel-firebase\config\firebase.php」に設定ファイルが作られる。

php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider" --tag=config

適当に実装

$auth = app('firebase.auth');

$token = $request->headers->get('Authorization');

$verifiedIdToken = $auth->verifyIdToken($token);
$uid = $verifiedIdToken->claims()->get('sub');

refleshTokenを受け取り、firebaseに接続してuidを取得してみる。

できた~うれしい

コメント

タイトルとURLをコピーしました