Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

closes #5 #11

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ Then, let's add consent scripts and modals to the application's views using the
</html>
```

### Middleware

Add `AddQueuedCookiesToResponse` to your `$middleware` or `$middlewareGroups`. The controller `Whitecube\LaravelCookieConsent\Http\Controllers\ResetController` uses cookie queue to reset cookies.

Comment on lines +121 to +124
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the suggested code for removing cookies works, this is not needed anymore.

## Registering cookies

This package aims to centralize cookie declaration and documentation at the same place in order to keep projects maintainable. However, the suggested methodology is not mandatory. If you wish to queue cookies or execute code upon consent somewhere else in your app's codebase, feel free to do so: we have a few available methods that can come in handy when you'll need to [check if consent has been granted](#checking-for-consent) during the request's lifecycle.
Expand Down
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
'prefix' => config('cookieconsent.url.prefix'),
], function() {
Route::get('script', ScriptController::class)
->name('script');
->name('script')
->middleware('cache.headers:public;max_age=2628000;etag');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain why this line is necessary and how it is related to the issue you're solving? Thanks!


Route::post('accept-all', AcceptAllController::class)
->name('accept.all');
Expand Down
26 changes: 20 additions & 6 deletions src/Http/Controllers/ResetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@

namespace Whitecube\LaravelCookieConsent\Http\Controllers;

use Whitecube\LaravelCookieConsent\CookiesManager;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Http\Request;
use Whitecube\LaravelCookieConsent\CookiesManager;
use Whitecube\LaravelCookieConsent\Facades\Cookies;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need the Cookies facade since the CookieManager is injected in the controller's __invoke method.



class ResetController
{
public function __invoke(Request $request, CookiesManager $cookies)
{
$response = ! $request->expectsJson()
class ResetController {

public function __invoke(Request $request, CookiesManager $cookies) {
$response = !$request->expectsJson()
Comment on lines +11 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use your own coding style rules within the package (I guess you're using an automatic CS fixer with different rules than ours). Thanks !

? redirect()->back()
: response()->json([
'status' => 'ok',
'scripts' => $cookies->getNoticeScripts(true),
'notice' => $cookies->getNoticeMarkup(),
]);

$domain = config('cookieconsent.cookie.domain');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$domain = config('cookieconsent.cookie.domain');
$consent = config('cookieconsent.cookie.name');
$domain = config('cookieconsent.cookie.domain');

Since we're using $domain below, we could also extract the consent cookie name into a variable to make code more readable (see next suggestion).


// delete all defined cookies
foreach (Cookies::getCategories() as $category) {
foreach ($category->getCookies() as $cookie) {
Cookie::queue(Cookie::forget(
name: $cookie->name,
domain: $domain,
));
}
}
Comment on lines +24 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// delete all defined cookies
foreach (Cookies::getCategories() as $category) {
foreach ($category->getCookies() as $cookie) {
Cookie::queue(Cookie::forget(
name: $cookie->name,
domain: $domain,
));
}
}
// delete all defined cookies
$response = array_reduce($cookies->getCategories(), fn($response, $category) => array_reduce(
$category->getCookies(),
fn($response, $cookie) => $response->withoutCookie(cookie: $cookie->name, domain: $domain),
$response
), $response);

Did not test, but this way we do not need to use Cookie queuing and the Cookie facade.


return $response->withoutCookie(
cookie: config('cookieconsent.cookie.name'),
domain: config('cookieconsent.cookie.domain'),
Comment on lines 35 to 36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cookie: config('cookieconsent.cookie.name'),
domain: config('cookieconsent.cookie.domain'),
cookie: $consent,
domain: $domain,

Expand Down