mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-25 03:44:29 +03:00
Updated layer2 drop in script to set/unset cookies (#17)
* Updated layer2 drop in script to set/unset cookies * Use ES5 and remove transforms
This commit is contained in:
parent
7fdbd57667
commit
51a5e53c06
@ -1,24 +1,24 @@
|
|||||||
const jwt = require('jsonwebtoken');
|
var jwt = require('jsonwebtoken');
|
||||||
const store = window.localStorage;
|
var store = window.localStorage;
|
||||||
|
|
||||||
const create = (options) => {
|
function create(options) {
|
||||||
return {
|
return {
|
||||||
getToken() {
|
getToken: function getToken() {
|
||||||
let token = store.getItem('members-token') || null;
|
var token = store.getItem('members-token') || null;
|
||||||
return Promise.resolve(token);
|
return Promise.resolve(token);
|
||||||
},
|
},
|
||||||
|
|
||||||
login() {
|
login: function login() {
|
||||||
let token = jwt.sign({}, null, {algorithm: 'none'});
|
var token = jwt.sign({}, null, {algorithm: 'none'});
|
||||||
store.setItem('members-token', token);
|
store.setItem('members-token', token);
|
||||||
return Promise.resolve(!!token);
|
return Promise.resolve(!!token);
|
||||||
},
|
},
|
||||||
|
|
||||||
logout() {
|
logout: function logout() {
|
||||||
store.removeItem('members-token');
|
store.removeItem('members-token');
|
||||||
return Promise.resolve(true);
|
return Promise.resolve(true);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
module.exports.create = create;
|
module.exports.create = create;
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
const Members = require('./');
|
var Members = require('./');
|
||||||
const ready = require('document-ready');
|
var ready = require('document-ready');
|
||||||
|
|
||||||
ready(Members.init);
|
ready(Members.init);
|
||||||
|
|
||||||
module.exports = Members;
|
|
||||||
|
@ -1,45 +1,61 @@
|
|||||||
const Members = require('@tryghost/members-layer1');
|
var Members = require('@tryghost/members-layer1');
|
||||||
|
|
||||||
const members = Members.create();
|
var members = Members.create();
|
||||||
|
|
||||||
const show = el => el.style.display = 'block';
|
function show (el) {
|
||||||
const hide = el => el.style.display = 'none';
|
el.style.display = 'block';
|
||||||
|
}
|
||||||
|
function hide (el) {
|
||||||
|
el.style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
function reload() {
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
function setCookie(token) {
|
||||||
|
if (!token) {
|
||||||
|
document.cookie = 'member=null;path=/;samesite;max-age=0';
|
||||||
|
} else {
|
||||||
|
document.cookie = ['member=', token, ';path=/;samesite;'].join('');
|
||||||
|
}
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getToken() {
|
init: function init() {
|
||||||
return members.getToken();
|
var signin = document.querySelector('[data-members-signin]');
|
||||||
},
|
var signout = document.querySelector('[data-members-signout]');
|
||||||
init() {
|
|
||||||
const signin = document.querySelector('[data-members-signin]');
|
|
||||||
const signout = document.querySelector('[data-members-signout]');
|
|
||||||
|
|
||||||
const render = (signedIn) => {
|
function render (token) {
|
||||||
const promise = signedIn !== undefined ? Promise.resolve(signedIn) : members.getToken();
|
if (token) {
|
||||||
return promise.then((token) => {
|
show(signout);
|
||||||
if (token) {
|
hide(signin);
|
||||||
show(signout);
|
} else {
|
||||||
hide(signin);
|
show(signin);
|
||||||
} else {
|
hide(signout);
|
||||||
show(signin);
|
}
|
||||||
hide(signout);
|
return token;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
signin.addEventListener('click', (event) => {
|
signin.addEventListener('click', function (event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
members.login()
|
members.login()
|
||||||
.then(render);
|
.then(members.getToken)
|
||||||
|
.then(setCookie)
|
||||||
|
.then(reload);
|
||||||
});
|
});
|
||||||
|
|
||||||
signout.addEventListener('click', (event) => {
|
signout.addEventListener('click', function (event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
members.logout()
|
members.logout()
|
||||||
.then((success) => {
|
.then(members.getToken)
|
||||||
render(!success);
|
.then(setCookie)
|
||||||
});
|
.then(reload);
|
||||||
});
|
});
|
||||||
|
|
||||||
return render();
|
return members.getToken()
|
||||||
|
.then(setCookie)
|
||||||
|
.then(render);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -9,8 +9,8 @@
|
|||||||
"dev": "echo \"Implement me!\"",
|
"dev": "echo \"Implement me!\"",
|
||||||
"test": "NODE_ENV=testing mocha './test/**/*.test.js'",
|
"test": "NODE_ENV=testing mocha './test/**/*.test.js'",
|
||||||
"lint": "eslint . --ext .js --cache",
|
"lint": "eslint . --ext .js --cache",
|
||||||
"build:lib": "browserify -t varify -t es6ify -s Members index.js > build/members-layer2.lib.js",
|
"build:lib": "browserify -s Members index.js > build/members-layer2.lib.js",
|
||||||
"build:drop-in": "browserify -t varify -t es6ify -s Members drop-in.js > build/members-layer2.dropin.js",
|
"build:drop-in": "browserify drop-in.js > build/members-layer2.dropin.js",
|
||||||
"prebuild": "rm -rf build && mkdir build",
|
"prebuild": "rm -rf build && mkdir build",
|
||||||
"build": "npm run build:lib && npm run build:drop-in",
|
"build": "npm run build:lib && npm run build:drop-in",
|
||||||
"posttest": "yarn lint"
|
"posttest": "yarn lint"
|
||||||
@ -20,12 +20,10 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"browserify": "^16.2.3",
|
"browserify": "^16.2.3",
|
||||||
"es6ify": "^1.6.0",
|
|
||||||
"eslint": "^5.9.0",
|
"eslint": "^5.9.0",
|
||||||
"mocha": "5.2.0",
|
"mocha": "5.2.0",
|
||||||
"should": "13.2.3",
|
"should": "13.2.3",
|
||||||
"sinon": "7.1.1",
|
"sinon": "7.1.1"
|
||||||
"varify": "^0.2.0"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tryghost/members-layer1": "file:../layer1",
|
"@tryghost/members-layer1": "file:../layer1",
|
||||||
|
Loading…
Reference in New Issue
Block a user