mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-23 22:11:09 +03:00
Updated example.js to reflect new API
no-issue
This commit is contained in:
parent
016422ce06
commit
667061676b
@ -4,66 +4,77 @@ const MembersSSR = require('./');
|
||||
const keys = keypair();
|
||||
|
||||
const membersApiInstance = {
|
||||
getMember() {
|
||||
return Promise.resolve({name: 'egg'});
|
||||
/**
|
||||
* @param {string} token
|
||||
*/
|
||||
async getMemberDataFromMagicLinkToken(token) {
|
||||
return jwt.decode(token);
|
||||
},
|
||||
getPublicConfig() {
|
||||
return Promise.resolve({
|
||||
issuer: 'example.com',
|
||||
publicKey: keys.public
|
||||
});
|
||||
async getMemberIdentityData() {
|
||||
return {name: 'egg'};
|
||||
}
|
||||
};
|
||||
|
||||
const {
|
||||
exchangeTokenForSession,
|
||||
getMemberDataFromSession
|
||||
} = MembersSSR({
|
||||
const membersSSR = MembersSSR({
|
||||
cookieSecure: false, // Secure cookie (default)
|
||||
cookieKeys: ['some-coole-secret'], // Key to sign cookie with
|
||||
membersApi: membersApiInstance // Used to fetch data and verify tokens
|
||||
getMembersApi: () => membersApiInstance // Used to fetch data and verify tokens
|
||||
});
|
||||
|
||||
const server = require('http').createServer((req, res) => {
|
||||
const server = require('http').createServer(async (req, res) => {
|
||||
if (!req.method) {
|
||||
res.writeHead(405);
|
||||
return res.end('Method not allowed.');
|
||||
}
|
||||
if (req.method.toLowerCase() === 'post') {
|
||||
exchangeTokenForSession(req, res).then(() => {
|
||||
try {
|
||||
await membersSSR.exchangeTokenForSession(req, res);
|
||||
res.writeHead(200);
|
||||
res.end();
|
||||
}).catch((err) => {
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.writeHead(err.statusCode);
|
||||
res.end(err.message);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
getMemberDataFromSession(req, res).then((member) => {
|
||||
try {
|
||||
const member = await membersSSR.getMemberDataFromSession(req, res);
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
res.end(JSON.stringify(member));
|
||||
}).catch((err) => {
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.writeHead(err.statusCode);
|
||||
res.end(err.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(0, '127.0.0.1', () => {
|
||||
const {address, port} = server.address();
|
||||
const addressInfo = server.address();
|
||||
if (addressInfo === null || typeof addressInfo === 'string') {
|
||||
throw new TypeError(`Unexpected return value from server.address(): ${addressInfo}`);
|
||||
}
|
||||
const {address, port} = addressInfo;
|
||||
const url = `http://${address}:${port}`;
|
||||
const token = jwt.sign({}, keys.private, {
|
||||
|
||||
const token = jwt.sign({
|
||||
name: 'egg',
|
||||
email: 'egg@mast.er'
|
||||
}, keys.private, {
|
||||
issuer: 'example.com',
|
||||
audience: 'example.com',
|
||||
algorithm: 'RS512'
|
||||
});
|
||||
|
||||
require('http').request(url, {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'content-type': 'text'
|
||||
}
|
||||
require('http').request(`${url}?token=${token}`, {
|
||||
method: 'post'
|
||||
}, (res) => {
|
||||
const cookies = res.headers['set-cookie'] || [];
|
||||
require('http').request(url, {
|
||||
headers: {
|
||||
cookie: res.headers['set-cookie'].join('; ')
|
||||
cookie: cookies.join('; ')
|
||||
}
|
||||
}, (res) => {
|
||||
res.pipe(process.stdout);
|
||||
@ -71,7 +82,7 @@ server.listen(0, '127.0.0.1', () => {
|
||||
server.close();
|
||||
});
|
||||
}).end();
|
||||
}).end(token);
|
||||
}).end();
|
||||
});
|
||||
|
||||
server.on('close', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user