64 lines
2.1 KiB
JavaScript
64 lines
2.1 KiB
JavaScript
const functions = require('@google-cloud/functions-framework');
|
|
|
|
functions.http('run', async (req, res) => {
|
|
|
|
|
|
// User-Defined Variables
|
|
|
|
const allowed_origins = ["http://localhost:5500", "https://projektrising.ai"];
|
|
|
|
// End of User-Defined Variables
|
|
|
|
|
|
|
|
|
|
const origin = req?.query?.origin;
|
|
|
|
if (allowed_origins.includes(origin)) {
|
|
res.set('Access-Control-Allow-Origin', "https://aifunctions.oliver.solutions");
|
|
console.log("dynamic set origin to: ", "https://aifunctions.oliver.solutions");
|
|
} else {
|
|
res.set('Access-Control-Allow-Origin', "https://baic.oliver.solutions");
|
|
console.log("default set origin to: ", "https://baic.oliver.solutions");
|
|
}
|
|
|
|
res.set("Access-Control-Allow-Credentials", "true");
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
res.set('Access-Control-Allow-Methods', 'GET');
|
|
res.set('Access-Control-Allow-Headers', 'Content-Type');
|
|
res.set('Access-Control-Max-Age', '3600');
|
|
return res.status(204).send('');
|
|
}
|
|
|
|
const make_request_url = req?.body?.url;
|
|
let make_request_body = req?.body?.body;
|
|
|
|
if (make_request_url === "clear_cookies") {
|
|
res.set('Set-Cookie', 'AuthToken=undefined; HttpOnly; Secure; SameSite=None');
|
|
return res.send({ message: "Cookies Cleared" });
|
|
}
|
|
|
|
let make_request_options = { method: "POST" }
|
|
if(make_request_body) make_request_options.body = JSON.stringify(make_request_body)
|
|
let auth_token = false;
|
|
try {
|
|
auth_token = req?.headers?.cookie?.split("; ").filter((e) => e.split("=")[0] === "AuthToken")[0].split("=")[1];
|
|
if (auth_token === "undefined") auth_token = undefined;
|
|
} catch {
|
|
console.log("");
|
|
}
|
|
|
|
if (auth_token) make_request_options.headers = { "Content-Type": "application/json", Authorization: "Bearer " + auth_token };
|
|
console.log("make_request_options", make_request_options);
|
|
|
|
const make_res = await fetch(make_request_url, make_request_options);
|
|
const make_data = await make_res.json();
|
|
|
|
if (make_data?.token !== undefined) {
|
|
res.set('Set-Cookie', 'AuthToken=' + make_data?.token + '; HttpOnly; Secure; SameSite=None');
|
|
res.send({ "token": "Success" });
|
|
} else {
|
|
res.send(make_data);
|
|
}
|
|
});
|