Skip to content

Commit

Permalink
Merge pull request #12 from lupas/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
lupas authored Nov 20, 2018
2 parents ced2141 + 099e38b commit 1bbecac
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 52 deletions.
99 changes: 76 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,44 @@ modules: [
'nuxt-fire',
{
config: {
apiKey: '<apiKey>',
authDomain: '<authDomain>',
databaseURL: '<databaseURL>',
projectId: '<projectId>',
storageBucket: '<storageBucket>',
messagingSenderId: '<messagingSenderId>'
development: {
apiKey: '<apiKey>',
authDomain: '<authDomain>',
databaseURL: '<databaseURL>',
projectId: '<projectId>',
storageBucket: '<storageBucket>',
messagingSenderId: '<messagingSenderId>'
},
production: {
apiKey: '<apiKey>',
authDomain: '<authDomain>',
databaseURL: '<databaseURL>',
projectId: '<projectId>',
storageBucket: '<storageBucket>',
messagingSenderId: '<messagingSenderId>'
}
}
}
]
],
```

## Usage

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.

Firebase products supported by nuxt-fire so far:

| Firebase Product | Shortcut |
| ----------------- | ------------- |
| Authentication | \$fireAuth |
| Realtime Database | \$fireDb |
| Firestore | \$fireStore |
| Storage | \$fireStorage |
| Functions | \$fireFunc |

See [Firebase's official docs](https://firebase.google.com/docs/) for more usage information.

## Options

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

#### config
#### config[environment]

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

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

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

#### devConfig
#### customEnv

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

## Usage
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.

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.
- type: `Boolean`
- default: `false`
- required: `false`

Firebase products supported by nuxt-fire so far:
_⚠️ Important:_

| Firebase Product | Shortcut |
| ----------------- | ------------- |
| Authentication | \$fireAuth |
| Realtime Database | \$fireDb |
| Firestore | \$fireStore |
| Storage | \$fireStorage |
| Functions | \$fireFunc |
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.

See [Firebase's official docs](https://firebase.google.com/docs/) for more usage information.
```js
env: {
FIRE_ENV: process.env.FIRE_ENV
}
```

After that, you can set FIRE_ENV to anything you like...

```js
"scripts": {
"serveFoo": "FIRE_ENV=foofoofoo nuxt",
"serveFaa": "FIRE_ENV=faafaafaa nuxt",
}
```

And then add your config to the nuxt-fire options in your `nuxt.config.js`:

```js
config: {
foofoofoo: {
apiKey: '<apiKey>',
authDomain: '<authDomain>',
databaseURL: '<databaseURL>',
projectId: '<projectId>',
storageBucket: '<storageBucket>',
messagingSenderId: '<messagingSenderId>'
},
faafaafaa: {
//
}
}
```

### Examples
## Examples

Check out the [GitHub Repo](https://github.com/lupas/nuxt-fire-demo) of our [Demo](https://nuxt-fire-demo.firebaseapp.com/) for example code.
Check out our [Demo](https://nuxt-fire-demo.firebaseapp.com/) or its [GitHub Repo](https://github.com/lupas/nuxt-fire-demo) for example code.
48 changes: 25 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,37 @@ import path from 'path'
export default function nuxtFire(moduleOptions) {
const options = Object.assign({}, this.options.fire, moduleOptions)

// Don't include when config is missing
if (
process.env.NODE_ENV === 'production' &&
(!options.config ||
!options.config.apiKey ||
!options.config.authDomain ||
!options.config.databaseURL ||
!options.config.projectId ||
!options.config.storageBucket ||
!options.config.messagingSenderId)
) {
// Set environment
let currentEnv = process.env.FIRE_ENV
if (!options.customEnv || !currentEnv) {
currentEnv = process.env.NODE_ENV
}
options.currentEnv = currentEnv

// If in CustomEnv Mode: Check if FIRE_ENV is set.
if (options.customEnv && !process.env.FIRE_ENV) {
//TODO: Replace with @nuxtjs/plugin-utils error
console.error('nuxtFire Error: Missing or incomplete Firebase config.')
return
return console.error(
'\x1b[31m',
`Nuxt-Fire Error: CustomEnv mode requires FIRE_ENV to be set.`
)
}

// Don't include when devConfig is missing
// Check if needed config is correctly set
if (
process.env.NODE_ENV === 'development' &&
(!options.devConfig ||
!options.devConfig.apiKey ||
!options.devConfig.authDomain ||
!options.devConfig.databaseURL ||
!options.devConfig.projectId ||
!options.devConfig.storageBucket ||
!options.devConfig.messagingSenderId)
!options.config[currentEnv] ||
!options.config[currentEnv].apiKey ||
!options.config[currentEnv].authDomain ||
!options.config[currentEnv].databaseURL ||
!options.config[currentEnv].projectId ||
!options.config[currentEnv].storageBucket ||
!options.config[currentEnv].messagingSenderId
) {
//TODO: Replace with @nuxtjs/plugin-utils error
console.error('nuxtFire Error: Missing or incomplete Firebase devConfig.')
console.error(
'\x1b[31m',
`Nuxt-Fire Error: Missing or incomplete config for current environment '${currentEnv}'!`
)
return
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuxt-fire",
"version": "1.1.0",
"version": "1.2.0",
"license": "MIT",
"description": "Intergrate Firebase into your Nuxt project.",
"main": "index.js",
Expand Down
6 changes: 1 addition & 5 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ export default (ctx, inject) => {

// Don't include when Firebase is already initialized
if (!firebase.apps.length) {
if (process.env.NODE_ENV === 'production') {
firebase.initializeApp(options.config)
} else {
firebase.initializeApp(options.devConfig)
}
firebase.initializeApp(options.config[options.currentEnv])
}

if (options.useOnly.includes('firestore')) {
Expand Down

0 comments on commit 1bbecac

Please sign in to comment.