Browse Source

further work to revert yesterday's omnifail

master
Rich Brown 1 year ago
parent
commit
1f21a8541a
5 changed files with 35 additions and 95 deletions
  1. +6
    -46
      routes/authRouter.js
  2. +10
    -10
      routes/blog-router.js
  3. +9
    -10
      routes/color-router.js
  4. +10
    -10
      routes/generic-notify.js
  5. +0
    -19
      src/index.js

+ 6
- 46
routes/authRouter.js View File

@@ -1,24 +1,17 @@
import express from 'express';
import Micropub from 'micropub-helper';

// import { store } from '../src/index';

const authRouter = express.Router();

authRouter.get('/callback', (req, res) => {
const { code, me } = req.query;
const foundMp = req.app.locals[me];
return foundMp
const { code } = req.query;
// also me and state available here
return req.app.locals.mp
.getToken(code)
.then((token) => {
if (!token) {
res.status(401).json({ err: 'Token fail on callback', msg: null });
} else {
const tokens = {
[me]: token,
};
req.session.token = token;
req.app.locals.tokens = tokens;
res.json({ err: null, msg: 'Token auth successful' });
}
})
@@ -28,30 +21,10 @@ authRouter.get('/callback', (req, res) => {
});
});


authRouter.post('/', (req, res) => {
const {
clientId, redirectUri, me, state,
} = req.body;

req.session.clientId = clientId;
req.session.redirectUri = redirectUri;
req.session.state = state;
req.session.me = me;

// console.log(req.session, '<-req.session in initial post');
const newMicropub = new Micropub({
clientId, redirectUri, me, state,
});

req.app.locals[me] = newMicropub;

req.session.save((err) => {
console.log(err || 'Session saved.');
});
// console.log(req.session, '<-- session in root, after vars set');

return req.app.locals[me]
const newMicropub = new Micropub({ ...req.body });
req.app.locals.mp = newMicropub;
return req.app.locals.mp
.getAuthUrl()
.then(url => res.json({ error: null, url }))
.catch(() => {
@@ -60,17 +33,4 @@ authRouter.post('/', (req, res) => {
});
});

// authRouter.get('/', (req, res) => {
// if (req.session.views) {
// req.session.views += 1;
// res.setHeader('Content-Type', 'text/html');
// res.write(`<p>views: ${req.session.views}</p>`);
// res.write(`<p>expires in: ${req.session.cookie.maxAge / 1000}s</p>`);
// res.end();
// } else {
// req.session.views = 1;
// res.end('welcome to the session demo. refresh!');
// }
// });

export default authRouter;

+ 10
- 10
routes/blog-router.js View File

@@ -1,4 +1,3 @@

import express from 'express';
import axios from 'axios';

@@ -22,15 +21,16 @@ blogRouter.post('/notify', async (req, res) => {
toSlack.bodyOfMessage = req.body.bodyOfMessage;
}

axios.post(
process.env.WEBHOOK,
{
text: `got a blog inquiry: ${JSON.stringify(toSlack)}`,
},
{
headers: { 'Content-type': 'application/json' },
},
)
axios
.post(
process.env.WEBHOOK,
{
text: `_got a blog inquiry_: ${JSON.stringify(toSlack)}`,
},
{
headers: { 'Content-type': 'application/json' },
},
)
.then(() => res.send({ message: `Received, ${req.body.email}. Forwarding now.` }))
.catch(e => console.log(e)); //eslint-disable-line
});

+ 9
- 10
routes/color-router.js View File

@@ -6,20 +6,19 @@ let serverColor = '#369';
const gradient = '';
const animation = '';

colorRouter.get('/', (req, res) => {
res.json({
color: serverColor,
gradient,
animation,
});
});
colorRouter.get('/', (req, res) => res.json({
color: serverColor,
gradient,
animation,
}));

colorRouter.post('/', (req, res) => {
try {
const { color } = req.body;
serverColor = color;
res.status(200).end();
} catch (er) {
console.log(er);
return res.status(200).end();
} catch (error) {
console.log(error);
return res.status(400).json({ error: 'Something went wrong in the color-router POST' });
}
});

+ 10
- 10
routes/generic-notify.js View File

@@ -1,4 +1,3 @@

import express from 'express';
import axios from 'axios';

@@ -21,15 +20,16 @@ genericRouter.post('/notify', async (req, res) => {
}
if (!req.body.msg) return null;

return axios.post(
process.env.WEBHOOK,
{
text: `hey! ${toSlack.msg}`,
},
{
headers: { 'Content-type': 'application/json' },
},
)
return axios
.post(
process.env.WEBHOOK,
{
text: `_generic notification_: ${toSlack.msg}`,
},
{
headers: { 'Content-type': 'application/json' },
},
)
.then(() => res.send({ message: `received ${req.body.msg} and sent to Slack` }))
.catch(e => console.log(e)); //eslint-disable-line
});

+ 0
- 19
src/index.js View File

@@ -11,32 +11,13 @@ import createRouter from '../routes/createRouter';
import { isMicropubTokenAuthed } from './funcs';

const port = 8086;

const app = express();

export const store = new session.MemoryStore();

app.set('trust proxy', 1); // trust first proxy
app.use(session({
secret: 'keyboard cat',
store,
resave: false,
saveUninitialized: false,
cookie: {
path: false, domain: 'https://post.porknachos.com', secure: false, httpOnly: true, maxAge: 2 * 60 * 60 * 1000,
},
}));

app.use(compression());
app.use(cors());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());

// app.use((req, res, next) => {
// console.log(req.app.locals);
// return next();
// });

app.use('/blog', blogRouter); // original blog-notification
app.use('/generic', genericRouter); // expanded general notificatios
app.use('/color', colorRouter); // for dashboard/color-panel project


Loading…
Cancel
Save