Skip to content

Commit

Permalink
Drop support for passing a Firebase instance.
Browse files Browse the repository at this point in the history
* Firebase is no longer a dependency (build size, native compatibility,
bundling for boilerplates, etc.) - #173, #131, #107
* Migration guide updated
* Roadmap updated with v2.0.0 and v1.5.0 info
  • Loading branch information
Scott Prue committed Jun 27, 2017
1 parent 2103c8d commit 675c256
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 164 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -41,7 +41,7 @@ npm install --save react-redux-firebase

## Use

Include reduxFirebase in your store compose function:
Include reactReduxFirebase in your store compose function:


```javascript
Expand Down
10 changes: 7 additions & 3 deletions docs/roadmap.md
Expand Up @@ -41,6 +41,7 @@
* Use `storeAs` with populates - [#130](https://github.com/prescottprue/react-redux-firebase/issues/130)
* `updateUser` method for updating currently authenticated user's user object (`/users/${uid}`)
* `updateAuth` method for updating currently authenticated user's auth object [as seen in the Firebase docs](https://firebase.google.com/docs/auth/web/manage-users#get_a_users_provider-specific_profile_information) - [#129](https://github.com/prescottprue/react-redux-firebase/issues/129)
* Ability to use different stores - [#148](https://github.com/prescottprue/react-redux-firebase/pull/148)
* Expose Firebase messaging (`firebase.messaging()`)
* Typescript typings - [#142](https://github.com/prescottprue/react-redux-firebase/issues/142)
* `enableEmptyAuthChanges` config option added - [#137](https://github.com/prescottprue/react-redux-firebase/issues/137)
Expand Down Expand Up @@ -87,21 +88,24 @@

#### Breaking Changes
* Remove usage of `Immutable.js` and Immutable Maps (no more need for `pathToJS()` & `dataToJS()` to load data from redux)
* Firebase is now initialized outside of `react-redux-firebase` - [#173](https://github.com/prescottprue/react-redux-firebase/issues), [#131](https://github.com/prescottprue/react-redux-firebase/issues), [#107](https://github.com/prescottprue/react-redux-firebase/issues)
* reducer split into multiple nested reducers for a few reasons:
* follows [standard for nesting of reducers using combine reducers](http://redux.js.org/docs/recipes/reducers/UpdatingNormalizedData.html)).
* allows for separately importable reducers (for placing in other parts of redux other than `state.firebase`)
* Improved rendering/update performance for `react` - [#84](https://github.com/prescottprue/react-redux-firebase/issues/84)

#### Features
* Support for keeping data on logout - [#125](https://github.com/prescottprue/react-redux-firebase/issues/125)
* `react-native` index file referenced in `package.json` that makes it no longer necessary to pass `ReactNative` in config
* `AuthRequired` decorator (or decorator factory) that forces auth to exist before rendering component
* Possibility of delayed initialization - [#70](https://github.com/prescottprue/react-redux-firebase/issues/70) (more research needed)
* Support [`react-native-firebase`](https://github.com/invertase/react-native-firebase) [#131](https://github.com/prescottprue/react-redux-firebase/issues/131)

#### Enhancements/Fixes
* Implement [`firebase-server`](https://github.com/urish/firebase-server) for tests instead of using demo firebase instance

#### Enhancements
* Implement [`firebase-server`](https://github.com/urish/firebase-server) for tests instead of using demo firebase instance
#### Under Consideration
* Possibility of delayed initialization - [#70](https://github.com/prescottprue/react-redux-firebase/issues/70) (more research needed)


### Long Term Goals
* Optional Built in Role Management
Expand Down
88 changes: 56 additions & 32 deletions docs/v2-migration-guide.md
Expand Up @@ -6,10 +6,52 @@
* simplified population (can easily be done manually following common redux patterns)
* [`redux-persist`](https://github.com/rt2zz/redux-persist) is supported out of the box
* Simplified population through `populate` (instead of `populatedDataToJS`)
* Firebase instance can be passed as first argument instead of config vars:
* Firebase instance must be passed as first argument instead of config vars:
* removes platform specific code while improving platform support
* allows any version of Firebase to be used
* allows [`react-native-firebase`](https://github.com/invertase/react-native-firebase) to be passed (for using native modules instead of JS within `react-native`)
* firebase is no longer a dependency (shrinks umd bundle size)

### Pass In Firebase instance

If you would like to instantiate a Firebase instance outside of `react-redux-firebase`, you can pass it in as the first argument like so:

#### `v1.*.*`

```js
import { reactReduxFirebase } from 'react-redux-firebase'
const fbConfig = {} // object containing Firebase config
const rrfConfig = { userProfile: 'users' } // react-redux-firebase config

