Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP User Authentication Adding Authentication to Your Application Working with Cookies

Cookie is not being set to the index.php but the doLogin.php only

Please help.. I have been stuck on the section for days. I am running the code on a local server(xampp)..but it does not seen to be working..The cookie is being set to the DoLogin.php and not in the index.php file...

here are the code snippets..

//Code for creating the jwt as per video

$expTime = time() + 9600; $jwt =\Firebase\JWT\JWT::encode([ 'iss'=>request()->getBaseUrl(), 'sub'=>"{$user['id']}", 'exp'=>$expTime, 'iat'=>time(), 'nbf'=>time(), 'is_admin'=>$user['role_ID']==1 ],getenv("SECRET_KEY"),'HS256');

$accessToken = new \Symfony\Component\HttpFoundation\Cookie('access_token',$jwt,$expTime,'/', getenv('COOKIE_DOMAIN'));

redirect('index.php',['cookies'=>[$accessToken]]);

//The code for the redirect function..

function redirect($path, $extra =[]){

    $response = \Symfony\Component\HttpFoundation\Response::create (null,\symfony\Component\HttpFoundation\Response::HTTP_FOUND, ['Location'=>'/UserAuthentication2/'.$path]);

     if(key_exists('cookies', $extra)){

        foreach ($extra['cookies'] as $cookie ) {    
             $response->headers->setCookie($cookie); 

         }
     } 
        $response->send();
        exit;

}

//The code on the index.php function.

if(request()->cookies->has('access_token')){ echo "You are Logged in";

       } 
       else { echo "You are not logged in";}