Skip to content

Commit 1bbecac

Browse files
authored
Merge pull request #12 from lupas/dev
Dev
2 parents ced2141 + 099e38b commit 1bbecac

File tree

4 files changed

+103
-52
lines changed

4 files changed

+103
-52
lines changed

README.md

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,44 @@ modules: [
4242
'nuxt-fire',
4343
{
4444
config: {
45-
apiKey: '<apiKey>',
46-
authDomain: '<authDomain>',
47-
databaseURL: '<databaseURL>',
48-
projectId: '<projectId>',
49-
storageBucket: '<storageBucket>',
50-
messagingSenderId: '<messagingSenderId>'
45+
development: {
46+
apiKey: '<apiKey>',
47+
authDomain: '<authDomain>',
48+
databaseURL: '<databaseURL>',
49+
projectId: '<projectId>',
50+
storageBucket: '<storageBucket>',
51+
messagingSenderId: '<messagingSenderId>'
52+
},
53+
production: {
54+
apiKey: '<apiKey>',
55+
authDomain: '<authDomain>',
56+
databaseURL: '<databaseURL>',
57+
projectId: '<projectId>',
58+
storageBucket: '<storageBucket>',
59+
messagingSenderId: '<messagingSenderId>'
60+
}
5161
}
5262
}
5363
]
5464
],
5565
```
5666

67+
## Usage
68+
69+
You can access the various Firebase products with **\$foo** in almost any context using `app.$foo` or `this.$foo`, including store actions. Make sure to replace the _foo_ with a shortcut from the table below.
70+
71+
Firebase products supported by nuxt-fire so far:
72+
73+
| Firebase Product | Shortcut |
74+
| ----------------- | ------------- |
75+
| Authentication | \$fireAuth |
76+
| Realtime Database | \$fireDb |
77+
| Firestore | \$fireStore |
78+
| Storage | \$fireStorage |
79+
| Functions | \$fireFunc |
80+
81+
See [Firebase's official docs](https://firebase.google.com/docs/) for more usage information.
82+
5783
## Options
5884

5985
#### useOnly
@@ -64,7 +90,7 @@ By default, all supported Firebase products are loaded. If you only wish to load
6490
- default: `['auth','firestore','functions','storage','realtimeDb']`
6591
- required: `false`
6692

67-
#### config
93+
#### config[environment]
6894

6995
Your firebase config snippet. You can retrieve this information from your Firebase project's overview page:
7096

@@ -81,28 +107,55 @@ Your firebase config snippet. You can retrieve this information from your Fireba
81107
}
82108
```
83109

