Install packages
yarn add firebase-admin jsonwebtoken ms nanoid
# or
npm install --save firebase-admin jsonwebtoken ms nanoid
Add or update the following environment variables
AUTH_PROVIDERS="firebase"
AUTH_FIREBASE_DRIVER="local"
GOOGLE_APPLICATION_CREDENTIALS=<path/to/firebaseServiceAccount.json>
Make sure to update the DEFAULT_ROLE_NAME variable in the endpoint with the default role you want users to have on sign up.
Add two extension files, one hook and one endpoint.
extensions/hooks/firebase/index.js
extensions/endpoints/firebase/index.js
The hook initializes firebase on app startup.
The endpoint creates an endpoint on /firebase/connect
to accept post requests with firebase id tokens.
The endpoint then verifies the id token, looks up the user by email from the decoded token and creates the user if it does not exist in directus.
The endpoint then returns an access_token and refresh_token for the user.
The implemenation is based on this file: https://github.com/directus/directus/blob/e7ada1f173dc6756cce40743023a8873ed2623e2/api/src/services/authentication.ts
Consider error handling and the emitter
At time of writing, the version of directus is 9.8.0
How do you use it on the front end? Does it mean one can use directus sdk?