Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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";}