Skip to content

Commit 77c383a

Browse files
AndyHubertryasmi
authored andcommitted
feat: Adds an env variable for changing the cookie domain. (#1356)
1 parent f13011c commit 77c383a

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ FS_REPO=local
228228
########
229229
#RESTRICT_CREATE_ORGANISATION=true
230230

231+
#COOKIE_DOMAIN=
232+
231233
# Location of virus scanning binary (ClamAV - https://www.clamav.net/)
232234
#CLAMDSCAN_BINARY=/usr/bin/clamdscan
233235
#CLAMDSCAN_CONF=/etc/clamav/clamd.conf

lib/tools/getWebpackConfig.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ function getWebpackConfig(args) {
205205
? '"development"'
206206
: '"production"',
207207
'process.env.BROWSER': true,
208+
'process.env.COOKIE_DOMAIN': process.env.COOKIE_DOMAIN
209+
? `"${process.env.COOKIE_DOMAIN}"`
210+
: false,
208211
__CLIENT__: true,
209212
__SERVER__: false,
210213
__DEVELOPMENT__: isDebug,

ui/src/redux/modules/auth/logout.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { actions as routerActions } from 'redux-router5';
33
import { Map } from 'immutable';
44
import Cookies from 'js-cookie';
55
import { each, pickBy } from 'lodash';
6-
import { testCookieName } from 'ui/utils/auth';
6+
import { testCookieName, getCookieOptions } from 'ui/utils/auth';
77

88
export const LOGOUT = 'learninglocker/auth/LOGOUT';
99

@@ -22,7 +22,7 @@ function* logoutSaga() {
2222
try {
2323
const cookies = Cookies.get();
2424
const filteredCookies = pickBy(cookies, (value, cookieName) => testCookieName(cookieName));
25-
each(filteredCookies, (value, name) => Cookies.remove(name));
25+
each(filteredCookies, (value, name) => Cookies.remove(name, getCookieOptions()));
2626
yield put(routerActions.navigateTo('login'));
2727
} catch (err) {
2828
console.error(err);

ui/src/redux/modules/auth/orgLogout.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { takeEvery, put } from 'redux-saga/effects';
22
import { actions as routerActions } from 'redux-router5';
33
import Cookies from 'js-cookie';
44
import { each, pickBy } from 'lodash';
5-
import { testOrgCookieName } from 'ui/utils/auth';
5+
import { testOrgCookieName, getCookieOptions } from 'ui/utils/auth';
66

77
export const ORG_LOGOUT = 'learninglocker/auth/ORG_LOGOUT';
88

@@ -27,7 +27,7 @@ function* orgLogoutSaga({ organisationId }) {
2727
const cookies = Cookies.get();
2828
const testCookie = testOrgCookieName(organisationId);
2929
const filteredCookies = pickBy(cookies, (value, cookieName) => testCookie(cookieName));
30-
each(filteredCookies, (value, name) => Cookies.remove(name));
30+
each(filteredCookies, (value, name) => Cookies.remove(name, getCookieOptions()));
3131
yield put(routerActions.navigateTo('home'));
3232
} catch (err) {
3333
throw new Error('Failed to remove auth cookies from browser storage');

ui/src/redux/modules/auth/token.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Cookies from 'js-cookie';
33
import jwtDecode from 'jwt-decode';
44
import { fromJS, Map } from 'immutable';
55
import { takeEvery } from 'redux-saga/effects';
6-
import { getCookieName, getCookieNameStartsWith } from 'ui/utils/auth';
6+
import { getCookieName, getCookieNameStartsWith, getCookieOptions } from 'ui/utils/auth';
77

88
/**
99
* takes tokens received from the auth process
@@ -117,7 +117,7 @@ function storeTokenSaga({ token, tokenType, tokenId }) {
117117
try {
118118
if (token) {
119119
const cookieName = getCookieName({ tokenType, tokenId });
120-
Cookies.set(cookieName, token);
120+
Cookies.set(cookieName, token, getCookieOptions());
121121
}
122122
} catch (err) {
123123
console.error(err);

ui/src/utils/auth.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ const getCookieNameStartsWith = ({ tokenType }, cookies) => {
1616
return key;
1717
};
1818

19+
const getCookieOptions = () => {
20+
const cookieOptions = {};
21+
if (process.env.COOKIE_DOMAIN) {
22+
cookieOptions.domain = process.env.COOKIE_DOMAIN
23+
.replace(/HOSTNAME/, location.hostname);
24+
}
25+
26+
return cookieOptions;
27+
};
28+
1929
/**
2030
* Tests whether the given cookie name is any auth cookie
2131
*/
@@ -35,6 +45,7 @@ const testOrgCookieName = organisationId => (cookieName) => {
3545
export {
3646
getCookieName,
3747
getCookieNameStartsWith,
48+
getCookieOptions,
3849
testCookieName,
3950
testOrgCookieName
4051
};

0 commit comments

Comments
 (0)