I'm using JWT-auth (https://github.com/tymondesigns/jwt-auth) for JWT authorization in my Laravel app. I am able to auth a user and return their JWT token, and all works well and good when I make a call to /api/user with an Authorization header such as: Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciO.eyJp... However I'd like to store this JWT in a cookie and auth the user from the JWT in the cookie. I don't seem able to access any user info, even though I can decode the JWT cookie properly.
My routes looks like:
Route::post('/register', 'AuthController@register');
Route::post('/login', 'AuthController@login');
Route::group(['middleware' => 'jwt.xyz'], function () {
Route::get('/user', 'AuthController@getAuthUser');
});
Auth controller is simply:
public function getAuthUser(Request $request)
{
return response()->json(auth()->user());
}
Kernel.php
protected $routeMiddleware = [
'jwt.xyz' => \App\Http\Middleware\JWTAuthenticate::class,
];
And \App\Http\Middleware\JWTAuthenticate.php:
class JWTAuthenticate extends BaseMiddleware
{
public function handle($request, Closure $next)
{
$cookie = $request->cookie(\Config::get('constants.cookieName'));
$token = new Token($cookie);
$payload = JWTAuth::decode($token);
\Log::debug($payload['sub']); // correct user ID from DB
\Auth::loginUsingId($payload['sub']);
return $next($request);
}
}
That errors with "message": "Method [loginUsingId] does not exist." I am curious how to auth the user so I can do something like Debug::info(auth()->user()) in the controller for the proper user?
Additionally - I thought all cookies sent to/from the browser were encrypted, as the actual cookie seems to be the same plain text representation of the JWT.