-1

My Symfony build project is running at localhost:8000. Now I need to create a cookie named token.

I am confused about what should be path & domain? I have tried below code, it is not creating any cookie.

$token = JWT::encode($tokenPayload, getenv("JWT_SECRET"));

$useHttps = true;

setcookie("token ", $token , $expireTime, "", "localhost:8000", $useHttps, true);
yivi
  • 42,438
  • 18
  • 116
  • 138
nas
  • 2,289
  • 5
  • 32
  • 67
  • Why are you using `useHttps` and `httpOnly`? You seem to be working off the same tutorial than the [person asking this other question](https://stackoverflow.com/questions/60027486/how-to-implement-multiple-authentication-in-symfony). – yivi Feb 02 '20 at 18:55

1 Answers1

0

I would recommend using a different approach for setting cookies in Symfony. Based on this article: https://alvinbunk.wordpress.com/2017/04/03/http-cookies-in-symfony/

use Symfony\Component\HttpFoundation\Cookie;

$cookie = new Cookie(
    'token',
    $token,
    time() + ( 2 * 365 * 24 * 60 * 60)  // Expires 2 years.
);

Also if you want to use this token to authenticate, I can highly recommend: https://github.com/lexik/LexikJWTAuthenticationBundle Then you don't have to worry about saving cookies manually.

BlackWiCKED
  • 386
  • 4
  • 11