Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a signed cookie from the request?

I need to restrict access to a route that communicates with a gem directly, and I cannot use a method of my ApplicationController.

Since I don't use an authentication gem like Devise, I don't have access to authenticate helpers in the routes file.

How do I access my signed cookies?

like image 209
David Morales Avatar asked Nov 07 '25 11:11

David Morales


1 Answers

Implement a constraint as indicated in the Ruby on Rails guide, and use this to access your cookies:

cookies = ActionDispatch::Cookies::CookieJar.build(request, request.cookies)

You can now access your signed or encrypted cookies as usual:

cookies.signed[:user_id]
like image 78
David Morales Avatar answered Nov 09 '25 23:11

David Morales



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!