|
1 | 1 | # React Native |
2 | 2 |
|
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. |
4 | 6 |
|
5 | 7 | ## Setup |
6 | 8 |
|
7 | 9 | 1. Click "Add Firebase To iOS" |
8 | 10 | <!-- TODO: Confirm this and get a picture --> |
9 | 11 | 1. Download `GoogleService-info.plist` |
10 | 12 | 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 |
12 | 15 |
|
13 | 16 |
|
14 | 17 | ## Example App Snippet |
15 | 18 |
|
16 | 19 | This snippet is a condensed version of [react-native complete example](/examples/complete/react-native). |
17 | 20 |
|
18 | 21 | ```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' |
20 | 26 | import { |
21 | 27 | AppRegistry, |
22 | 28 | StyleSheet, |
23 | 29 | Text, |
24 | 30 | View, |
25 | 31 | 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) |
27 | 36 |
|
28 | | -import { GoogleSignin, GoogleSigninButton } from 'react-native-google-signin'; |
29 | 37 | const iosClientId = '499842460400-teaflfd8695oilltk5qkvl5688ebgq6b.apps.googleusercontent.com' // get this from plist file |
30 | 38 |
|
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 | +}) |
35 | 57 |
|
| 58 | +@firebaseConnect() |
| 59 | +@connect(({ firebase }) => ({ |
| 60 | + auth: pathToJS(firebase, 'auth', undefined) |
| 61 | +})) |
| 62 | +export default class GoogleSigninSampleApp extends Component { |
36 | 63 | componentDidMount() { |
37 | | - this._setupGoogleSignin(); |
| 64 | + this._setupGoogleSignin() |
38 | 65 | } |
39 | 66 |
|
40 | 67 | 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) { |
42 | 77 | return ( |
43 | 78 | <View style={styles.container}> |
44 | 79 | <GoogleSigninButton |
45 | 80 | style={{width: 212, height: 48}} |
46 | 81 | size={GoogleSigninButton.Size.Standard} |
47 | 82 | color={GoogleSigninButton.Color.Auto} |
48 | | - onPress={this._signIn.bind(this)} |
| 83 | + onPress={() => this._signIn()} |
49 | 84 | /> |
50 | 85 | </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 | + ) |
69 | 87 | } |
| 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 | + ) |
70 | 103 | } |
71 | 104 |
|
| 105 | + // based on react-native-google-signin example |
72 | 106 | async _setupGoogleSignin() { |
73 | 107 | try { |
74 | | - await GoogleSignin.hasPlayServices({ autoResolve: true }); |
| 108 | + await GoogleSignin.hasPlayServices({ autoResolve: true }) |
75 | 109 | await GoogleSignin.configure({ |
76 | 110 | iosClientId, |
77 | | - // webClientId: '603421766430-60og8n04mebic8hi49u1mrcmcdmugnd5.apps.googleusercontent.com', |
78 | 111 | offlineAccess: false |
79 | | - }); |
| 112 | + }) |
80 | 113 |
|
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) |
84 | 117 | } |
85 | 118 | catch(err) { |
86 | | - console.log("Google signin error", err.code, err.message); |
| 119 | + console.log("Google signin error", err.code, err.message) |
87 | 120 | } |
88 | 121 | } |
89 | 122 |
|
90 | 123 | _signIn() { |
| 124 | + const { auth } = this.props.firebase |
91 | 125 | return GoogleSignin.signIn() |
92 | 126 | .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) |
95 | 129 | }) |
96 | 130 | .catch((err) => { |
97 | | - console.log('WRONG SIGNIN', err); |
| 131 | + console.error('error authing with firebase:', err) |
| 132 | + return Promise.reject(err) |
98 | 133 | }) |
99 | 134 | } |
100 | 135 |
|
101 | 136 | _signOut() { |
102 | 137 | return GoogleSignin.revokeAccess() |
103 | 138 | .then(() => GoogleSignin.signOut()) |
104 | | - .then(() => { |
105 | | - this.setState({user: null}); |
106 | | - }) |
| 139 | + .then(() => this.props.firebase.logout()) |
107 | 140 | } |
108 | 141 | } |
109 | 142 |
|
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) |
130 | 144 |
|
131 | 145 | ``` |
0 commit comments