const store = createStore(
reducer,
initialState,
compose(
reactReduxFirebase(fbConfig, rrfConfig), // pass in firebase instance instead of config
applyMiddleware(...middleware)
)
)
```

#### `v2.*.*`

```js
import { reactReduxFirebase } from 'react-redux-firebase'
import * as firebase from 'firebase'

const fbConfig = {} // object containing Firebase config
firebase.initializeApp(fbConfig) // initialize firebase instance
const rrfConfig = { userProfile: 'users' } // react-redux-firebase config

const store = createStore(
reducer,
initialState,
compose(
reactReduxFirebase(firebase, rrfConfig), // pass in firebase instance instead of config
applyMiddleware(...middleware)
)
)
```

### Loading Data and Paths

Expand All @@ -29,6 +71,7 @@ import { firebaseConnect, dataToJS, pathToJS } from 'react-redux-firebase';
```

#### `v2.*.*`

```js
import { connect } from 'react-redux'
import { firebaseConnect } from 'react-redux-firebase';
Expand Down Expand Up @@ -85,28 +128,6 @@ const populates = [{ child: 'owner', root: 'users' }]
)
```

### Pass In Firebase instance

If you would like to instantiate a Firebase instance outside of `react-redux-firebase`, you can pass it in as the first argument like so:

```js
import * as firebase from 'firebase/app'
import 'firebase/auth'
import 'firebase/database'
import 'firebase/storage'

const fbConfig = {} // object containing Firebase config
firebase.initializeApp(fbConfig) // initialize firebase instance

const store = createStore(
reducer,
initialState,
compose(
reactReduxFirebase(firebase, reduxConfig), // pass in firebase instance instead of config
applyMiddleware(...middleware)
)
)
```

#### [react-native-firebase](https://github.com/invertase/react-native-firebase)

