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

Adds config for samesite support #68

Merged
merged 5 commits into from
May 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from "react";
import Cookies from "js-cookie";

export interface CookieConsentProps {
sameSite?: "strict" | "lax" | "none";
location?: "top" | "bottom" | "none";
style?: object;
buttonStyle?: object;
Expand Down
32 changes: 27 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export const OPTIONS = {
NONE: "none",
};

export const SAMESITE_OPTIONS = {
STRICT: "strict",
LAX: "lax",
NONE: "none",
}

class CookieConsent extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -108,6 +114,7 @@ class CookieConsent extends Component {
hideOnAccept,
onAccept,
extraCookieOptions,
sameSite,
} = this.props;

// fire onAccept
Expand All @@ -116,7 +123,14 @@ class CookieConsent extends Component {
// remove listener if set
window.removeEventListener("scroll", this.handleScroll);

Cookies.set(cookieName, cookieValue, { expires: expires, ...extraCookieOptions });
if (sameSite === SAMESITE_OPTIONS.NONE) {
Cookies.set(cookieName, cookieValue, { expires, sameSite, secure: true, ...extraCookieOptions });

// Fallback for older browsers where can not set SameSite=None, SEE: https://web.dev/sameSite-cookie-recipes/#handling-incompatible-clients
Cookies.set(cookieName, cookieValue, { expires, secure: true, ...extraCookieOptions });
Mastermindzh marked this conversation as resolved.
Show resolved Hide resolved
} else {
Cookies.set(cookieName, cookieValue, { expires, sameSite, ...extraCookieOptions });
}

if (hideOnAccept) {
this.setState({ visible: false });
Expand All @@ -135,6 +149,7 @@ class CookieConsent extends Component {
onDecline,
extraCookieOptions,
setDeclineCookie,
sameSite,
} = this.props;

// fire onDecline
Expand All @@ -143,8 +158,13 @@ class CookieConsent extends Component {
// remove listener if set
window.removeEventListener("scroll", this.handleScroll);

if (setDeclineCookie) {
Cookies.set(cookieName, declineCookieValue, { expires: expires, ...extraCookieOptions });
if (setDeclineCookie && sameSite === SAMESITE_OPTIONS.NONE) {
Mastermindzh marked this conversation as resolved.
Show resolved Hide resolved
Cookies.set(cookieName, declineCookieValue, { expires, sameSite, secure: true, ...extraCookieOptions });

// Fallback for older browsers where can not set SameSite=None, SEE: https://web.dev/sameSite-cookie-recipes/#handling-incompatible-clients
Cookies.set(cookieName, declineCookieValue, { expires, secure: true, ...extraCookieOptions });
} else if (setDeclineCookie) {
Cookies.set(cookieName, declineCookieValue, { expires, sameSite, ...extraCookieOptions });
}

if (hideOnDecline) {
Expand Down Expand Up @@ -274,6 +294,7 @@ class CookieConsent extends Component {

CookieConsent.propTypes = {
location: PropTypes.oneOf(Object.keys(OPTIONS).map((key) => OPTIONS[key])),
sameSite: PropTypes.oneOf(Object.keys(SAMESITE_OPTIONS).map((key) => SAMESITE_OPTIONS[key])),
style: PropTypes.object,
buttonStyle: PropTypes.object,
declineButtonStyle: PropTypes.object,
Expand Down Expand Up @@ -315,8 +336,9 @@ CookieConsent.defaultProps = {
acceptOnScroll: false,
acceptOnScrollPercentage: 25,
location: OPTIONS.BOTTOM,
onAccept: () => {},
onDecline: () => {},
sameSite: SAMESITE_OPTIONS.STRICT,
onAccept: () => { },
onDecline: () => { },
cookieName: "CookieConsent",
cookieValue: true,
declineCookieValue: false,
Expand Down