Updated config and OPTIONS method for function.
This commit is contained in:
parent
83a36d412e
commit
57a5d03518
@ -1,4 +1,3 @@
|
|||||||
import {Context} from "@netlify/functions";
|
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
const baseurl = 'https://syncdb-service-d3f974de56ef.herokuapp.com/';
|
const baseurl = 'https://syncdb-service-d3f974de56ef.herokuapp.com/';
|
||||||
@ -124,9 +123,10 @@ async function authorizeUser(params: Params) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (req: Request, context: Context): Promise<Response> => {
|
export default async (req: Request): Promise<Response> => {
|
||||||
console.log(req.method);
|
console.log(req.method);
|
||||||
|
|
||||||
|
try {
|
||||||
if (req.method == 'OPTIONS') {
|
if (req.method == 'OPTIONS') {
|
||||||
const origin = req.headers.get('Origin');
|
const origin = req.headers.get('Origin');
|
||||||
console.log(origin);
|
console.log(origin);
|
||||||
@ -143,6 +143,20 @@ export default async (req: Request, context: Context): Promise<Response> => {
|
|||||||
status: 200
|
status: 200
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify(err),
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Allow': 'POST',
|
||||||
|
'Max-Age': '30',
|
||||||
|
'Access-Control-Allow-Methods': 'POST',
|
||||||
|
'Access-Control-Allow-Origin': origin ? origin : 'https://cameras.immersiveidea.com',
|
||||||
|
'Access-Control-Allow-Credentials': 'true'
|
||||||
|
},
|
||||||
|
status: 500
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -157,18 +171,18 @@ export default async (req: Request, context: Context): Promise<Response> => {
|
|||||||
const exists = await checkIfDbExists(params);
|
const exists = await checkIfDbExists(params);
|
||||||
switch (exists) {
|
switch (exists) {
|
||||||
case Access.ALLOWED:
|
case Access.ALLOWED:
|
||||||
return new Response("OK");
|
console.log('Allowed');
|
||||||
|
return new Response('OK', {status: 200});
|
||||||
case Access.DENIED:
|
case Access.DENIED:
|
||||||
if (exists == Access.DENIED) {
|
console.log('Denied');
|
||||||
return new Response("Denied", {status: 401});
|
return new Response('Denied', {status: 401});
|
||||||
}
|
|
||||||
case Access.MISSING:
|
case Access.MISSING:
|
||||||
if (exists == Access.MISSING) {
|
console.log('Creating Missing DB');
|
||||||
const createDbResponse = await createDB(params);
|
const createDbResponse = await createDB(params);
|
||||||
if (createDbResponse.status != 201) {
|
if (createDbResponse.status != 201) {
|
||||||
throw new Error('Could not create DB');
|
throw new Error('Could not create DB');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,20 +193,20 @@ export default async (req: Request, context: Context): Promise<Response> => {
|
|||||||
}
|
}
|
||||||
const origin = req.headers['origin'];
|
const origin = req.headers['origin'];
|
||||||
return new Response(
|
return new Response(
|
||||||
"OK",
|
'OK',
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Access-Control-Allow-Origin': origin ? origin : 'https://cameras.immersiveidea.com',
|
'Access-Control-Allow-Origin': origin ? origin : 'https://cameras.immersiveidea.com',
|
||||||
'Access-Control-Allow-Credentials': 'true'
|
'Access-Control-Allow-Credentials': 'true'
|
||||||
},
|
},
|
||||||
status: 200,
|
status: 200
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
const response = {err: err};
|
const response = {err: err};
|
||||||
return new Response(JSON.stringify(response),
|
return new Response('Error',
|
||||||
{status: 500}
|
{status: 500}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user