Expand All @@ -115,17 +136,15 @@ Passing in an instance also allows for libraries with similar APIs (such as [`re
```js
import RNFirebase from 'react-native-firebase';

const configurationOptions = {
debug: true
};

const firebase = RNFirebase.initializeApp(configurationOptions);
const rnfConfig = { debug: true } // react-native-firebase config
const firebase = RNFirebase.initializeApp(rnfConfig);

const store = createStore(
reducer,
undefined,
initialState,
compose(
reactReduxFirebase(RNFirebase, reduxConfig), // pass in react-native-firebase instance instead of config
// pass in react-native-firebase instance instead of firebase instance
reactReduxFirebase(RNFirebase, reduxConfig),
applyMiddleware(...middleware)
)
)
Expand All @@ -139,13 +158,18 @@ View the [redux-persist](/docs/recipes/redux-persist) section for the full examp
import { compose, createStore } from 'redux'
import { reactReduxFirebase } from 'react-redux-firebase'
import { persistStore, autoRehydrate } from 'redux-persist'
import { firebase as fbConfig, reduxFirebase as reduxConfig } from '../config'
import * as firebase from 'firebase'

const fbConfig = {} // firebase config object
firebase.initializeApp(fbConfig)

const rrfConfig = {}

const store = createStore(
reducer,
initialState,
compose(
reactReduxFirebase(fbConfig, reduxConfig),
reactReduxFirebase(fbConfig, rrfConfig),
autoRehydrate()
)
)
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "react-redux-firebase",
"version": "2.0.0-alpha.2",
"version": "2.0.0-beta",
"description": "Redux integration for Firebase. Comes with a Higher Order Component for use with React.",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down Expand Up @@ -56,7 +56,6 @@
],
"dependencies": {
"es6-promise": "^4.1.0",
"firebase": "^4.1.3",
"hoist-non-react-statics": "^1.2.0",
"jwt-decode": "^2.2.0",
"lodash": "^4.17.4",
Expand Down Expand Up @@ -98,6 +97,7 @@
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-react": "^6.10.3",
"eslint-plugin-standard": "^2.2.0",
"firebase": "^4.1.3",
"firebase-server": "^0.10.1",
"gitbook-cli": "^2.3.0",
"gitbook-plugin-anchorjs": "^1.1.1",
Expand Down
42 changes: 17 additions & 25 deletions src/actions/auth.js
@@ -1,3 +1,4 @@
import jwtDecode from 'jwt-decode'
import {
omit,
isArray,
Expand All @@ -9,7 +10,6 @@ import {
map,
mapValues
} from 'lodash'
import jwtDecode from 'jwt-decode'
import { actionTypes, defaultJWTProps } from '../constants'
import { getLoginMethodAndParams } from '../utils/auth'
import {
Expand All @@ -18,17 +18,6 @@ import {
getChildType
} from '../utils/populate'

const {
SET,
SET_PROFILE,
LOGIN,
LOGOUT,
LOGIN_ERROR,
UNAUTHORIZED_ERROR,
AUTHENTICATION_INIT_STARTED,
AUTHENTICATION_INIT_FINISHED
} = actionTypes

/**
* @description Dispatch login error action
* @param {Function} dispatch - Action dispatch function
Expand All @@ -37,7 +26,7 @@ const {
*/
export const dispatchLoginError = (dispatch, authError) =>
dispatch({
type: LOGIN_ERROR,
type: actionTypes.LOGIN_ERROR,
authError
})

Expand All @@ -49,7 +38,7 @@ export const dispatchLoginError = (dispatch, authError) =>
*/
export const dispatchUnauthorizedError = (dispatch, authError) =>
dispatch({
type: UNAUTHORIZED_ERROR,
type: actionTypes.UNAUTHORIZED_ERROR,
authError
})

Expand All @@ -61,9 +50,8 @@ export const dispatchUnauthorizedError = (dispatch, authError) =>
*/
export const dispatchLogin = (dispatch, auth) =>
dispatch({
type: LOGIN,
auth,
authError: null
type: actionTypes.LOGIN,
auth
})

/**
Expand Down Expand Up @@ -106,10 +94,11 @@ export const watchUserProfile = (dispatch, firebase) => {
} = firebase._.config
if (!profileParamsToPopulate || (!isArray(profileParamsToPopulate) && !isString(profileParamsToPopulate))) {
dispatch({
type: SET_PROFILE,
type: actionTypes.SET_PROFILE,
profile: snap.val()
})
} else {
// TODO: Share population logic with query action
// Convert each populate string in array into an array of once query promises
promisesForPopulate(firebase, snap.val(), profileParamsToPopulate)
.then(data => {
Expand Down Expand Up @@ -153,13 +142,13 @@ export const watchUserProfile = (dispatch, firebase) => {
set(profile, p.child, populatedChild)
})
dispatch({
type: SET_PROFILE,
type: actionTypes.SET_PROFILE,
profile
})
} else {
// dispatch with unpopulated profile data
dispatch({
type: SET_PROFILE,
type: actionTypes.SET_PROFILE,
profile: snap.val()
})
}
Expand All @@ -168,7 +157,7 @@ export const watchUserProfile = (dispatch, firebase) => {
if (setProfilePopulateResults) {
forEach(data, (result, path) => {
dispatch({
type: SET,
type: actionTypes.SET,
path,
data: result,
timestamp: Date.now(),
Expand Down Expand Up @@ -238,15 +227,15 @@ export const createUserProfile = (dispatch, firebase, userData, profile) => {
* @private
*/
export const init = (dispatch, firebase) => {
dispatch({ type: AUTHENTICATION_INIT_STARTED })
dispatch({ type: actionTypes.AUTHENTICATION_INIT_STARTED })

firebase.auth().onAuthStateChanged(authData => {
if (!authData) {
// Run onAuthStateChanged if it exists in config and enableEmptyAuthChanges is set to true
if (isFunction(firebase._.config.onAuthStateChanged) && firebase._.config.enableEmptyAuthChanges) {
firebase._.config.onAuthStateChanged(authData, firebase, dispatch)
}
return dispatch({ type: LOGOUT })
return dispatch({ type: actionTypes.LOGOUT })
}

firebase._.authUid = authData.uid
Expand Down Expand Up @@ -296,7 +285,7 @@ export const init = (dispatch, firebase) => {

firebase.auth().currentUser // eslint-disable-line no-unused-expressions

dispatch({ type: AUTHENTICATION_INIT_FINISHED })
dispatch({ type: actionTypes.AUTHENTICATION_INIT_FINISHED })
}

/**
Expand Down Expand Up @@ -370,7 +359,10 @@ export const logout = (dispatch, firebase) =>
firebase.auth()
.signOut()
.then(() => {
dispatch({ type: LOGOUT })
dispatch({
type: actionTypes.LOGOUT,
preserve: firebase._.config.preserveOnLogout
})
firebase._.authUid = null
unWatchUserProfile(firebase)
return firebase
Expand Down

0 comments on commit 675c256

Please sign in to comment.