84-
Only applies when `NODE_ENV === 'production'`. In that case it is required.
110+
`config.production` gets loaded when `NODE_ENV === 'production', same applies to 'development' and any other values that you set in NODE_ENV.
85111

86-
#### devConfig
112+
#### customEnv
87113

88-
Same es `config`, but applies when `NODE_ENV === 'development'`. In that case it is required
114+
By default, the Firebase config will be chosen based on the NODE_ENV environment variable.
89115

90-
## Usage
116+
If customEnv is set to true, however, nuxt-fire will determine the environment based on the environment variable called FIRE_ENV, which you can define yourself. This gives you the flexibility to define as many different Firebase configs as you like, independent of your NODE_ENV.
91117

92-
You can access the various Firebase products with **\$foo** in almost any context using `app.$foo` or `this.$foo`, including store actions. Make sure to replace the _foo_ with a shortcut from the table below.
118+
- type: `Boolean`
119+
- default: `false`
120+
- required: `false`
93121

94-
Firebase products supported by nuxt-fire so far:
122+
_⚠️ Important:_
95123

96-
| Firebase Product | Shortcut |
97-
| ----------------- | ------------- |
98-
| Authentication | \$fireAuth |
99-
| Realtime Database | \$fireDb |
100-
| Firestore | \$fireStore |
101-
| Storage | \$fireStorage |
102-
| Functions | \$fireFunc |
124+
If you decide to turn on this option, you need to add the following code to your `nuxt.config.js` to make sure that the environment variable gets passed from server to client.
103125

104-
See [Firebase's official docs](https://firebase.google.com/docs/) for more usage information.
126+
```js
127+
env: {
128+
FIRE_ENV: process.env.FIRE_ENV
129+
}
130+
```
131+
132+
After that, you can set FIRE_ENV to anything you like...
133+
134+
```js
135+
"scripts": {
136+
"serveFoo": "FIRE_ENV=foofoofoo nuxt",
137+
"serveFaa": "FIRE_ENV=faafaafaa nuxt",
138+
}
139+
```
140+
141+
And then add your config to the nuxt-fire options in your `nuxt.config.js`:
142+
143+
```js
144+
config: {
145+
foofoofoo: {
146+
apiKey: '<apiKey>',
147+
authDomain: '<authDomain>',
148+
databaseURL: '<databaseURL>',
149+
projectId: '<projectId>',
150+
storageBucket: '<storageBucket>',
151+
messagingSenderId: '<messagingSenderId>'
152+
},
153+
faafaafaa: {
154+
//
155+
}
156+
}
157+
```
105158

106-
### Examples
159+
## Examples
107160

108-
Check out the [GitHub Repo](https://github.com/lupas/nuxt-fire-demo) of our [Demo](https://nuxt-fire-demo.firebaseapp.com/) for example code.
161+
Check out our [Demo](https://nuxt-fire-demo.firebaseapp.com/) or its [GitHub Repo](https://github.com/lupas/nuxt-fire-demo) for example code.

index.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,37 @@ import path from 'path'
33
export default function nuxtFire(moduleOptions) {
44
const options = Object.assign({}, this.options.fire, moduleOptions)
55

6-
// Don't include when config is missing
7-
if (
8-
process.env.NODE_ENV === 'production' &&
9-
(!options.config ||
10-
!options.config.apiKey ||
11-
!options.config.authDomain ||
12-
!options.config.databaseURL ||
13-
!options.config.projectId ||
14-
!options.config.storageBucket ||
15-
!options.config.messagingSenderId)
16-
) {
6+
// Set environment
7+
let currentEnv = process.env.FIRE_ENV
8+
if (!options.customEnv || !currentEnv) {
9+
currentEnv = process.env.NODE_ENV
10+
}
11+
options.currentEnv = currentEnv
12+
13+
// If in CustomEnv Mode: Check if FIRE_ENV is set.
14+
if (options.customEnv && !process.env.FIRE_ENV) {
1715
//TODO: Replace with @nuxtjs/plugin-utils error
18-
console.error('nuxtFire Error: Missing or incomplete Firebase config.')
19-
return
16+
return console.error(
17+
'\x1b[31m',
18+
`Nuxt-Fire Error: CustomEnv mode requires FIRE_ENV to be set.`
19+
)
2020
}
2121

22-
// Don't include when devConfig is missing
22+
// Check if needed config is correctly set
2323
if (
24-
process.env.NODE_ENV === 'development' &&
25-
(!options.devConfig ||
26-
!options.devConfig.apiKey ||
27-
!options.devConfig.authDomain ||
28-
!options.devConfig.databaseURL ||
29-
!options.devConfig.projectId ||
30-
!options.devConfig.storageBucket ||
31-
!options.devConfig.messagingSenderId)
24+
!options.config[currentEnv] ||
25+
!options.config[currentEnv].apiKey ||
26+
!options.config[currentEnv].authDomain ||
27+
!options.config[currentEnv].databaseURL ||
28+
!options.config[currentEnv].projectId ||
29+
!options.config[currentEnv].storageBucket ||
30+
!options.config[currentEnv].messagingSenderId
3231
) {
3332
//TODO: Replace with @nuxtjs/plugin-utils error
34-
console.error('nuxtFire Error: Missing or incomplete Firebase devConfig.')
33+
console.error(
34+
'\x1b[31m',
35+
`Nuxt-Fire Error: Missing or incomplete config for current environment '${currentEnv}'!`
36+
)
3537
return
3638
}
3739

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nuxt-fire",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"license": "MIT",
55
"description": "Intergrate Firebase into your Nuxt project.",
66
"main": "index.js",

plugin.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ export default (ctx, inject) => {
1111

1212
// Don't include when Firebase is already initialized
1313
if (!firebase.apps.length) {
14-
if (process.env.NODE_ENV === 'production') {
15-
firebase.initializeApp(options.config)
16-
} else {
17-
firebase.initializeApp(options.devConfig)
18-
}
14+
firebase.initializeApp(options.config[options.currentEnv])
1915
}
2016

2117
if (options.useOnly.includes('firestore')) {

0 commit comments

Comments
 (0)