私はLaravelプロジェクトに取り組んでいます。Lighthouse ライブラリを使用して GraphQL API を構築しています。このライブラリを Passport 認証に使用しています https://github.com/joselfonseca/lighthouse-graphql-passport-auth。そのライブラリからエンドポイントを使用して、認証アクセス トークンを生成できます。事前に Passport をインストールして構成しました。問題は、エンドポイントを消費したり、保護されているミューテーションをクエリしたりすると、機能しないことです。トークンがヘッダーに存在する場合でも、ユーザーがログインしているかどうかを確認するために常に false を返します。
次のスキーマ宣言があります。
extend type Mutation @middleware(checks: ["api", "auth.restaurant-admin"]) {
updateUserProfile(input: UpdateUserProfileInput): User! @updateUserProfileValidation
}
ご覧のとおり、これは文字通り「api」および「auth.restaurant-admin」ミドルウェアによって保護されたミューテーションです。
以下は私の auth.restaurant-admin ミドルウェアの実装です。
<?php
namespace App\Http\Middleware;
use App\Models\User;
use Closure;
use Illuminate\Http\Response;
class AuthRestaurantAdmin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (auth()->check() and auth()->user()->hasRole(User::ROLE_RESTAURANT_ADMIN)) {
if (auth()->user()->restaurant) {
return $next($request);
} else {
return abort(Response::HTTP_FORBIDDEN, 'There is no restaurant assigned to the user');
}
}
return abort(Response::HTTP_FORBIDDEN, 'You are not authorised to access this part of the application');
}
}
auth()->check()
ヘッダーに有効なアクセス トークンが存在する場合でも、メソッドは常に false を返します。GraphQL プレイグラウンドで次のようにトークンを渡します。
[![enter code here][1]][1]
$request->header('Authorization') のようにヘッダーからトークンを取得すると、期待値が表示されます。しかし、auth()->check() は機能しません。私のコードには何が間違っていて、何が欠けていますか? どうすれば修正できますか?