|
1 | 1 | const admin = require('firebase-admin');
|
2 | 2 |
|
| 3 | +function initializeApplicationDefault() { |
| 4 | + // [START initialize_application_default] |
| 5 | + admin.initializeApp({ |
| 6 | + credential: admin.credential.applicationDefault(), |
| 7 | + databaseURL: 'https://<DATABASE_NAME>.firebaseio.com' |
| 8 | + }); |
| 9 | + // [END initialize_application_default] |
| 10 | +} |
| 11 | + |
| 12 | +function initializeRefreshToken() { |
| 13 | + // [START initialize_refresh_token] |
| 14 | + const refreshToken = "..."; // Get refresh token from OAuth2 flow |
| 15 | + |
| 16 | + admin.initializeApp({ |
| 17 | + credential: admin.credential.refreshToken(refreshToken), |
| 18 | + databaseURL: 'https://<DATABASE_NAME>.firebaseio.com' |
| 19 | + }); |
| 20 | + // [END initialize_refresh_token] |
| 21 | +} |
| 22 | + |
| 23 | +function initializeEmpty() { |
| 24 | + // [START initialize_empty] |
| 25 | + const app = admin.initializeApp(); |
| 26 | + // [END initialize_empty] |
| 27 | +} |
| 28 | + |
| 29 | +function initializeDefaultApp() { |
| 30 | + const defaultAppConfig = {}; |
| 31 | + |
| 32 | + // [START initialize_default_app] |
| 33 | + // Initialize the default app |
| 34 | + const defaultApp = admin.initializeApp(defaultAppConfig); |
| 35 | + |
| 36 | + console.log(defaultApp.name); // '[DEFAULT]' |
| 37 | + |
| 38 | + // Retrieve services via the defaultApp variable... |
| 39 | + let defaultAuth = defaultApp.auth(); |
| 40 | + let defaultDatabase = defaultApp.database(); |
| 41 | + |
| 42 | + // ... or use the equivalent shorthand notation |
| 43 | + defaultAuth = admin.auth(); |
| 44 | + defaultDatabase = admin.database(); |
| 45 | + // [END initialize_default_app] |
| 46 | +} |
| 47 | + |
| 48 | +function initializeMultipleApps() { |
| 49 | + const defaultAppConfig = {}; |
| 50 | + const otherAppConfig = {}; |
| 51 | + |
| 52 | + // [START initialize_multiple_apps] |
| 53 | + // Initialize the default app |
| 54 | + admin.initializeApp(defaultAppConfig); |
| 55 | + |
| 56 | + // Initialize another app with a different config |
| 57 | + var otherApp = admin.initializeApp(otherAppConfig, 'other'); |
| 58 | + |
| 59 | + console.log(admin.app().name); // '[DEFAULT]' |
| 60 | + console.log(otherApp.name); // 'other' |
| 61 | + |
| 62 | + // Use the shorthand notation to retrieve the default app's services |
| 63 | + const defaultAuth = admin.auth(); |
| 64 | + const defaultDatabase = admin.database(); |
| 65 | + |
| 66 | + // Use the otherApp variable to retrieve the other app's services |
| 67 | + const otherAuth = otherApp.auth(); |
| 68 | + const otherDatabase = otherApp.database(); |
| 69 | + // [END initialize_multiple_apps] |
| 70 | +} |
| 71 | + |
3 | 72 | function multipleFirebaseApps() {
|
4 | 73 | // [START firebase_options]
|
5 | 74 | const secondaryServiceAccount = require('./path/to/serviceAccountKey.json');
|
|
0 commit comments