Implement multi tenancy

This commit is contained in:
Sara
2023-10-27 11:26:00 +02:00
committed by Mathieu Virbel
parent 6bd5247bab
commit 4a69bffc9c
37 changed files with 409 additions and 236 deletions

View File

@@ -1,6 +1,8 @@
"use client";
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";
export const SESSION_COOKIE_NAME = "reflector-auth";
@@ -38,13 +40,54 @@ class MemoryUserInfoCache implements IUserInfoCache {
}
}
export const fiefAuth = new FiefAuth({
client: fiefClient,
sessionCookieName: SESSION_COOKIE_NAME,
redirectURI:
process.env.NEXT_PUBLIC_AUTH_CALLBACK_URL ||
"http://localhost:3000/auth-callback",
logoutRedirectURI:
process.env.NEXT_PUBLIC_SITE_URL || "http://localhost:3000",
userInfoCache: new MemoryUserInfoCache(),
});
const FIEF_AUTHS = {} as { [domain: string]: FiefAuth };
export const getFiefAuth = async (url: URL) => {
if (FIEF_AUTHS[url.hostname]) {
return FIEF_AUTHS[url.hostname];
} else {
const config = url && (await get(url.hostname));
if (config) {
FIEF_AUTHS[url.hostname] = new FiefAuth({
client: fiefClient,
sessionCookieName: SESSION_COOKIE_NAME,
redirectURI: config["auth_callback_url"],
logoutRedirectURI: url.origin,
userInfoCache: new MemoryUserInfoCache(),
});
return FIEF_AUTHS[url.hostname];
} else {
throw new Error("Fief intanciation failed");
}
}
};
export const getFiefAuthMiddleware = async (url) => {
const protectedPaths = [
{
matcher: "/:domain/transcripts",
parameters: {},
},
{
matcher: "/:domain/transcripts/:path*",
parameters: {},
},
{
matcher: "/:domain/browse",
parameters: {},
},
{
matcher: "/transcripts",
parameters: {},
},
{
matcher: "/transcripts/:path*",
parameters: {},
},
{
matcher: "/browse",
parameters: {},
},
];
return (await getFiefAuth(url))?.middleware(protectedPaths);
};

View File

@@ -2,12 +2,16 @@ import { Configuration } from "../api/runtime";
import { DefaultApi } from "../api/apis/DefaultApi";
import { useFiefAccessTokenInfo } from "@fief/fief/nextjs/react";
import { useContext } from "react";
import { DomainContext } from "../[domain]/domainContext";
export default function getApi(): DefaultApi {
const accessTokenInfo = useFiefAccessTokenInfo();
const api_url = useContext(DomainContext).apiUrl;
if (!api_url) throw new Error("no API URL");
const apiConfiguration = new Configuration({
basePath: process.env.NEXT_PUBLIC_API_URL,
basePath: api_url,
accessToken: accessTokenInfo
? "Bearer " + accessTokenInfo.access_token
: undefined,

View File

@@ -1,15 +1,3 @@
export function isDevelopment() {
return process.env.NEXT_PUBLIC_ENV === "development";
}
export function featPrivacy() {
return process.env.NEXT_PUBLIC_FEAT_PRIVACY === "1";
}
export function featBrowse() {
return process.env.NEXT_PUBLIC_FEAT_BROWSE === "1";
}
export function featRequireLogin() {
return process.env.NEXT_PUBLIC_FEAT_LOGIN_REQUIRED === "1";
}