-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathgetUserData.scala.js
47 lines (41 loc) · 1.67 KB
/
getUserData.scala.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@()(implicit context: model.ApplicationContext)
@import play.api.Mode.Dev
// If the browser cuts the mustard and supports `atob`
// then read the user data from cookies and add it to `guardian`
try {
((isVeryModern, document, window) => {
if (isVeryModern) {
function decodeBase64(str) {
return decodeURIComponent(atob(str.replace(/-/g, '+').replace(/_/g, '/').replace(/,/g, '=')));
}
// Short version of cookie.get(), inspired by Google Analytics' code
var cookieData = (function(a) {
var d = new window.Array(),
e = new window.Array();
e = document.cookie.split(";");
a = RegExp("^\\s*" + a + "=\\s*(.*?)\\s*$");
for (var b = 0; b < e.length; b++) {
var f = e[b].match(a);
f && d.push(f[1]);
}
if (d.length > 0) {
return d[0];
}
return null;
})('GU_U');
var userData = cookieData ? JSON.parse(decodeBase64(cookieData.split('.')[0])) : null;
if (userData) {
var displayName = decodeURIComponent(userData[2]);
window.guardian.config.user = {
id: userData[0],
displayName: displayName,
accountCreatedDate: userData[6],
emailVerified: userData[7],
rawResponse: cookieData
}
}
}
})(guardian.isEnhanced && 'atob' in window, document, window);
} catch (e) {
@if(context.environment.mode == Dev) {throw (e)}
}