Accessing the initialized app? #61
steveruizok
started this conversation in
Ideas
Replies: 3 comments 10 replies
-
Maybe scratch that—after a little more exploration, it makes more sense for me to initialize firebase myself. Still could be nice to get the admin instance however. Here's are the files all together. // utils/firebase.js
import firebase from 'firebase/app'
import 'firebase/firestore'
if (!firebase.apps.length) {
firebase.initializeApp({
// ...config object (same as in config.firebaseClientInitConfig)
})
}
export default firebase // utils/admin.js
import './firebase'
import admin from 'firebase-admin'
export default admin.apps[0] // utils/firestore.js
import firebase from './firebase'
export default firebase.firestore() // utils/initAuth.js
import './firebase'
import { init } from 'next-firebase-auth'
// ...initAuth as shown in the example, but with only the apiKey for firebaseClientInitConfig // pages/example.js
import {
useAuthUser,
withAuthUser,
AuthAction,
withAuthUserTokenSSR,
} from 'next-firebase-auth'
import db from '../utils/firestore'
import admin from '../utils/admin'
function Editor({ message }) {
const AuthUser = useAuthUser()
return (
<textarea
defaultValue={message}
onChange={({ currentTarget: { value } }) =>
db.collection('messages').doc(AuthUser.id).set({ message: value })
}
/>
)
}
export const getServerSideProps = withAuthUserTokenSSR({
whenUnauthed: AuthAction.REDIRECT_TO_LOGIN,
})(async ({ AuthUser }) => {
const db = admin.firestore()
const doc = await db.collection('messages').doc(AuthUser.id).get()
return { message: doc.exists ? doc.data().message : '...' }
})
export default withAuthUser({
whenUnauthedAfterInit: AuthAction.REDIRECT_TO_LOGIN,
})(Editor) |
Beta Was this translation helpful? Give feedback.
5 replies
-
Right on, having access to that admin instance would be really handy for projects that use Firestore (or other Firebase services).
… On 30 Jan 2021, at 23:03, Kevin Jennison ***@***.***> wrote:
Thanks for the feedback! So, in your case you need access to the Firebase app's admin within getServerSideProps? If this is a very common pattern for people, we could consider passing FirebaseAdmin to the wrapped function as a convenience—I'd love to hear from others.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
0 replies
-
We're tracking improvements to this in #135. Release v0.13.1-alpha.0 adds a |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey, thanks for this excellent library.
I would like to use it together with Firestore. To do this, I need to access the initialized app. At the moment, the only way to get that instance is from
window.apps
for the client oradmin.apps
on the server.For the client:
For the server:
This feels a little hacky, however.
Would it be possible for the
init
function to return the app instance? TheinitFirebaseClientSDK
could return it, as couldinitFirebaseAdminSDK
, and then we could import it from wherever we callinit
, such asinitAuth.js
in the example. From what I can tell,init
would return the correct result whether server-side or client-side.Beta Was this translation helpful? Give feedback.
All reactions