4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel tymon/jwt-auth でトークンを Cookie に保存する

Last updated at Posted at 2019-10-24

環境

概要

jwt-auth では、JWT トークンを以下の順番で検索する。

  • Authorization ヘッダ (Bearer)
  • Request->query('token')
  • Request->input('token')
  • Route パラメタ (token)
  • Cookie (token)

ということで、トークンを発行したら、token という名前の Cookie を設定してあげればよい。

設定手順

Cookie 設定例

class AuthController extends Controller
{
    public function login(Request $request, CookieJar $cookie): Response
    {
        $credentials = request(['email', 'password']);
        $token = auth()->attempt($credentials);
        ...
        return response(...)
            ->withCookie($cookie->make(
                'token',
                $token,
                config('jwt.refresh_ttl'), // minutes
                null, // path
                null, // domain
                $request->getScheme() === 'https', // secure
                true // httpOnly
            ));
    }
}
4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?