From 80ef1b595f8c13ccc0b8a56ba4161f5e7fc33452 Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Mon, 6 Nov 2023 05:32:01 -0600 Subject: [PATCH] Refactored code. --- netlify/functions/users/users.mts | 202 ++++++++++++++++++------------ 1 file changed, 124 insertions(+), 78 deletions(-) diff --git a/netlify/functions/users/users.mts b/netlify/functions/users/users.mts index 0afd4a1..21df4c6 100644 --- a/netlify/functions/users/users.mts +++ b/netlify/functions/users/users.mts @@ -1,86 +1,133 @@ 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']; - 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 baseurl = 'https://syncdb-service-d3f974de56ef.herokuapp.com/'; +const auth = 'admin:stM8Lnm@Cuf-tWZHv'; +const authToken = Buffer.from(auth).toString('base64'); - const dbKey = params.username; - const password = params.password; - - if (!dbKey || !password) { - 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, - {}, +function buildOptions(req: Request) { + if (req.method == 'OPTIONS') { + return new Response( + new Blob(), { headers: { - 'Authorization': 'Basic ' + authToken, - 'Content-Type': 'application/json', - 'Accept': 'application/json' - } - }); - 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'}, - { - headers: { - 'Authorization': 'Basic ' + authToken, - 'Content-Type': 'application/json', - 'Accept': 'application/json' - } - }); - data.auth = response2.data; - const authresponse = await axios.put( - baseurl + dbKey + '/_security', - {admins: {names: [], roles: []}, members: {names: [dbKey], roles: []}}, - { - headers: { - 'Authorization': 'Basic ' + authToken, - 'Content-Type': 'application/json', - 'Accept': 'application/json' - } - }); + '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 + }) + } else { + return null; + } +} + +type Params = { + username: string, + password: string +} + +async function checkIfDbExists(params: Params) { + console.log("Checking if DB exists"); + + if (!params.username || !params.password) { + throw new Error('No share key provided'); + } + try { + console.log('Checking for DB'); + const exist = await axios.head(baseurl + params.username); + if (exist) { + return true; } - return - new Response( - new Blob(), + } catch (err) { + console.log("DB not Found"); + 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: { '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} ) }