Refactored code.
This commit is contained in:
parent
1ae2dd3609
commit
80ef1b595f
@ -1,10 +1,11 @@
|
||||
import {Context} from "@netlify/functions";
|
||||
import axios from 'axios';
|
||||
|
||||
export default async (req: Request, context: Context) => {
|
||||
try {
|
||||
console.log(req.method);
|
||||
const origin = req.headers['origin'];
|
||||
const baseurl = 'https://syncdb-service-d3f974de56ef.herokuapp.com/';
|
||||
const auth = 'admin:stM8Lnm@Cuf-tWZHv';
|
||||
const authToken = Buffer.from(auth).toString('base64');
|
||||
|
||||
function buildOptions(req: Request) {
|
||||
if (req.method == 'OPTIONS') {
|
||||
return new Response(
|
||||
new Blob(),
|
||||
@ -18,34 +19,39 @@ export default async (req: Request, context: Context) => {
|
||||
},
|
||||
status: 200
|
||||
})
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
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;
|
||||
const password = params.password;
|
||||
type Params = {
|
||||
username: string,
|
||||
password: string
|
||||
}
|
||||
|
||||
if (!dbKey || !password) {
|
||||
console.log(params);
|
||||
async function checkIfDbExists(params: Params) {
|
||||
console.log("Checking if DB exists");
|
||||
|
||||
if (!params.username || !params.password) {
|
||||
throw new Error('No share key provided');
|
||||
}
|
||||
try {
|
||||
const exist = await axios.head(baseurl + dbKey);
|
||||
console.log('Checking for DB');
|
||||
const exist = await axios.head(baseurl + params.username);
|
||||
if (exist) {
|
||||
return
|
||||
new Response('OK');
|
||||
return true;
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.log("DB not Found");
|
||||
console.log(err);
|
||||
}
|
||||
const auth = 'admin:stM8Lnm@Cuf-tWZHv';
|
||||
const authToken = Buffer.from(auth).toString('base64');
|
||||
return false;
|
||||
}
|
||||
|
||||
async function createDB(params: Params) {
|
||||
console.log("Creating DB");
|
||||
const response = await axios.put(
|
||||
baseurl + dbKey,
|
||||
baseurl + params.username,
|
||||
{},
|
||||
{
|
||||
headers: {
|
||||
@ -55,10 +61,19 @@ export default async (req: Request, context: Context) => {
|
||||
}
|
||||
});
|
||||
const data = await response.data;
|
||||
if (response.status == 201) {
|
||||
const response2 = await axios.put(
|
||||
baseurl + '_users/org.couchdb.user:' + dbKey,
|
||||
{_id: 'org.couchdb.user:' + dbKey, name: dbKey, password: password, roles: [], type: 'user'},
|
||||
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,
|
||||
@ -66,10 +81,14 @@ export default async (req: Request, context: Context) => {
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
});
|
||||
data.auth = response2.data;
|
||||
const authresponse = await axios.put(
|
||||
baseurl + dbKey + '/_security',
|
||||
{admins: {names: [], roles: []}, members: {names: [dbKey], roles: []}},
|
||||
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,
|
||||
@ -78,9 +97,37 @@ export default async (req: Request, context: Context) => {
|
||||
}
|
||||
});
|
||||
}
|
||||
return
|
||||
new Response(
|
||||
new Blob(),
|
||||
|
||||
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: {
|
||||
'Content-Type': 'application/json',
|
||||
@ -93,8 +140,7 @@ export default async (req: Request, context: Context) => {
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
const response = {err: err};
|
||||
return
|
||||
new Response(JSON.stringify(response),
|
||||
return new Response(JSON.stringify(response),
|
||||
{status: 500}
|
||||
)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user