From 980024dfecc26a7a672351eceac272c71eba880a Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Fri, 13 Oct 2023 09:34:13 -0500 Subject: [PATCH] Updated user function --- netlify/functions/users/users.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/netlify/functions/users/users.ts b/netlify/functions/users/users.ts index 70d7a40..32c0b23 100644 --- a/netlify/functions/users/users.ts +++ b/netlify/functions/users/users.ts @@ -3,16 +3,24 @@ import axios from 'axios'; export const handler: Handler = async (event: HandlerEvent, context: HandlerContext) => { try { + const origin = event.headers.origin; const baseurl = 'https://syncdb-service-d3f974de56ef.herokuapp.com/'; const more = 'mike/_all_docs?include_docs=true' const dbKey = event.queryStringParameters.shareKey; if (!dbKey) { throw new Error('No share key provided'); } - const exist = await axios.head(baseurl + dbKey); - if (exist.status == 200) { - throw new Error('Share key already exists'); + try { + const exist = await axios.head(baseurl + dbKey); + return { + statusCode: 200, + body: JSON.stringify({data: exist.data}) + } + } catch (err) { + console.log(err); + } + const response = await axios.put( baseurl + dbKey, null, @@ -21,7 +29,7 @@ export const handler: Handler = async (event: HandlerEvent, context: HandlerCont return { headers: { 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': 'https://cameras.immersiveidea.com', + 'Access-Control-Allow-Origin': origin ? origin : 'https://cameras.immersiveidea.com', 'Access-Control-Allow-Credentials': 'true' }, statusCode: 200,