I have a login processing file in which I am attempting to set a cookie:
$expTime = time() + 3600;
$key = getenv("SECRET_KEY");
$token = array(
"iss" => request()->getBaseUrl(),
"sub" => [$user['id']],
"exp" => $expTime,
"iat" => time(),
"nbf" => time(),
"is_admin" => $user['role_id'] == 1
);
$jwt = JWT::encode($token, $key);
$accessToken = new Cookie('access_token', $jwt, $expTime, '/', getenv("COOKIE_DOMAIN"));
redirect('/', ['cookies' => [$accessToken]]);
I'm using Firebase/JWT to include a JWT as the cookie value. The SECRET_KEY and COOKIE_DOMAIN are pulled in from my .ENV file.
I then call my redirect()
function redirect($path, $extra = []) {
$response = new Response(
null,
Response::HTTP_FOUND,
array('location' => $path)
);
if (key_exists('cookies', $extra)) {
foreach ($extra['cookies'] as $cookie) {
$response->headers->setCookie($cookie);
}
}
$response->send();
}
I then test whether or not the cookie has been set in my index file:
if (request()->cookies->has('access_token')) {
echo "Logged in";
} else echo "No cookie :(";
My problem is that my test is returning "No cookie :(".
Any help would be greatly appreciated.
If you prefer you can fork it on GutHub: jpradcliffe/user-authentication
I finally resolved the issue with some help (see comments below). The code as it stands above is correct. The issue was in my .env file.