Ghost/ghost/members-ssr/README.md

47 lines
1.4 KiB
Markdown
Raw Normal View History

2019-04-02 11:20:43 +03:00
# Members Ssr
## Usage
2019-04-05 09:46:54 +03:00
```js
const MembersSSR = require('./');
const {
exchangeTokenForSession,
2019-04-11 17:29:34 +03:00
getMemberDataFromSession,
deleteSession
2019-04-05 09:46:54 +03:00
} = MembersSSR({
cookieMaxAge: 1000 * 60 * 60 * 24 * 184, // 184 days max cookie age (default)
cookieSecure: true, // Secure cookie (default)
cookieName: 'members-ssr', // Name of cookie (default)
cookiePath: '/', // Path of cookie (default)
cookieKeys: 'some-coole-secret', // Key to sign cookie with
getMembersApi: () => membersApiInstance // Used to fetch data and verify tokens
2019-04-05 09:46:54 +03:00
});
2019-04-11 17:29:34 +03:00
const handleError = res => err => {
res.writeHead(err.statusCode);
res.end(err.message);
};
2019-04-05 09:46:54 +03:00
require('http').createServer((req, res) => {
if (req.method.toLowerCase() === 'post') {
exchangeTokenForSession(req, res).then((member) => {
2019-04-05 09:46:54 +03:00
res.writeHead(200);
res.end(JSON.stringify(member));
2019-04-11 17:29:34 +03:00
}).catch(handleError(res));
} else if (req.method.toLowerCase() === 'delete') {
deleteSession(req, res).then(() => {
res.writeHead(204);
res.end();
}).catch(handleError(res));
2019-04-05 09:46:54 +03:00
} else {
getMemberDataFromSession(req, res).then((member) => {
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify(member));
2019-04-11 17:29:34 +03:00
}).catch(handleError(res));
2019-04-05 09:46:54 +03:00
}
}).listen(3665);
```