Skip to content

feat(root-cms): support custom storage bucket project #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/storage-bucket-project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"@blinkk/root-cms": minor
---
feat: add `storageConfig` option to support storage buckets in a different Firebase project.
6 changes: 6 additions & 0 deletions docs/root.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export default defineConfig({
projectId: 'rootjs-dev',
storageBucket: 'rootjs-dev.appspot.com',
},
storageConfig: {
apiKey: 'AIzaSyDIoi6zECKeyJoCduYEmV5j9PIF-wbpaPo',
authDomain: 'rootjs-dev.firebaseapp.com',
projectId: 'rootjs-dev',
storageBucket: 'rootjs-dev.appspot.com',
},
gapi: {
apiKey: process.env.GAPI_API_KEY,
clientId: process.env.GAPI_CLIENT_ID,
Expand Down
6 changes: 6 additions & 0 deletions examples/blog/root.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export default defineConfig({
appId: '1:636169634531:web:7b8fe398f10e5d9c4e7bd6',
measurementId: 'G-5JTQHSPWBB',
},
storageConfig: {
apiKey: 'AIzaSyDIoi6zECKeyJoCduYEmV5j9PIF-wbpaPo',
authDomain: 'rootjs-dev.firebaseapp.com',
projectId: 'rootjs-dev',
storageBucket: 'rootjs-dev.appspot.com',
},
gapi: {
apiKey: process.env.GAPI_API_KEY,
clientId: process.env.GAPI_CLIENT_ID,
Expand Down
6 changes: 6 additions & 0 deletions examples/cms/root.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export default defineConfig({
projectId: 'rootjs-dev',
storageBucket: 'rootjs-dev.appspot.com',
},
storageConfig: {
apiKey: 'AIzaSyDIoi6zECKeyJoCduYEmV5j9PIF-wbpaPo',
authDomain: 'rootjs-dev.firebaseapp.com',
projectId: 'rootjs-dev',
storageBucket: 'rootjs-dev.appspot.com',
},
gapi: {
apiKey: process.env.GAPI_API_KEY,
clientId: process.env.GAPI_CLIENT_ID,
Expand Down
2 changes: 2 additions & 0 deletions packages/root-cms/core/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export async function renderApp(
},
},
firebaseConfig: cmsConfig.firebaseConfig,
storageConfig: cmsConfig.storageConfig,
gapi: cmsConfig.gapi,
collections: collections,
sidebar: cmsConfig.sidebar,
Expand Down Expand Up @@ -220,6 +221,7 @@ export async function renderSignIn(
const ctx = {
name: options.cmsConfig.name || options.cmsConfig.id || '',
firebaseConfig: options.cmsConfig.firebaseConfig,
storageConfig: options.cmsConfig.storageConfig,
};
const mainHtml = renderToString(<SignIn title="Sign in" ctx={ctx} />);
let html = `<!doctype html>\n${mainHtml}`;
Expand Down
24 changes: 16 additions & 8 deletions packages/root-cms/core/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ export interface CMSSidebarTool {
iframeUrl?: string;
}

export interface CMSFirebaseConfig {
[key: string]: string | undefined;
apiKey: string;
authDomain: string;
projectId: string;
storageBucket: string;
databaseId?: string;
}

export type CMSPluginOptions = {
/**
* The ID of the project. Data will be stored under the namespace
Expand All @@ -76,14 +85,13 @@ export type CMSPluginOptions = {
* Firebase config object, which can be obtained in the Firebase Console by
* going to "Project Settings".
*/
firebaseConfig: {
[key: string]: string | undefined;
apiKey: string;
authDomain: string;
projectId: string;
storageBucket: string;
databaseId?: string;
};
firebaseConfig: CMSFirebaseConfig;

/**
* Optional Firebase config used for interacting with a storage bucket that
* exists in a different Firebase project.
*/
storageConfig?: CMSFirebaseConfig;

/**
* GAPI credentials. Include if using Google Drive and Google Sheets features.
Expand Down
1 change: 1 addition & 0 deletions packages/root-cms/signin/signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare global {
__ROOT_CTX: {
name: string;
firebaseConfig: Record<string, string>;
storageConfig?: Record<string, string>;
};
firebase: {
app: FirebaseApp;
Expand Down
13 changes: 11 additions & 2 deletions packages/root-cms/ui/ui.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {MantineProvider} from '@mantine/core';
import {ModalsProvider} from '@mantine/modals';
import {NotificationsProvider} from '@mantine/notifications';
import {initializeApp} from 'firebase/app';
import {initializeApp, getApp} from 'firebase/app';
import {User, getAuth} from 'firebase/auth';
import {initializeFirestore} from 'firebase/firestore';
import {getStorage} from 'firebase/storage';
Expand Down Expand Up @@ -66,6 +66,7 @@ declare global {
};
};
firebaseConfig: Record<string, string>;
storageConfig?: Record<string, string>;
gapi?: {
apiKey: string;
clientId: string;
Expand Down Expand Up @@ -169,6 +170,14 @@ function loginRedirect() {
}

const app = initializeApp(window.__ROOT_CTX.firebaseConfig);
let storageApp = app;
if (window.__ROOT_CTX.storageConfig) {
try {
storageApp = initializeApp(window.__ROOT_CTX.storageConfig, 'storage');
} catch (err) {
storageApp = getApp('storage');
}
}
const databaseId = window.__ROOT_CTX.firebaseConfig.databaseId || '(default)';
// const db = getFirestore(app);
// NOTE(stevenle): the firestore web channel rpc sometimes has issues in
Expand All @@ -184,7 +193,7 @@ const db = initializeFirestore(
databaseId
);
const auth = getAuth(app);
const storage = getStorage(app);
const storage = getStorage(storageApp);
auth.onAuthStateChanged((user) => {
if (!user) {
loginRedirect();
Expand Down