Adds config for samesite support

This commit is contained in:
mding5692 2020-05-17 12:09:53 -04:00
parent b3bd42adae
commit ee7ac9ed86
2 changed files with 22 additions and 5 deletions

1
src/index.d.ts vendored
View File

@ -2,6 +2,7 @@ import * as React from "react";
import Cookies from "js-cookie"; import Cookies from "js-cookie";
export interface CookieConsentProps { export interface CookieConsentProps {
samesite?: "strict" | "lax" | "none";
location?: "top" | "bottom" | "none"; location?: "top" | "bottom" | "none";
style?: object; style?: object;
buttonStyle?: object; buttonStyle?: object;

View File

@ -8,6 +8,12 @@ export const OPTIONS = {
NONE: "none", NONE: "none",
}; };
export const SAMESITE_OPTIONS = {
STRICT: "strict",
LAX: "lax",
NONE: "none",
}
class CookieConsent extends Component { class CookieConsent extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
@ -108,6 +114,7 @@ class CookieConsent extends Component {
hideOnAccept, hideOnAccept,
onAccept, onAccept,
extraCookieOptions, extraCookieOptions,
samesite,
} = this.props; } = this.props;
// fire onAccept // fire onAccept
@ -116,7 +123,11 @@ class CookieConsent extends Component {
// remove listener if set // remove listener if set
window.removeEventListener("scroll", this.handleScroll); 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 });
} else {
Cookies.set(cookieName, cookieValue, { expires, samesite, ...extraCookieOptions });
}
if (hideOnAccept) { if (hideOnAccept) {
this.setState({ visible: false }); this.setState({ visible: false });
@ -135,6 +146,7 @@ class CookieConsent extends Component {
onDecline, onDecline,
extraCookieOptions, extraCookieOptions,
setDeclineCookie, setDeclineCookie,
samesite,
} = this.props; } = this.props;
// fire onDecline // fire onDecline
@ -143,8 +155,10 @@ class CookieConsent extends Component {
// remove listener if set // remove listener if set
window.removeEventListener("scroll", this.handleScroll); window.removeEventListener("scroll", this.handleScroll);
if (setDeclineCookie) { if (setDeclineCookie && samesite === SAMESITE_OPTIONS.NONE) {
Cookies.set(cookieName, declineCookieValue, { expires: expires, ...extraCookieOptions }); Cookies.set(cookieName, declineCookieValue, { expires, samesite, secure: true, ...extraCookieOptions });
} else if (setDeclineCookie) {
Cookies.set(cookieName, declineCookieValue, { expires, samesite, ...extraCookieOptions });
} }
if (hideOnDecline) { if (hideOnDecline) {
@ -274,6 +288,7 @@ class CookieConsent extends Component {
CookieConsent.propTypes = { CookieConsent.propTypes = {
location: PropTypes.oneOf(Object.keys(OPTIONS).map((key) => OPTIONS[key])), 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, style: PropTypes.object,
buttonStyle: PropTypes.object, buttonStyle: PropTypes.object,
declineButtonStyle: PropTypes.object, declineButtonStyle: PropTypes.object,
@ -315,6 +330,7 @@ CookieConsent.defaultProps = {
acceptOnScroll: false, acceptOnScroll: false,
acceptOnScrollPercentage: 25, acceptOnScrollPercentage: 25,
location: OPTIONS.BOTTOM, location: OPTIONS.BOTTOM,
samesite: SAMESITE_OPTIONS.STRICT,
onAccept: () => { }, onAccept: () => { },
onDecline: () => { }, onDecline: () => { },
cookieName: "CookieConsent", cookieName: "CookieConsent",