trying more stuff
This commit is contained in:
parent
41f401aa45
commit
4daf54106b
|
@ -6,9 +6,13 @@
|
||||||
"hosting": {
|
"hosting": {
|
||||||
"public": "public",
|
"public": "public",
|
||||||
"rewrites": [
|
"rewrites": [
|
||||||
{
|
{
|
||||||
"source": "/testme",
|
"source": "/publishDictionary",
|
||||||
"function": "testme"
|
"function": "/publishDictionary"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"source": "/willError",
|
||||||
|
"function": "/willError"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,29 +1,24 @@
|
||||||
import * as functions from "firebase-functions";
|
import * as functions from "firebase-functions";
|
||||||
import fetch from "node-fetch";
|
import * as FT from "../../website/src/lib/functions-types";
|
||||||
import cors from "cors";
|
import auth from "./middleware/lingdocs-auth";
|
||||||
// import publish from "./publish";
|
import publish from "./publish";
|
||||||
// import * as BT from "../../website/src/lib/backend-types"
|
|
||||||
|
|
||||||
export const testme = functions
|
export const publishDictionary = functions.https.onRequest(
|
||||||
// .runWith({
|
auth((req, res: functions.Response<FT.PublishDictionaryResponse | FT.FunctionError>) => {
|
||||||
// timeoutSeconds: 200,
|
if (req.user.level !== "editor") {
|
||||||
// memory: "2GB"
|
res.status(403).send({ ok: false, error: "403 forbidden" });
|
||||||
// })
|
|
||||||
.https.onRequest((req, res) => {
|
|
||||||
return cors({ credentials: true, origin: /\.lingdocs\.com$/ })(req, res, () => {
|
|
||||||
const { headers: { cookie }} = req;
|
|
||||||
if (!cookie) {
|
|
||||||
return res.status(401).send({ ok: false, error: "unauthorized" });
|
|
||||||
}
|
|
||||||
fetch("https://account.lingdocs.com/api/user", {
|
|
||||||
headers: { cookie },
|
|
||||||
}).then(r => r.json()).then(r => {
|
|
||||||
res.send({ ok: true, r });
|
|
||||||
}).catch((error) => res.send({ ok: false, error }));
|
|
||||||
return;
|
return;
|
||||||
});
|
}
|
||||||
});
|
publish().then(res.send);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
export const willError = functions.https.onRequest((req, res) => {
|
||||||
|
auth((req, res: functions.Response<FT.PublishDictionaryResponse | FT.FunctionError>) => {
|
||||||
|
throw new Error("this is an error");
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
// TODO: BETTER HANDLING OF EXPRESS MIDDLEWARE
|
// TODO: BETTER HANDLING OF EXPRESS MIDDLEWARE
|
||||||
|
|
||||||
export const submissions = functions
|
export const submissions = functions
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
import cors from "cors";
|
||||||
|
import fetch from "node-fetch";
|
||||||
|
import type { https, Response } from "firebase-functions";
|
||||||
|
import * as FT from "../../../website/src/lib/functions-types";
|
||||||
|
import type { LingdocsUser } from "../../../website/src/lib/account-types";
|
||||||
|
|
||||||
|
const useCors = cors({ credentials: true, origin: /\.lingdocs\.com$/ });
|
||||||
|
|
||||||
|
interface ReqWUser extends https.Request {
|
||||||
|
user: LingdocsUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* creates a handler to pass to a firebase https.onRequest function
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export default function makeHandler(toRun: (req: ReqWUser, res: Response<FT.FunctionResponse>) => any | Promise<any>): (req: https.Request, res: Response<any>) => void | Promise<void> {
|
||||||
|
return function(reqPlain: https.Request, resPlain: Response<any>) {
|
||||||
|
return useCors(reqPlain, resPlain, async () => {
|
||||||
|
const { req, res } = await lingdocsAuth(reqPlain, resPlain);
|
||||||
|
if (!req) {
|
||||||
|
res.status(401).send({ ok: false, error: "unauthorized" });
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
toRun(req, res);
|
||||||
|
return;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function lingdocsAuth(req: https.Request, res: Response<any>): Promise<{ req: ReqWUser | null, res: Response<FT.FunctionResponse> }> {
|
||||||
|
const { headers: { cookie }} = req;
|
||||||
|
if (!cookie) {
|
||||||
|
return { req: null, res };
|
||||||
|
}
|
||||||
|
const r = await fetch("https://account.lingdocs.com/api/user", { headers: { cookie }})
|
||||||
|
const { ok, user } = await r.json();
|
||||||
|
if (ok === true && user) {
|
||||||
|
req.user = user;
|
||||||
|
return { req: req as ReqWUser, res };
|
||||||
|
}
|
||||||
|
return { req: null, res };
|
||||||
|
}
|
|
@ -22,12 +22,11 @@ async function accountApiFetch(url: string, method: "GET" | "POST" | "PUT" | "DE
|
||||||
return await response.json() as AT.APIResponse;
|
return await response.json() as AT.APIResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function publishDictionary(): Promise<FT.PublishDictionaryResponse> {
|
export async function publishDictionary(): Promise<FT.PublishDictionaryResponse | FT.FunctionError> {
|
||||||
return {
|
const r = await fetch("https://functions.lingdocs.com/publishDictionary", {
|
||||||
ok: true,
|
credentials: "include",
|
||||||
// @ts-ignore
|
});
|
||||||
info: {},
|
return (await r.json()) as FT.PublishDictionaryResponse | FT.FunctionError;
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function upgradeAccount(password: string): Promise<AT.UpgradeUserResponse> {
|
export async function upgradeAccount(password: string): Promise<AT.UpgradeUserResponse> {
|
||||||
|
|
|
@ -9,6 +9,10 @@
|
||||||
import { Types as T } from "@lingdocs/pashto-inflector";
|
import { Types as T } from "@lingdocs/pashto-inflector";
|
||||||
import * as AT from "./account-types";
|
import * as AT from "./account-types";
|
||||||
|
|
||||||
|
export type FunctionResponse = PublishDictionaryResponse | SubmissionsResponse | FunctionError;
|
||||||
|
|
||||||
|
export type FunctionError = { ok: false, error: string };
|
||||||
|
|
||||||
export type PublishDictionaryResponse = {
|
export type PublishDictionaryResponse = {
|
||||||
ok: true,
|
ok: true,
|
||||||
info: T.DictionaryInfo,
|
info: T.DictionaryInfo,
|
||||||
|
|
|
@ -6,7 +6,7 @@ import { Modal, Button } from "react-bootstrap";
|
||||||
import {
|
import {
|
||||||
upgradeAccount,
|
upgradeAccount,
|
||||||
signOut,
|
signOut,
|
||||||
// publishDictionary,
|
publishDictionary,
|
||||||
} from "../lib/backend-calls";
|
} from "../lib/backend-calls";
|
||||||
import LoadingElipses from "../components/LoadingElipses";
|
import LoadingElipses from "../components/LoadingElipses";
|
||||||
import { Helmet } from "react-helmet";
|
import { Helmet } from "react-helmet";
|
||||||
|
@ -24,16 +24,15 @@ const Account = ({ user, loadUser }: { user: AT.LingdocsUser | undefined, loadUs
|
||||||
const [upgradePassword, setUpgradePassword] = useState<string>("");
|
const [upgradePassword, setUpgradePassword] = useState<string>("");
|
||||||
const [upgradeError, setUpgradeError] = useState<string>("");
|
const [upgradeError, setUpgradeError] = useState<string>("");
|
||||||
const [waiting, setWaiting] = useState<boolean>(false);
|
const [waiting, setWaiting] = useState<boolean>(false);
|
||||||
// const [popup, setPopup] = useState<WindowProxy | null>(null);
|
const [publishingStatus, setPublishingStatus] = useState<undefined | "publishing" | any>(undefined);
|
||||||
// const [publishingStatus, setPublishingStatus] = useState<undefined | "publishing" | any>(undefined);
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setShowingUpgradePrompt(false);
|
setShowingUpgradePrompt(false);
|
||||||
setUpgradeError("");
|
setUpgradeError("");
|
||||||
setWaiting(false);
|
setWaiting(false);
|
||||||
window.addEventListener("message", handleIncomingMessage);
|
window.addEventListener("message", handleIncomingMessage);
|
||||||
console.log("send test func");
|
console.log("send test erroring func");
|
||||||
fetch("https://functions.lingdocs.com/testme", { credentials: "include" }).then((res) => res.text()).then((res) => {
|
fetch("https://functions.lingdocs.com/willError", { credentials: "include" }).then((res) => res.text()).then((res) => {
|
||||||
console.log("test func here");
|
console.log("test error here");
|
||||||
console.log(res);
|
console.log(res);
|
||||||
});
|
});
|
||||||
return () => {
|
return () => {
|
||||||
|
@ -76,15 +75,15 @@ const Account = ({ user, loadUser }: { user: AT.LingdocsUser | undefined, loadUs
|
||||||
function handleOpenSignup() {
|
function handleOpenSignup() {
|
||||||
popupRef = window.open("https://account.lingdocs.com");
|
popupRef = window.open("https://account.lingdocs.com");
|
||||||
}
|
}
|
||||||
// function handlePublish() {
|
function handlePublish() {
|
||||||
// setPublishingStatus("publishing");
|
setPublishingStatus("publishing");
|
||||||
// publishDictionary().then((response) => {
|
publishDictionary().then((response) => {
|
||||||
// setPublishingStatus(response);
|
setPublishingStatus(response);
|
||||||
// }).catch((err) => {
|
}).catch((err) => {
|
||||||
// console.error(err);
|
console.error(err);
|
||||||
// setPublishingStatus("Offline or connection error");
|
setPublishingStatus("Offline or connection error");
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return <div className="text-center mt-3">
|
return <div className="text-center mt-3">
|
||||||
<Helmet>
|
<Helmet>
|
||||||
|
@ -104,7 +103,7 @@ const Account = ({ user, loadUser }: { user: AT.LingdocsUser | undefined, loadUs
|
||||||
<title>Account - LingDocs Pashto Dictionary</title>
|
<title>Account - LingDocs Pashto Dictionary</title>
|
||||||
</Helmet>
|
</Helmet>
|
||||||
<h2 className="mb-4">Account</h2>
|
<h2 className="mb-4">Account</h2>
|
||||||
{/* {user.level === "editor" &&
|
{user.level === "editor" &&
|
||||||
<div className="mb-3">
|
<div className="mb-3">
|
||||||
<h4>Editor Tools</h4>
|
<h4>Editor Tools</h4>
|
||||||
{publishingStatus !== "publishing" &&
|
{publishingStatus !== "publishing" &&
|
||||||
|
@ -123,7 +122,7 @@ const Account = ({ user, loadUser }: { user: AT.LingdocsUser | undefined, loadUs
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
} */}
|
}
|
||||||
<div>
|
<div>
|
||||||
{/* {user.p && <div className="mb-4 mt-3" style={{ textAlign: "center" }}>
|
{/* {user.p && <div className="mb-4 mt-3" style={{ textAlign: "center" }}>
|
||||||
<img src={user.photoURL} data-testid="userAvatar" alt="avatar" style={{ borderRadius: "50%", width: "5rem", height: "5rem" }}/>
|
<img src={user.photoURL} data-testid="userAvatar" alt="avatar" style={{ borderRadius: "50%", width: "5rem", height: "5rem" }}/>
|
||||||
|
|
Loading…
Reference in New Issue