Skip to content

Commit cc0ad58

Browse files
committed
Switched to ReactNative instead of rn
* Switched to using ReactNative instead of rn * Roadmap updated with `react-native` stuff * react-native complete example README added
1 parent 2b2f429 commit cc0ad58

File tree

7 files changed

+123
-73
lines changed

7 files changed

+123
-73
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [Epics](/docs/recipes/epics.md)
1515
* [Routing](/docs/recipes/routing.md)
1616
* [Redux Form](/docs/recipes/redux-form.md)
17+
* [React Native](/docs/recipes/react-native.md)
1718
* [Populate](/docs/recipes/populate.md)
1819
* [API Reference](/docs/api/README.md)
1920
* [constants](/docs/api/constants.md)

docs/recipes/react-native.md

Lines changed: 77 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,145 @@
11
# React Native
22

3-
**NOTE**: Only works for versions `v1.4.0-alpha` and higher. It is still in the early stages of support.
3+
[react-native complete example app](/examples/complete/react-native)
4+
5+
**NOTE**: Only works for versions `v1.4.0-beta` and higher. It is still in the early stages of support.
46

57
## Setup
68

79
1. Click "Add Firebase To iOS"
810
<!-- TODO: Confirm this and get a picture -->
911
1. Download `GoogleService-info.plist`
1012
1. Place `GoogleService-info.plist` in the folder of whichever platform you are using (i.e. `/ios`)
11-
1. Make sure you correctly setup [`react-native-google-signin`](https://github.com/devfd/react-native-google-signin) (means you should end up with a `GoogleSDK` folder in the folder matching your platform)
13+
1. Copy your client id out of the `GoogleService-info.plist` file (should end in `.apps.googleusercontent.com`)
14+
1. Place the client id into `iosClientId` variable within the example
1215

1316

1417
## Example App Snippet
1518

1619
This snippet is a condensed version of [react-native complete example](/examples/complete/react-native).
1720

1821
```js
19-
import React, { Component } from 'react';
22+
import React, { Component } from 'react'
23+
import { GoogleSignin, GoogleSigninButton } from 'react-native-google-signin'
24+
import { firebaseConnect, pathToJS, isLoaded } from 'react-redux-firebase'
25+
import { connect } from 'react-redux'
2026
import {
2127
AppRegistry,
2228
StyleSheet,
2329
Text,
2430
View,
2531
TouchableOpacity,
26-
} from 'react-native';
32+
} from 'react-native'
33+
import configureStore from './store'
34+
const initialState = { firebase: { authError: null, auth: undefined }}
35+
const store = configureStore(initialState)
2736

28-
import { GoogleSignin, GoogleSigninButton } from 'react-native-google-signin';
2937
const iosClientId = '499842460400-teaflfd8695oilltk5qkvl5688ebgq6b.apps.googleusercontent.com' // get this from plist file
3038

31-
class SigninSampleApp extends Component {
32-
state = {
33-
user: null
34-
}
39+
const styles = StyleSheet.create({
40+
container: {
41+
flex: 1,
42+
justifyContent: 'center',
43+
alignItems: 'center',
44+
backgroundColor: '#F5FCFF',
45+
},
46+
welcome: {
47+
fontSize: 20,
48+
textAlign: 'center',
49+
margin: 10,
50+
},
51+
instructions: {
52+
textAlign: 'center',
53+
color: '#333333',
54+
marginBottom: 5,
55+
},
56+
})
3557

58+
@firebaseConnect()
59+
@connect(({ firebase }) => ({
60+
auth: pathToJS(firebase, 'auth', undefined)
61+
}))
62+
export default class GoogleSigninSampleApp extends Component {
3663
componentDidMount() {
37-
this._setupGoogleSignin();
64+
this._setupGoogleSignin()
3865
}
3966

4067
render() {
41-
if (!this.state.user) {
68+
const { auth } = this.props
69+
if (!isLoaded(auth)) {
70+
return (
71+
<View style={styles.container}>
72+
<Text>Loading...</Text>
73+
</View>
74+
)
75+
}
76+
if (!this.props.auth) {
4277
return (
4378
<View style={styles.container}>
4479
<GoogleSigninButton
4580
style={{width: 212, height: 48}}
4681
size={GoogleSigninButton.Size.Standard}
4782
color={GoogleSigninButton.Color.Auto}
48-
onPress={this._signIn.bind(this)}
83+
onPress={() => this._signIn()}
4984
/>
5085
</View>
51-
);
52-
}
53-
54-
if (this.state.user) {
55-
return (
56-
<View style={styles.container}>
57-
<Text style={{fontSize: 18, fontWeight: 'bold', marginBottom: 20}}>
58-
Welcome {this.state.user.name}
59-
</Text>
60-
<Text>Your email is: {this.state.user.email}</Text>
61-
62-
<TouchableOpacity onPress={() => {this._signOut(); }}>
63-
<View style={{marginTop: 50}}>
64-
<Text>Log out</Text>
65-
</View>
66-
</TouchableOpacity>
67-
</View>
68-
);
86+
)
6987
}
88+
return (
89+
<View style={styles.container}>
90+
<Text style={{fontSize: 18, fontWeight: 'bold', marginBottom: 20}}>
91+
Welcome {this.props.auth.displayName}
92+
</Text>
93+
<Text>
94+
Your email is: {this.props.auth.email}</Text>
95+
96+
<TouchableOpacity onPress={() => {this._signOut() }}>
97+
<View style={{marginTop: 50}}>
98+
<Text>Log out</Text>
99+
</View>
100+
</TouchableOpacity>
101+
</View>
102+
)
70103
}
71104

105+
// based on react-native-google-signin example
72106
async _setupGoogleSignin() {
73107
try {
74-
await GoogleSignin.hasPlayServices({ autoResolve: true });
108+
await GoogleSignin.hasPlayServices({ autoResolve: true })
75109
await GoogleSignin.configure({
76110
iosClientId,
77-
// webClientId: '603421766430-60og8n04mebic8hi49u1mrcmcdmugnd5.apps.googleusercontent.com',
78111
offlineAccess: false
79-
});
112+
})
80113

81-
const user = await GoogleSignin.currentUserAsync();
82-
console.log(user);
83-
this.setState({user});
114+
const user = await GoogleSignin.currentUserAsync()
115+
const creds = this.props.firebase.auth.GoogleAuthProvider.credential(null, user.accessToken)
116+
await this.props.firebase.auth().signInWithCredential(creds)
84117
}
85118
catch(err) {
86-
console.log("Google signin error", err.code, err.message);
119+
console.log("Google signin error", err.code, err.message)
87120
}
88121
}
89122

90123
_signIn() {
124+
const { auth } = this.props.firebase
91125
return GoogleSignin.signIn()
92126
.then((user) => {
93-
console.log(user);
94-
this.setState({user: user});
127+
const creds = auth.GoogleAuthProvider.credential(null, user.accessToken)
128+
return auth().signInWithCredential(creds)
95129
})
96130
.catch((err) => {
97-
console.log('WRONG SIGNIN', err);
131+
console.error('error authing with firebase:', err)
132+
return Promise.reject(err)
98133
})
99134
}
100135

101136
_signOut() {
102137
return GoogleSignin.revokeAccess()
103138
.then(() => GoogleSignin.signOut())
104-
.then(() => {
105-
this.setState({user: null});
106-
})
139+
.then(() => this.props.firebase.logout())
107140
}
108141
}
109142

110-
const styles = StyleSheet.create({
111-
container: {
112-
flex: 1,
113-
justifyContent: 'center',
114-
alignItems: 'center',
115-
backgroundColor: '#F5FCFF',
116-
},
117-
welcome: {
118-
fontSize: 20,
119-
textAlign: 'center',
120-
margin: 10,
121-
},
122-
instructions: {
123-
textAlign: 'center',
124-
color: '#333333',
125-
marginBottom: 5,
126-
},
127-
});
128-
129-
AppRegistry.registerComponent('GoogleSigninSampleApp', () => GoogleSigninSampleApp);
143+
AppRegistry.registerComponent('GoogleSigninSampleApp', () => GoogleSigninSampleApp)
130144

131145
```

docs/roadmap.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import { pathToJS, dataToJS, populatedDataToJS } from 'react-redux-firebase'
5151
*None Yet Planned*
5252

5353
#### Features
54+
* Integration for [`react-native-google-signin`](https://github.com/devfd/react-native-google-signin) to simplify react-native authentication implementation
5455
* Nested populates [#85](https://github.com/prescottprue/react-redux-firebase/issues/85)
5556

5657
## Upcoming Major Version (`v2.0.0`)
@@ -68,6 +69,7 @@ import { pathToJS, dataToJS, populatedDataToJS } from 'react-redux-firebase'
6869
* Improved rendering/update performance for `react` as described in [#84](https://github.com/prescottprue/react-redux-firebase/issues/84)
6970

7071
#### Features
72+
* `react-native` index file referenced in `package.json` that makes it no longer necessary to pass `ReactNative` in config
7173
* `AuthRequired` decorator (or decorator factory) that forces auth to exist before rendering component
7274
* Possibility of delayed initialization as mentioned in [#70](https://github.com/prescottprue/react-redux-firebase/issues/70) (more research needed)
7375

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# React Native Complete Example
2+
3+
## Getting Started
4+
5+
**NOTE** This example assumes you have `react-native` installed
6+
7+
1. Clone main repo
8+
1. Enter example folder by running `cd examples/complete/react-native`
9+
1. Install dependencies using `yarn install` or `npm install`
10+
1. Run using `react-native run-ios`
11+
12+
## Dev Scripts
13+
* `npm run dev` - starts watcher and packager together concurrently
14+
15+
## Application Structure
16+
17+
```
18+
.
19+
├─ src # Application source code
20+
│ ├── index.js # Application bootstrap and rendering
21+
│ └── Home.js # Main page
22+
├- ios # Platform specific code, config and dependencies for iOS
23+
│ ├─ GoogleService-info.plist # iOS app config file downloaded from Firebase
24+
├─ scripts # Development helper scripts
25+
│ └── watch-and-copy.js # Development helper that watches and copies changed files
26+
└─ index.ios.js # iOS Application Mounting (connects iOS -> `/src`)
27+
```
28+
29+
30+
## Running Your Own
31+
32+
If you are planning on running your own `react-native` project based on this one. Please make sure you follow the new setup steps in the [react-native recipe](/docs/recipes/react-native.md)

examples/complete/react-native/src/Home.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ const initialState = { firebase: { authError: null, auth: undefined }}
1414
const store = configureStore(initialState)
1515

1616
const iosClientId = '499842460400-teaflfd8695oilltk5qkvl5688ebgq6b.apps.googleusercontent.com'; // get this from plist file
17-
const webClientId = '603421766430-60og8n04mebic8hi49u1mrcmcdmugnd5.apps.googleusercontent.com';
18-
1917

2018
const styles = StyleSheet.create({
2119
container: {
@@ -82,13 +80,12 @@ export default class SigninSampleApp extends Component {
8280
</View>
8381
);
8482
}
85-
83+
// based on google signin example
8684
async _setupGoogleSignin() {
8785
try {
8886
await GoogleSignin.hasPlayServices({ autoResolve: true });
8987
await GoogleSignin.configure({
9088
iosClientId,
91-
webClientId,
9289
offlineAccess: false
9390
});
9491

@@ -102,12 +99,15 @@ export default class SigninSampleApp extends Component {
10299
}
103100

104101
_signIn() {
102+
const { auth } = this.props.firebase
105103
return GoogleSignin.signIn()
106104
.then((user) => {
107-
const creds = this.props.firebase.auth.GoogleAuthProvider.credential(null, user.accessToken)
108-
return this.props.firebase.auth().signInWithCredential(creds)
105+
const creds = auth.GoogleAuthProvider.credential(null, user.accessToken)
106+
return auth()
107+
.signInWithCredential(creds)
109108
.catch((err) => {
110109
console.error('error authing with firebase:', err)
110+
return Promise.reject(err)
111111
})
112112
})
113113
.catch((err) => {

examples/complete/react-native/src/store.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createStore, compose } from 'redux'
22
import rootReducer from './reducer'
33
import { firebase as fbConfig } from './config'
44
import { reactReduxFirebase } from 'react-redux-firebase'
5-
import ReactNative from 'react-native'
5+
import { AsyncStorage } from 'react-native'
66

77
export default function configureStore (initialState, history) {
88
const createStoreWithMiddleware = compose(
@@ -11,7 +11,7 @@ export default function configureStore (initialState, history) {
1111
userProfile: 'users',
1212
enableLogging: false,
1313
enableRedirectHandling: false,
14-
rn: ReactNative,
14+
rn: { AsyncStorage },
1515
}
1616
),
1717
typeof window === 'object' && typeof window.devToolsExtension !== 'undefined' ? window.devToolsExtension() : f => f

src/compose.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ export default (fbConfig, otherConfig) => next =>
8282
}
8383

8484
// Handle react-native
85-
if (configs.rn) {
86-
const { AsyncStorage } = configs.rn
85+
if (configs.ReactNative) {
86+
const { AsyncStorage } = configs.ReactNative
87+
// Stub firebase's internal's with react-native (based on firebase's react-native index file)
8788
firebase.INTERNAL.extendNamespace({
8889
INTERNAL: {
8990
reactNative: {

0 commit comments

Comments
 (0)