Skip to content
Snippets Groups Projects
Commit aa4c906f authored by David Mynors's avatar David Mynors
Browse files

Add mastodon oauth for meet.coop-registration-form

parent d240a962
Branches
No related tags found
1 merge request!2Add mastodon oauth for meet.coop-registration-form
const axios = require('axios');
const url = require('url');
// TODO: logging
let config = require('./config.js');
const baseUrl = config.theme.urls.base;
config = config.oauth;
const ensureAuthenticated = async (req, res, next) => {
const code = req.url.split('?code=')[1];
const redirectUri = baseUrl + req.url;
if (!code) {
try {
return res.redirect(
`${config.instance}/oauth/authorize?client_id=${config.clientId}&scope=read:accounts&redirect_uri=${redirectUri}&response_type=code`
);
} catch (err) {
console.log('error redirecting to login:', err);
return res.json("an error ocurred")
}
}
const params = new url.URLSearchParams({
client_id: config.clientId,
client_secret: config.clientSecret,
redirect_uri: redirectUri,
grant_type: 'authorization_code',
code: code,
scope: 'read:accounts',
});
let token;
try {
const tokenResponse = await axios.post(
`${config.instance}/oauth/token`,
params.toString()
);
token = tokenResponse.data.access_token;
} catch (err) {
console.log('error requesting access token:', err.response.data || err);
return res.json("an error ocurred")
}
try {
const profileResponse = await axios.get(
`${config.instance}/api/v1/accounts/verify_credentials`,
{
headers: { Authorization: 'Bearer ' + token },
}
);
if (profileResponse.status === 200) {
console.log(
'successfully logged in',
profileResponse.data.username
);
next();
} else {
throw new Error(
`verify_credentials response was ${profileResponse.status}, body: ${profileResponse.data}`
);
}
} catch (err) {
console.log('error verifying credentials:', err.response.data || err);
return res.json("an error ocurred")
}
};
module.exports = ensureAuthenticated;
'use strict';
const express = require('express');
const fs = require('fs');
const ensureAuthenticated = require('./lib/auth.js')
const templates = require('./lib/templates.js');
const metalsmith = require('./metalsmith.js');
const email = require('./lib/email.js');
......@@ -72,6 +73,9 @@ app.get('/', (req, res) => {
app.get('/style.css', (req, res) => {
res.sendFile(req.path, fileOptions);
});
app.get(/^\/meet.coop-registration-form(.html)?/, ensureAuthenticated, (req, res, next) => {
next();
});
app.get('*', (req, res, next) => {
var path = req.path+'/index.html';
if (fs.existsSync(fileOptions.root+path)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment