These are chat archives for dflydev/dflydev-fig-cookies

4th
Dec 2015
Beau Simensen
@simensen
Dec 04 2015 00:15
@mbakker96 what would you like help with? If you can describe it here in an try to get back to you in 30-45 minutes.
Martijn Bakker
@mbakker96
Dec 04 2015 00:17
@simensen I get the error Call to undefined method Dflydev\FigCookies\SetCookies::renderIntoResponse() everytime
This message was deleted
$setCookies = Dflydev\FigCookies\SetCookies::fromResponse($response);

        $setCookie = SetCookie::create($this->config->get('auth.remember'))
            ->withValue("{$rememberIdentifier}___{$rememberToken}")
            ->withExpires(Carbon::parse('+1 week')->timestamp)
        ;

        $response = $setCookies->renderIntoResponse($response);
I use slim 3 framework btw
Call to undefined method Dflydev\FigCookies\SetCookies::renderIntoResponse()
Beau Simensen
@simensen
Dec 04 2015 01:09
@mbakker96 ok, let me see.
@mbakker96 looks like maybe it is a documentation error: renderIntoSetCookieHeader
@mbakker96 would you e interested in updating the README docs? :) $setCookies->renderIntoResponse sould be $setCookies->renderIntoSetCookieHeader
and $cookies->renderIntoRequest should be $cookies->renderIntoCookieHeader
Martijn Bakker
@mbakker96
Dec 04 2015 01:19
I will look for it tomorrow :) Can you tell me what the different is between request cookie and response coockie. I'm want to create a remember cookie and it has to be 1 week active and after that it has to delete itself
Beau Simensen
@simensen
Dec 04 2015 01:20
A request contains a Cookie: ... header. A response contains Set-Cookie: ... headers.
So you need to be aware of both.
Martijn Bakker
@mbakker96
Dec 04 2015 01:21
with one would you advice?
Beau Simensen
@simensen
Dec 04 2015 01:21
@mbakker96 you need both.
Martijn Bakker
@mbakker96
Dec 04 2015 01:21
@simensen oh haha
I started learning this since today lol
Beau Simensen
@simensen
Dec 04 2015 01:22
@mbakker96 ok. i think you'll probably want to use the facades.
Martijn Bakker
@mbakker96
Dec 04 2015 01:22
what is that?
Beau Simensen
@simensen
Dec 04 2015 01:23
$cookie = FigRequestCookies::get($request, 'theme');
Then you can just do $cookie->getValue() to see what value was there for, in this case, the theme cookie.
Then when you are working with the response, you'd use FigResponseCookies::set or FigResponseCookies::modify
Martijn Bakker
@mbakker96
Dec 04 2015 01:25
and then i can edit it with Modify a Request Cookie and then $cookie->withExpires(Carbon::parse('+1 week')->timestamp);
Beau Simensen
@simensen
Dec 04 2015 01:26
No, because that modifies the request cookie. If you need to change the value that the client sends you via the request cookie, you need to use Set-Cookie header in the response so that the client can say, "oh ok, next time i send a Cookie with a request i'll use that value.
$response = FigResponseCookies::set($response, SetCookie::create('token')->withExpires(Carbon::parse('+1 week')->timestamp);
$response = FigResponseCookies::set($response, SetCookie::create('token')->withExpires(Carbon::parse('+1 week')->timestamp)->withValue('your new value');
Martijn Bakker
@mbakker96
Dec 04 2015 01:28
My idea was this:
create a random token
Put it in cookie
Cookie is available 1 week and every time a user enter a admin page he looks for that token
I Will try your code Tommorow
Beau Simensen
@simensen
Dec 04 2015 01:33
@mbakker96 ok. if you can build a middleware around it that would probably work nicely.
Beau Simensen
@simensen
Dec 04 2015 01:38
a lot depends on what else you put around it. :)
Martijn Bakker
@mbakker96
Dec 04 2015 01:40
Hahaha That is so true as 1😂
I use middleware for the check cookie
Do I have to use the same name by respond and request?
Beau Simensen
@simensen
Dec 04 2015 04:29
yes, the cookie name should be the same both for response and request. if that is what you are asking.
Martijn Bakker
@mbakker96
Dec 04 2015 07:56
I updated the README file and created a git pull request.
I will now create a middleware for the coockie
Martijn Bakker
@mbakker96
Dec 04 2015 09:22
I've now this but the coockie is not updating into the header in my middleware. And the problem with the next code is that I echo the $response twice so it shows my page twice
    // Get a collection representing the cookies in the Cookie headers
   // BEFORE
    $cookies    = Cookies::fromRequest($request);
    $setCookies = SetCookies::fromResponse($response);

    $response = $next($request, $response);

    // Render the Set-Cookie headers
   // AFTER
    $request  = $cookies->renderIntoCookieHeader($request);
    $response = $setCookies->renderIntoSetCookieHeader($response);

    return $next($request, $response);
Beau Simensen
@simensen
Dec 04 2015 13:15
@mbakker96 you would want something like this:
    // Get a collection representing the cookies in the Cookie headers
    // BEFORE
    $cookies    = Cookies::fromRequest($request);

    $response = $next($request, $response);

    // Render the Set-Cookie headers
    // AFTER
    $response = $setCookies->renderIntoSetCookieHeader($response);

    return $response
@mbakker96 actually....
    // Get a collection representing the cookies in the Cookie headers
    // BEFORE
    $cookies    = Cookies::fromRequest($request);


    // Do something w/ $cookies like check to see if cookie
    // was sent at all or if it has expired or whatever.
    // 
    // It is possible your code here won't even care about
    // existing cookies on the request at this point
    // as you may have another middleware (that
    // does auth) that will need to know about
    // the remember me cookie or whatever.


    // Hand off the existing request and response to the
    // next middleware or your controller/action.
    $response = $next($request, $response);


    // Render the Set-Cookie headers
    // AFTER
    $setCookies = SetCookies::fromResponse($response);

    // Do something to $setCookies like set or modify a
    // specific cookie. For example, set a new cookie
    // that expires a week in the future or if that
    // cookie already exists, modify the existing
    // cookie to expire a week into the future.

    // This is how any set cookie modifications that you
    // have made make it into the response.
    $response = $setCookies->renderIntoSetCookieHeader($response);

    // Pass back the modified response.
    return $response
Martijn Bakker
@mbakker96
Dec 04 2015 13:43
I think i slowly understand it @simensen. The request is for when i need the cookie and the response is when i want to use te cookie for later use. So when I go to a page i first ask for the request cookie with $cookies = Cookies::fromRequest($request); after my code run where I created a cookie i run $setCookies = SetCookies::fromResponse($response); and $response = $setCookies->renderIntoSetCookieHeader($response);
Beau Simensen
@simensen
Dec 04 2015 13:44
@mbakker96 yep!
Martijn Bakker
@mbakker96
Dec 04 2015 13:44
My start now my laptop and then I will try it out
Martijn Bakker
@mbakker96
Dec 04 2015 14:02
It is still empty
Beau Simensen
@simensen
Dec 04 2015 14:03
@mbakker96 is it possible to share the actual middleware code in its entirety? so i can see all the things going on?
Martijn Bakker
@mbakker96
Dec 04 2015 14:03
I can upload it on my gitlab server and share the link with you
Beau Simensen
@simensen
Dec 04 2015 14:03
@mbakker96 the request will never have the cookie until at least one response is set with the appropriate set-cookie.
@mbakker96 it would evenb e fine if you could paste the code that is doing the cookie manipulation stuff into a gist or pastebin
@mbakker96 i don't think i need to see all of the code, just the middleware if you are using a middleware.
Beau Simensen
@simensen
Dec 04 2015 14:10
@mbakker96 this line is literally doing nothing: https://gist.github.com/mbakker96/491ee0320cb8de90b9be#file-login-php-L37
it creates a cookie but you don't assign it to anything so it isn't doing anything
@mbakker96 these lines are similar; you are assinging to $setCookie but you're not doing anything with $setCookie https://gist.github.com/mbakker96/491ee0320cb8de90b9be#file-login-php-L39-L41
Martijn Bakker
@mbakker96
Dec 04 2015 14:11
The first one i forgot to delete
And thought that i used the $setCookie in the middleware
Beau Simensen
@simensen
Dec 04 2015 14:13
@mbakker96 you need to do something with that $setCookie for it to be meaningful. you need to get $setCookies from the response, and then add that new $setCookie to $setCookies. Then render $setcookies into a response.
Beau Simensen
@simensen
Dec 04 2015 14:14
@mbakker96 right, but you're not doing anything to $setCookies here: https://gist.github.com/mbakker96/491ee0320cb8de90b9be#file-cookie-php-L36-L46
you just extract it from the response and then render it back into the response without making any changes.
that code has no way to know about the $setCookie somewhere else in your code.
Martijn Bakker
@mbakker96
Dec 04 2015 14:15
Btw Thankyou so much for helping me out ;)
so what do I've to do?
Beau Simensen
@simensen
Dec 04 2015 14:16
@mbakker96 sure thing, but i don't have a lot more time i can help with. i'm under a lot of pressure on a work project.
Martijn Bakker
@mbakker96
Dec 04 2015 14:16
Can you give me a direction to the right way?
Beau Simensen
@simensen
Dec 04 2015 14:21
@mbakker96 you can drop the middleware and do this: https://gist.github.com/simensen/b316e8facd40114391b8#file-login-php-L38-L45
@mbakker96 Inside modify's callback you are always passed a set cookie. if the set cookie already existed (probably not in your case) it will already have a value. either way, by setting the value here it will override what already existed as the value for auth.remember.
Beau Simensen
@simensen
Dec 04 2015 14:28
i've gotta go now. i hope that works for you, though. :)
you can write more in here but not sure when i'll be able to jmp back.
Martijn Bakker
@mbakker96
Dec 04 2015 14:36
I works
Yeah just take your time
It is now for work or school or anything is just for private learning
Beau Simensen
@simensen
Dec 04 2015 14:37
It works now? :)
Martijn Bakker
@mbakker96
Dec 04 2015 14:37
it*
it works
Dflydev\FigCookies\Cookie Object
(
[name:Dflydev\FigCookies\Cookie:private] => user_rem
[value:Dflydev\FigCookies\Cookie:private] => x27sRlj1BFCC3ErdxI65Bdee9zKgLXfmleNXnHvUzPo02cVpQ40V5Mg6ucaE0T6/0zAk8OuFLC6dl/pe8yXcJq2M98S0BEzHEWBdgT3O5+EblW7cO5fw0cfP8W9Zy+Hr_+GBFKUR5uunLEOgcRqGxnJ7vy2+MX2fLxcIxxUYeOKVRDAH6bu04w1MsoOn5Uqb4wpEppTV8knYQwMPVXf19G5ZFHqueCmRXQAWTk/oL6jh/+KhKCK598sKUX9q1I1Lt
)
Beau Simensen
@simensen
Dec 04 2015 14:38
cool. :)
Martijn Bakker
@mbakker96
Dec 04 2015 14:38
and delete? if you have tie
time
Beau Simensen
@simensen
Dec 04 2015 14:38
I think you already had the delete stuff working.
actually, no
no, yeah, that is right. :) you're removing it bothf rom the request and the response so that is good.
Martijn Bakker
@mbakker96
Dec 04 2015 14:40
Good to hear
Good luck with your work ;)
Martijn Bakker
@mbakker96
Dec 04 2015 14:49
delete not working
I think same problem as before
Martijn Bakker
@mbakker96
Dec 04 2015 15:26
I updated by logout code but it still not works
btw just take your time :+1:
Beau Simensen
@simensen
Dec 04 2015 15:28
@mbakker96 try to do a print_r on $response after $this->FigResponseCookies->remove($response, $this->config->get('auth.remember')); and see what it is setting for your cookie value/expire time (if anything at all).
@mbakker96 might need to print_r($response->getHeaders()) or whatever is appropriate there to see what headers are there, then you can see what the Set-Cookie header is doing.
Martijn Bakker
@mbakker96
Dec 04 2015 15:29
Array ( [Content-Type] => Array ( [0] => text/html; charset=UTF-8 ) )
Beau Simensen
@simensen
Dec 04 2015 15:30
@mbakker96 i think maybe remove is not the best thing to do in this case. remove for request is fine, but for response, rather than remove, you might want to modify it to set the set-cookie expires time into the past.
$response = $this->FigResponseCookies->modify($response, $this->config->get('auth.remember'), function (SetCookie $setCookie) { return $setCookie->withExpires(some time far in the past); });
Martijn Bakker
@mbakker96
Dec 04 2015 15:38
@simensen tx it works
Beau Simensen
@simensen
Dec 04 2015 15:39
@mbakker96 ^5
Martijn Bakker
@mbakker96
Dec 04 2015 15:39
^5