Added members-theme-bindings module

no-issue
This commit is contained in:
Fabien O'Carroll 2019-04-23 14:36:36 +02:00
parent 9e996857bc
commit 87690f9a03
10 changed files with 291 additions and 0 deletions

View File

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/node',
]
};

View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 Ghost Foundation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,44 @@
# Members Theme Bindings
## Install
`npm install @tryghost/members-theme-bindings --save`
or
`yarn add @tryghost/members-theme-bindings`
## Usage
```js
const MembersThemeBindings = require('@tryghost/members-theme-bindings');
MembersThemeBindings.init("http://ghost-site.com/root-ghost-path");
```
## Develop
This is a mono repository, managed with [lerna](https://lernajs.io/).
Follow the instructions for the top-level repo.
1. `git clone` this repo & `cd` into it as usual
2. Run `yarn` to install top-level dependencies.
## Run
- `yarn dev`
## Test
- `yarn lint` run just eslint
- `yarn test` run lint and tests
# Copyright & License
Copyright (c) 2019 Ghost Foundation - Released under the [MIT license](LICENSE).

View File

@ -0,0 +1,135 @@
/* global window document fetch */
const each = require('lodash/each');
const browserAuth = require('@tryghost/members-browser-auth');
module.exports.init = init;
function init(membersUrl) {
const auth = browserAuth({membersUrl});
const [hashMatch, hash, query] = window.location.hash.match(/^#([^?]+)\??(.*)$/) || [];
if (hashMatch && hash === 'reset-password') {
const [tokenMatch, token] = query.match(/token=([a-zA-Z0-9-_]+.[a-zA-Z0-9-_]+.[a-zA-Z0-9-_]+)/) || [];
if (tokenMatch) {
return auth.resetPassword({token})
.then((success) => {
window.location.hash = '';
return success;
})
.then(reload);
}
}
auth.on('signedin', function () {
auth.getSSRToken({fresh: true}).then(function (token) {
createSession(token);
});
});
auth.on('signedout', function () {
destroySession();
});
function signout() {
auth.signout()
.then(() => {
return destroySession();
})
.then(reload);
}
function signin() {
auth.signin()
.then(() => {
return auth.getSSRToken({
fresh: true
}).then(function (token) {
return createSession(token);
});
})
.then(reload);
}
function signup({coupon = ''}) {
auth.signup({coupon})
.then(() => {
return auth.getSSRToken({
fresh: true
}).then(function (token) {
return createSession(token);
});
})
.then(reload);
}
function upgrade() {
auth.upgrade()
.then(() => {
return auth.getSSRToken({
fresh: true
}).then(function (token) {
return createSession(token);
});
})
.then(reload);
}
const signinEls = document.querySelectorAll('[data-members-signin]');
const signupEls = document.querySelectorAll('[data-members-signup]');
const upgradeEls = document.querySelectorAll('[data-members-upgrade]');
const signoutEls = document.querySelectorAll('[data-members-signout]');
each(signinEls, (el) => {
el.addEventListener('click', (event) => {
event.preventDefault();
signin();
});
});
each(signupEls, (el) => {
el.addEventListener('click', (event) => {
event.preventDefault();
const coupon = el.dataset.membersCoupon;
signup({coupon});
});
});
each(upgradeEls, (el) => {
el.addEventListener('click', (event) => {
event.preventDefault();
upgrade();
});
});
each(signoutEls, (el) => {
el.addEventListener('click', (event) => {
event.preventDefault();
signout();
});
});
}
function reload(success) {
if (success) {
window.location.reload();
}
}
function createSession(token) {
return fetch('/members/ssr', {
method: 'post',
credentials: 'include',
body: token
}).then(function (res) {
return !!res.ok;
});
}
function destroySession() {
return fetch('/members/ssr', {
method: 'delete'
}).then(function (res) {
return !!res.ok;
});
}

View File

@ -0,0 +1,37 @@
{
"name": "@tryghost/members-theme-bindings",
"version": "0.0.0",
"repository": "https://github.com/TryGhost/Members/tree/master/packages/members-theme-bindings",
"author": "Ghost Foundation",
"license": "MIT",
"main": "index.js",
"scripts": {
"dev": "echo \"Implement me!\"",
"prebuild": "rm -rf build && mkdir build",
"build": "browserify -s MembersThemeBindings -g es6ify index.js > build/members-theme-bindings.js",
"prepublishOnly": "npm run build",
"test": "NODE_ENV=testing mocha './test/**/*.test.js'",
"lint": "eslint . --ext .js --cache",
"posttest": "yarn lint"
},
"files": [
"index.js",
"build/members-theme-bindings.js"
],
"publishConfig": {
"access": "public"
},
"devDependencies": {
"browserify": "^16.2.3",
"es6ify": "^1.6.0",
"mocha": "6.1.4",
"should": "13.2.3",
"sinon": "7.3.2"
},
"dependencies": {
"@tryghost/members-browser-auth": "^0.1.0",
"bluebird": "^3.5.4",
"ghost-ignition": "^3.1.0",
"lodash": "^4.17.11"
}
}

View File

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/test',
]
};

View File

@ -0,0 +1,10 @@
// Switch these lines once there are useful utils
// const testUtils = require('./utils');
require('./utils');
describe('Hello world', function () {
it('Runs a test', function () {
// TODO: Write me!
'hello'.should.eql('hello');
});
});

View File

@ -0,0 +1,11 @@
/**
* Custom Should Assertions
*
* Add any custom assertions to this file.
*/
// Example Assertion
// should.Assertion.add('ExampleAssertion', function () {
// this.params = {operator: 'to be a valid Example Assertion'};
// this.obj.should.be.an.Object;
// });

View File

@ -0,0 +1,11 @@
/**
* Test Utilities
*
* Shared utils for writing tests
*/
// Require overrides - these add globals for tests
require('./overrides');
// Require assertions - adds custom should assertions
require('./assertions');

View File

@ -0,0 +1,10 @@
// This file is required before any test is run
// Taken from the should wiki, this is how to make should global
// Should is a global in our eslint test config
global.should = require('should').noConflict();
should.extend();
// Sinon is a simple case
// Sinon is a global in our eslint test config
global.sinon = require('sinon');