|
| 1 | +--- |
| 2 | +description: '@adminjs/firebase-auth' |
| 3 | +--- |
| 4 | + |
| 5 | +# FirebaseAuthProvider |
| 6 | + |
| 7 | +`@adminjs/firebase-auth` is an authentication provider which allows you to sign in using Firebase Authentication. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +<figure><img src="../../.gitbook/assets/Screenshot 2023-11-22 at 13.25.33.png" alt=""><figcaption></figcaption></figure> |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +Make sure you follow official Firebase documentation to properly set up Firebase Authentication in Firebase Console: [https://firebase.google.com/docs/auth](https://firebase.google.com/docs/auth) |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +`@adminjs/firebase-auth` is a premium feature which can be purchased at [https://cloud.adminjs.co](https://cloud.adminjs.co) |
| 20 | + |
| 21 | +All premium features currently use **One Time Payment** model and you can use them in all apps that belong to you. Once you purchase the addon, you will receive a license key which you should provide in `@adminjs/firebase-auth` configuration in your application's code. |
| 22 | + |
| 23 | +Installing the library: |
| 24 | + |
| 25 | +```bash |
| 26 | +$ yarn add @adminjs/firebase-auth |
| 27 | +``` |
| 28 | + |
| 29 | +The license key should be provided via `FirebaseAuthProvider` constructor: |
| 30 | + |
| 31 | +```typescript |
| 32 | +new FirebaseAuthProvider({ |
| 33 | + licenseKey: process.env.LICENSE_KEY, |
| 34 | + // the rest of the config |
| 35 | +}) |
| 36 | +``` |
| 37 | + |
| 38 | +If you encounter any issues or require help installing the package please contact us through our Discord server. |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +`FirebaseAuthProvider` requires you to prepare Firebase-specific configuration. |
| 43 | + |
| 44 | +#### UI Config |
| 45 | + |
| 46 | +`@adminjs/firebase-auth` uses `firebaseui-web` to generate Firebase UI inside the login form and it requires you to configure it. Please refer to the following link for configuration options: [https://github.com/firebase/firebaseui-web/blob/master/types/index.d.ts#L115](https://github.com/firebase/firebaseui-web/blob/master/types/index.d.ts#L115) |
| 47 | + |
| 48 | +Note that you can only configure raw values but you will not be able to configure functions or callbacks this way. A workaround will be described in the later part of this documentation. |
| 49 | + |
| 50 | +```typescript |
| 51 | +import { EmailAuthProvider } from 'firebase/auth'; |
| 52 | + |
| 53 | +const uiConfig = { |
| 54 | + popupMode: true, |
| 55 | + signInFlow: 'popup', |
| 56 | + signInOptions: [ |
| 57 | + { |
| 58 | + provider: EmailAuthProvider.PROVIDER_ID, |
| 59 | + disableSignUp: { |
| 60 | + status: true, |
| 61 | + }, |
| 62 | + }, |
| 63 | + ], |
| 64 | +}; |
| 65 | +``` |
| 66 | + |
| 67 | +#### Firebase Configuration |
| 68 | + |
| 69 | +`@adminjs/firebase-auth` initializes a Firebase App in the Login view. Make sure you copy the configuration from your project in Firebase Console. |
| 70 | + |
| 71 | +```typescript |
| 72 | +const firebaseConfig = { |
| 73 | + apiKey: 'AIza...', |
| 74 | + authDomain: 'XXXX.firebaseapp.com', |
| 75 | + projectId: 'XXXX', |
| 76 | + storageBucket: 'XXXX.appspot.com', |
| 77 | + messagingSenderId: '11111111111', |
| 78 | + appId: '1:11111111111:web:abcdef', |
| 79 | +}; |
| 80 | +``` |
| 81 | + |
| 82 | +You will most likely also have to initialize a Firebase app on your server's end to verify the user's access token later: |
| 83 | + |
| 84 | +```typescript |
| 85 | +import { initializeApp } from 'firebase-admin/app'; |
| 86 | + |
| 87 | +// ... |
| 88 | + |
| 89 | +const firebaseApp = initializeApp(firebaseConfig); |
| 90 | +``` |
| 91 | + |
| 92 | +#### Authenticate method |
| 93 | + |
| 94 | +Lastly, you must define an `authenticate` method which you will use to verify the user's access token and return the user object. |
| 95 | + |
| 96 | +```typescript |
| 97 | +import { getAuth } from 'firebase-admin/auth'; |
| 98 | +import { FirebaseAuthenticatePayload } from '@adminjs/firebase-auth'; |
| 99 | + |
| 100 | +export const authenticate = async ({ |
| 101 | + accessToken, |
| 102 | +}: FirebaseAuthenticatePayload) => { |
| 103 | + const auth = getAuth(firebaseApp); |
| 104 | + |
| 105 | + try { |
| 106 | + const decodedToken = await auth.verifyIdToken(accessToken); |
| 107 | + |
| 108 | + return { |
| 109 | + id: decodedToken.uid, |
| 110 | + email: decodedToken.email ?? '', |
| 111 | + avatarUrl: decodedToken.picture, |
| 112 | + }; |
| 113 | + } catch (error) { |
| 114 | + console.log(error); |
| 115 | + return null; |
| 116 | + } |
| 117 | +}; |
| 118 | +``` |
| 119 | + |
| 120 | +### FirebaseAuthProvider Configuration |
| 121 | + |
| 122 | +After you are done with Firebase-specific configuration, you can instantiate `FirebaseAuthProvider`: |
| 123 | + |
| 124 | +```typescript |
| 125 | +import { FirebaseAuthProvider } from '@adminjs/firebase-auth'; |
| 126 | +import componentLoader from '<path to your component loader file>'; |
| 127 | + |
| 128 | +// ... assume Firebase related configuration is in the same file |
| 129 | + |
| 130 | +const authProvider = new FirebaseAuthProvider({ |
| 131 | + // make sure that the same ComponentLoader instance is configured in AdminJS! |
| 132 | + componentLoader, |
| 133 | + uiConfig, |
| 134 | + firebaseConfig, |
| 135 | + authenticate, |
| 136 | + licenseKey: process.env.LICENSE_KEY, |
| 137 | +}); |
| 138 | + |
| 139 | +// ... |
| 140 | + |
| 141 | +// Add "provider" to authentication options of your framework plugin, Express example: |
| 142 | + |
| 143 | +const router = buildAuthenticatedRouter( |
| 144 | + admin, |
| 145 | + { |
| 146 | + cookiePassword: 'test', |
| 147 | + provider: authProvider, |
| 148 | + }, |
| 149 | + null, |
| 150 | + { |
| 151 | + secret: 'test', |
| 152 | + resave: false, |
| 153 | + saveUninitialized: true, |
| 154 | + } |
| 155 | +); |
| 156 | +``` |
| 157 | + |
| 158 | +With your plugin and auth provider configured you should be able to restart your server and a new login page with embedded Firebase UI should appear. |
| 159 | + |
| 160 | +## Troubleshooting |
| 161 | + |
| 162 | +#### How to prevent @adminjs/firebase-auth from overriding the Login page? |
| 163 | + |
| 164 | +By default, `@adminjs/firebase-auth` will override your Login page with it's own UI. You can disable that by adding `overrideLogin: false` in `FirebaseAuthProvider` configuration: |
| 165 | + |
| 166 | +```typescript |
| 167 | +const authProvider = new FirebaseAuthProvider({ |
| 168 | + componentLoader, |
| 169 | + uiConfig, |
| 170 | + firebaseConfig, |
| 171 | + authenticate, |
| 172 | + licenseKey: process.env.LICENSE_KEY, |
| 173 | + overrideLogin: false, |
| 174 | +}); |
| 175 | +``` |
| 176 | + |
| 177 | +If you do this, though, Firebase won't render it's own UI. You will have to create your own Login page and import `FirebaseAuthForm` component from `@adminjs/firebase-auth` and put it wherever you want in your own Login component. |
| 178 | + |
| 179 | +```typescript |
| 180 | +import { FirebaseAuthForm } from '@adminjs/firebase-auth/components' |
| 181 | + |
| 182 | +const CustomLogin = () => { |
| 183 | + return <FirebaseAuthForm /> |
| 184 | +} |
| 185 | + |
| 186 | +export default CustomLogin |
| 187 | +``` |
| 188 | + |
| 189 | +Remember to override `Login` using your component loader: |
| 190 | + |
| 191 | +```typescript |
| 192 | +componentLoader.override('Login', '<path to custom login>'); |
| 193 | +``` |
| 194 | +
|
| 195 | +#### How to configure functions and callbacks of UI Config? |
| 196 | +
|
| 197 | +Follow the steps above to create your custom Login page. Create UI configuration in your custom component and provide it as props of `FirebaseAuthForm`: |
| 198 | +
|
| 199 | +```typescript |
| 200 | +const uiConfig = {/* custom config */}; |
| 201 | + |
| 202 | +const CustomLogin = () => { |
| 203 | + return <FirebaseAuthForm uiConfig={uiConfig} /> |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +#### How to customize the look of Firebase UI? |
| 208 | + |
| 209 | +If you decide to follow the steps above you will be able to create a styled component out of `FirebaseAuthForm` and customize it fully. |
| 210 | + |
| 211 | +Alternatively, you can create a CSS file and add it to your app's assets. |
| 212 | + |
| 213 | +```css |
| 214 | +.adminjs_firebaseui-container { |
| 215 | + background: red; |
| 216 | +} |
| 217 | +``` |
| 218 | + |
| 219 | +Make sure the CSS file is present in your server's public assets directory. Lastly, configure `assets` of AdminJS: |
| 220 | + |
| 221 | +```typescript |
| 222 | +const admin = new AdminJS({ |
| 223 | + assets: { |
| 224 | + styles: ['/firebase-ui.css'], |
| 225 | + }, |
| 226 | + // other config |
| 227 | +}) |
| 228 | +``` |
0 commit comments