Updated layer2 to inline with drop in solution

This commit is contained in:
Fabien O'Carroll 2018-11-14 18:33:07 +07:00 committed by Rishabh Garg
parent 37e8d5b670
commit 52759bceb1
4 changed files with 19 additions and 47 deletions

View File

@ -11,28 +11,13 @@ or
## Usage
### As a drop in script:
This will automatically hide and show the elements with `data-members-signin` and `data-members-signout` attributes.
It will set a js cookie called `member` with the token, on the `/` path and keep it in sync with the state of the users loggedin status
It will also reload the page on login/logout, so you are able to read the cookie serverside, and do any content rendering there.
```html
<script src="members-layer2.dropin.js"></script>
<script>
fetch('/ghost/api/v2/content/posts/<id>', { credentials: 'same-origin' });
</script>
```
### As a library
```html
<script src="members-layer2.lib.js"></script>
<script>
Members.init(); // Sets up binding the elements to the login/logout state and member cookie
fetch('/ghost/api/v2/content/posts/<id>', { credentials: 'same-origin' });
// Sets up binding the elements to the login/logout state
Members.init().then(function ({getToken}) {
// can getToken etc...
});
</script>
```
@ -41,7 +26,10 @@ It will also reload the page on login/logout, so you are able to read the cookie
```javascript
const Members = require('@tryghost/members-layer2');
Members.init(); // Sets up binding the elements to the login/logout state
// Sets up binding the elements to the login/logout state
Members.init().then(function ({getToken}) {
// can get token etc...
});
fetch('/ghost/api/v2/content/posts/<id>', { credentials: 'same-origin' });
```

View File

@ -1,4 +0,0 @@
var Members = require('./');
var ready = require('document-ready');
ready(Members.init);

View File

@ -1,7 +1,5 @@
var Members = require('@tryghost/members-layer1');
var members = Members.create();
function show (el) {
el.style.display = 'block';
}
@ -9,21 +7,10 @@ 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 = {
init: function init() {
init: function init(options) {
var members = Members.create();
var signin = document.querySelector('[data-members-signin]');
var signout = document.querySelector('[data-members-signout]');
@ -42,20 +29,22 @@ module.exports = {
event.preventDefault();
members.login()
.then(members.getToken)
.then(setCookie)
.then(reload);
.then(render);
});
signout.addEventListener('click', function (event) {
event.preventDefault();
members.logout()
.then(members.getToken)
.then(setCookie)
.then(reload);
.then(render);
});
return members.getToken()
.then(setCookie)
.then(render);
.then(render)
.then(function () {
return {
getToken: members.getToken
};
});
}
};

View File

@ -10,7 +10,6 @@
"test": "NODE_ENV=testing mocha './test/**/*.test.js'",
"lint": "eslint . --ext .js --cache",
"build:lib": "browserify -s Members index.js > build/members-layer2.lib.js",
"build:drop-in": "browserify drop-in.js > build/members-layer2.dropin.js",
"prebuild": "rm -rf build && mkdir build",
"build": "npm run build:lib && npm run build:drop-in",
"posttest": "yarn lint"