Refactored code.
This commit is contained in:
parent
1ae2dd3609
commit
80ef1b595f
@ -1,86 +1,133 @@
|
|||||||
import {Context} from "@netlify/functions";
|
import {Context} from "@netlify/functions";
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
export default async (req: Request, context: Context) => {
|
const baseurl = 'https://syncdb-service-d3f974de56ef.herokuapp.com/';
|
||||||
try {
|
const auth = 'admin:stM8Lnm@Cuf-tWZHv';
|
||||||
console.log(req.method);
|
const authToken = Buffer.from(auth).toString('base64');
|
||||||
const origin = req.headers['origin'];
|
|
||||||
if (req.method == 'OPTIONS') {
|
|
||||||
return new Response(
|
|
||||||
new Blob(),
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
'Allow': 'POST',
|
|
||||||
'Max-Age': '86400',
|
|
||||||
'Access-Control-Allow-Methods': 'POST',
|
|
||||||
'Access-Control-Allow-Origin': origin ? origin : 'https://cameras.immersiveidea.com',
|
|
||||||
'Access-Control-Allow-Credentials': 'true'
|
|
||||||
},
|
|
||||||
status: 200
|
|
||||||
})
|
|
||||||
}
|
|
||||||
const baseurl = 'https://syncdb-service-d3f974de56ef.herokuapp.com/';
|
|
||||||
console.log(baseurl);
|
|
||||||
const params = JSON.parse(await req.text());
|
|
||||||
console.log(params);
|
|
||||||
|
|
||||||
const dbKey = params.username;
|
function buildOptions(req: Request) {
|
||||||
const password = params.password;
|
if (req.method == 'OPTIONS') {
|
||||||
|
return new Response(
|
||||||
if (!dbKey || !password) {
|
new Blob(),
|
||||||
console.log(params);
|
|
||||||
throw new Error('No share key provided');
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const exist = await axios.head(baseurl + dbKey);
|
|
||||||
if (exist) {
|
|
||||||
return
|
|
||||||
new Response('OK');
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
const auth = 'admin:stM8Lnm@Cuf-tWZHv';
|
|
||||||
const authToken = Buffer.from(auth).toString('base64');
|
|
||||||
|
|
||||||
const response = await axios.put(
|
|
||||||
baseurl + dbKey,
|
|
||||||
{},
|
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': 'Basic ' + authToken,
|
'Allow': 'POST',
|
||||||
'Content-Type': 'application/json',
|
'Max-Age': '86400',
|
||||||
'Accept': 'application/json'
|
'Access-Control-Allow-Methods': 'POST',
|
||||||
}
|
'Access-Control-Allow-Origin': origin ? origin : 'https://cameras.immersiveidea.com',
|
||||||
});
|
'Access-Control-Allow-Credentials': 'true'
|
||||||
const data = await response.data;
|
},
|
||||||
if (response.status == 201) {
|
status: 200
|
||||||
const response2 = await axios.put(
|
})
|
||||||
baseurl + '_users/org.couchdb.user:' + dbKey,
|
} else {
|
||||||
{_id: 'org.couchdb.user:' + dbKey, name: dbKey, password: password, roles: [], type: 'user'},
|
return null;
|
||||||
{
|
}
|
||||||
headers: {
|
}
|
||||||
'Authorization': 'Basic ' + authToken,
|
|
||||||
'Content-Type': 'application/json',
|
type Params = {
|
||||||
'Accept': 'application/json'
|
username: string,
|
||||||
}
|
password: string
|
||||||
});
|
}
|
||||||
data.auth = response2.data;
|
|
||||||
const authresponse = await axios.put(
|
async function checkIfDbExists(params: Params) {
|
||||||
baseurl + dbKey + '/_security',
|
console.log("Checking if DB exists");
|
||||||
{admins: {names: [], roles: []}, members: {names: [dbKey], roles: []}},
|
|
||||||
{
|
if (!params.username || !params.password) {
|
||||||
headers: {
|
throw new Error('No share key provided');
|
||||||
'Authorization': 'Basic ' + authToken,
|
}
|
||||||
'Content-Type': 'application/json',
|
try {
|
||||||
'Accept': 'application/json'
|
console.log('Checking for DB');
|
||||||
}
|
const exist = await axios.head(baseurl + params.username);
|
||||||
});
|
if (exist) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return
|
} catch (err) {
|
||||||
new Response(
|
console.log("DB not Found");
|
||||||
new Blob(),
|
console.log(err);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createDB(params: Params) {
|
||||||
|
console.log("Creating DB");
|
||||||
|
const response = await axios.put(
|
||||||
|
baseurl + params.username,
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Authorization': 'Basic ' + authToken,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Accept': 'application/json'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const data = await response.data;
|
||||||
|
console.log(JSON.stringify(response));
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createUser(params: Params) {
|
||||||
|
console.log("Creating User");
|
||||||
|
const userResponse = await axios.put(
|
||||||
|
baseurl + '_users/org.couchdb.user:' + params.username,
|
||||||
|
{
|
||||||
|
_id: 'org.couchdb.user:' + params.username,
|
||||||
|
name: params.username,
|
||||||
|
password: params.password, roles: [], type: 'user'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Authorization': 'Basic ' + authToken,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Accept': 'application/json'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return userResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function authorizeUser(params: Params) {
|
||||||
|
console.log("Authorizing User");
|
||||||
|
return await axios.put(
|
||||||
|
baseurl + params.username + '/_security',
|
||||||
|
{admins: {names: [], roles: []}, members: {names: [params.username], roles: []}},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Authorization': 'Basic ' + authToken,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Accept': 'application/json'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async (req: Request, context: Context) => {
|
||||||
|
console.log(req.method);
|
||||||
|
const origin = req.headers['origin'];
|
||||||
|
const options = buildOptions(req);
|
||||||
|
if (options) {
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const params = JSON.parse(await req.text());
|
||||||
|
console.log(params);
|
||||||
|
const exists = await checkIfDbExists(params);
|
||||||
|
if (exists) {
|
||||||
|
return new Response("OK");
|
||||||
|
}
|
||||||
|
const createDbResponse = await createDB(params);
|
||||||
|
if (createDbResponse.status != 201) {
|
||||||
|
throw new Error('Could not create DB');
|
||||||
|
}
|
||||||
|
const createUserResponse = await createUser(params);
|
||||||
|
if (createUserResponse.status != 201) {
|
||||||
|
throw new Error('Could not create User');
|
||||||
|
}
|
||||||
|
const authorizeUserResponse = await authorizeUser(params);
|
||||||
|
if (authorizeUserResponse.status != 200) {
|
||||||
|
throw new Error('could not authorize user');
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
"OK",
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@ -93,8 +140,7 @@ export default async (req: Request, context: Context) => {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
const response = {err: err};
|
const response = {err: err};
|
||||||
return
|
return new Response(JSON.stringify(response),
|
||||||
new Response(JSON.stringify(response),
|
|
||||||
{status: 500}
|
{status: 500}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user