mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
fix typings and edge config key issue
This commit is contained in:
31
www/app/lib/edgeConfig.ts
Normal file
31
www/app/lib/edgeConfig.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { get } from "@vercel/edge-config";
|
||||
|
||||
type EdgeConfig = {
|
||||
[domainWithDash: string]: {
|
||||
features: {
|
||||
[featureName in "requireLogin" | "privacy" | "browse"]: boolean;
|
||||
};
|
||||
auth_callback_url: string;
|
||||
api_url: string;
|
||||
};
|
||||
};
|
||||
|
||||
export type DomainConfig = EdgeConfig["domainWithDash"];
|
||||
|
||||
// Edge config main keys can only be alphanumeric and _ or -
|
||||
export function edgeKeyToDomain(key: string) {
|
||||
return key.replaceAll(".", "_");
|
||||
}
|
||||
|
||||
export function edgeDomainToKey(domain: string) {
|
||||
return domain.replaceAll("_", ".");
|
||||
}
|
||||
|
||||
// get edge config server-side (prefer DomainContext when available), domain is the hostname
|
||||
export async function getConfig(domain: string) {
|
||||
const config = await get(edgeDomainToKey(domain));
|
||||
|
||||
if (typeof config !== "object") throw Error("Error fetchig config");
|
||||
|
||||
return config as DomainConfig;
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
import { Fief, FiefUserInfo } from "@fief/fief";
|
||||
import { FiefAuth, IUserInfoCache } from "@fief/fief/nextjs";
|
||||
import { get } from "@vercel/edge-config";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { useError } from "../(errors)/errorContext";
|
||||
import { getConfig } from "./edgeConfig";
|
||||
|
||||
export const SESSION_COOKIE_NAME = "reflector-auth";
|
||||
|
||||
@@ -46,12 +44,12 @@ export const getFiefAuth = async (url: URL) => {
|
||||
if (FIEF_AUTHS[url.hostname]) {
|
||||
return FIEF_AUTHS[url.hostname];
|
||||
} else {
|
||||
const config = url && (await get(url.hostname));
|
||||
const config = url && (await getConfig(url.hostname));
|
||||
if (config) {
|
||||
FIEF_AUTHS[url.hostname] = new FiefAuth({
|
||||
client: fiefClient,
|
||||
sessionCookieName: SESSION_COOKIE_NAME,
|
||||
redirectURI: config["auth_callback_url"],
|
||||
redirectURI: config.auth_callback_url,
|
||||
logoutRedirectURI: url.origin,
|
||||
userInfoCache: new MemoryUserInfoCache(),
|
||||
});
|
||||
|
||||
@@ -7,7 +7,7 @@ import { DomainContext } from "../[domain]/domainContext";
|
||||
|
||||
export default function getApi(): DefaultApi {
|
||||
const accessTokenInfo = useFiefAccessTokenInfo();
|
||||
const api_url = useContext(DomainContext).apiUrl;
|
||||
const api_url = useContext(DomainContext).api_url;
|
||||
if (!api_url) throw new Error("no API URL");
|
||||
|
||||
const apiConfiguration = new Configuration({
|
||||
|
||||
Reference in New Issue
Block a user