Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unimodule] expo-google-sign-in #2543

Merged
merged 23 commits into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/expoview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ dependencies {
api project(':expo-image-loader-interface')
api project(':expo-gl')
api project(':expo-gl-cpp')
api project(':expo-google-sign-in')
api project(':expo-sms')
api project(':expo-print')
api project(':expo-media-library')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import expo.modules.facedetector.FaceDetectorPackage;
import expo.modules.filesystem.FileSystemPackage;
import expo.modules.gl.GLPackage;
import expo.modules.google.signin.GoogleSignInPackage;
import expo.modules.location.LocationPackage;
import expo.modules.medialibrary.MediaLibraryPackage;
import expo.modules.permissions.PermissionsPackage;
Expand Down Expand Up @@ -108,6 +109,7 @@ public class ExponentPackage implements ReactPackage {
new FaceDetectorPackage(),
new ConstantsPackage(),
new GLPackage(),
new GoogleSignInPackage(),
new PermissionsPackage(),
new SMSPackage(),
new PrintPackage(),
Expand Down
1 change: 1 addition & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def includeUniversalModule = { name ->
'expo-font-interface',
'expo-gl',
'expo-gl-cpp',
'expo-google-sign-in',
'expo-image-loader-interface',
'expo-local-authentication',
'expo-location',
Expand Down
48 changes: 48 additions & 0 deletions apps/native-component-list/components/GoogleSignInButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import { Image, StyleSheet, Text, TouchableOpacity, View } from 'react-native';

const googleIcon = {
uri:
'https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Google_%22G%22_Logo.svg/200px-Google_%22G%22_Logo.svg.png',
};

export default class GoogleSignInButton extends React.PureComponent {
static defaultProps = {
onPress() {},
};
render() {
const { children, style, ...props } = this.props;
return (
<TouchableOpacity
activeOpacity={0.6}
style={StyleSheet.flatten([styles.touchable, style])}
{...props}>
<View style={styles.content}>
<Image source={googleIcon} style={styles.icon} />
<Text style={styles.text}>{children}</Text>
</View>
</TouchableOpacity>
);
}
}

const styles = StyleSheet.create({
touchable: {
shadowOpacity: 0.2,
shadowRadius: 1.5,
shadowOffset: { width: 0, height: 1 },
overflow: 'visible',
shadowColor: 'black',
backgroundColor: 'white',
borderRadius: 4,
},
content: {
flexDirection: 'row',
paddingHorizontal: 16,
paddingVertical: 12,
alignItems: 'center',
justifyContent: 'space-between',
},
icon: { width: 24, aspectRatio: 1 },
text: { color: 'gray', marginLeft: 12, fontSize: 16, fontWeight: '600' },
});
3 changes: 2 additions & 1 deletion apps/native-component-list/constants/Icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default {
FileSystem: require('../assets/icons/api/FileSystem.png'),
Geocoding: require('../assets/icons/api/Geocoding.png'),
GL: require('../assets/icons/api/GL.png'),
GoogleLogin: require('../assets/icons/api/GoogleLogin.png'),
Google: require('../assets/icons/api/GoogleLogin.png'),
GoogleSignIn: require('../assets/icons/api/GoogleLogin.png'),
Haptic: require('../assets/icons/api/Haptic.png'),
ImagePicker: require('../assets/icons/api/ImagePicker.png'),
KeepAwake: require('../assets/icons/api/KeepAwake.png'),
Expand Down
6 changes: 4 additions & 2 deletions apps/native-component-list/navigation/MainTabNavigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import FacebookLoginScreen from '../screens/FacebookLoginScreen';
import GestureHandlerPinchScreen from '../screens/GestureHandlerPinchScreen';
import GestureHandlerListScreen from '../screens/GestureHandlerListScreen';
import GestureHandlerSwipeableScreen from '../screens/GestureHandlerSwipeableScreen';
import GoogleLoginScreen from '../screens/GoogleLoginScreen';
import GoogleScreen from '../screens/GoogleScreen';
import GoogleSignInScreen from '../screens/GoogleSignInScreen';
import RemindersScreen from '../screens/RemindersScreen';
import SensorScreen from '../screens/SensorScreen';
import GeocodingScreen from '../screens/GeocodingScreen';
Expand Down Expand Up @@ -128,7 +129,8 @@ const ExpoApisStackNavigator = createStackNavigator(
FacebookLogin: { screen: FacebookLoginScreen },
FileSystem: { screen: FileSystemScreen },
Font: { screen: FontScreen },
GoogleLogin: { screen: GoogleLoginScreen },
Google: GoogleScreen,
GoogleSignIn: GoogleSignInScreen,
Haptic: { screen: HapticScreen },
Calendars: { screen: CalendarsScreen },
Constants: { screen: ConstantsScreen },
Expand Down
3 changes: 2 additions & 1 deletion apps/native-component-list/screens/ExpoApisScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export default class ExpoApisScreen extends React.Component {
'FileSystem',
'Font',
'Geocoding',
'GoogleLogin',
'Google',
'GoogleSignIn',
'Haptic',
'ImagePicker',
'ImageManipulator',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class GoogleLoginScreen extends React.Component {
static navigationOptions = {
title: 'Google',
};

render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
Expand Down
133 changes: 133 additions & 0 deletions apps/native-component-list/screens/GoogleSignInScreen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import React from 'react';
import { GoogleSignIn, Localization } from 'expo';
import { Image, StyleSheet, Text, View } from 'react-native';

import GoogleSignInButton from '../components/GoogleSignInButton';

export default class GoogleSignInScreen extends React.Component {
static navigationOptions = {
title: 'Google Sign-In',
};

state = { user: null };

componentDidMount() {
this._configureAsync();
}

_configureAsync = async () => {
const language = Localization.locale;
try {
await GoogleSignIn.initAsync({
isOfflineEnabled: true,
isPromptEnabled: true,
clientId: '603386649315-vp4revvrcgrcjme51ebuhbkbspl048l9.apps.googleusercontent.com',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it going to work for all Expo developers (for example for Facebook apps in development mode one would need to invite all other developers testing NCL for them to be able to authenticate with Facebook to an unpublished Facebook app)? Certainly it could be extracted to some constant at the top of the file. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK this module will only work in standalone. You need a URI scheme defined in your info.plist to run. This is really just for testing. The client ID value here is an override value, the real value will be inferred from a GoogleServices-Info.plist. I think the client ID corresponds to the custom URI scheme, so this probably only works for this individual value.

I imagine that in order to get the firebase modules working, we will need a way to include a custom GoogleServices-Info.plist before building a standalone IPA. Once we add this method, I may just remove the clientId value altogether.

Re: Facebook

this works for everyone

language,
});
} catch ({ message }) {
console.error('Demo: Error: init: ' + message);
}
this._syncUserWithStateAsync();
};

_syncUserWithStateAsync = async () => {
/*
const user = await GoogleSignIn.signInSilentlyAsync();
this.setState({ user });
*/

if (await GoogleSignIn.signInSilentlyAsync()) {
const photoURL = await GoogleSignIn.getPhotoAsync(256);
this.setState({
user: {
...GoogleSignIn.currentUser.toJSON(),
photoURL: photoURL || GoogleSignIn.currentUser.photoURL,
},
});
} else {
this.setState({ user: null });
}
};

get buttonTitle() {
return this.state.user ? 'Sign-Out of Google' : 'Sign-In with Google';
}

render() {
const { user } = this.state;
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
{user && <GoogleProfile {...user} />}
<GoogleSignInButton onPress={this._toggleAuth}>{this.buttonTitle}</GoogleSignInButton>
</View>
);
}

_toggleAuth = () => {
if (this.state.user) {
this._signOutAsync();
} else {
this._signInAsync();
}
};

_signOutAsync = async () => {
try {
// await GoogleSignIn.disconnectAsync();
await GoogleSignIn.signOutAsync();
console.log('Log out successful');
} catch ({ message }) {
console.error('Demo: Error: logout: ' + message);
} finally {
this.setState({ user: null });
}
};

_signInAsync = async () => {
try {
await GoogleSignIn.askForPlayServicesAsync();
const { type, user } = await GoogleSignIn.signInAsync();
console.log({ type, user });
if (type === 'success') {
this._syncUserWithStateAsync();
}
} catch ({ message }) {
console.error('login: Error:' + message);
}
};
}

class GoogleProfile extends React.PureComponent {
render() {
const { photoURL, displayName, email } = this.props;
return (
<View style={styles.container}>
{photoURL && (
<Image
source={{
uri: photoURL,
}}
style={styles.image}
/>
)}
<View style={{ marginLeft: 12 }}>
<Text style={styles.text}>{displayName}</Text>
<Text style={styles.text}>{email}</Text>
</View>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flexDirection: 'row',
paddingHorizontal: 16,
paddingVertical: 12,
alignItems: 'center',
justifyContent: 'space-between',
marginBottom: 24,
},
image: { width: 128, borderRadius: 64, aspectRatio: 1 },
text: { color: 'black', fontSize: 16, fontWeight: '600' },
});
1 change: 1 addition & 0 deletions apps/test-suite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async function getTestModulesAsync() {
require('./tests/Constants'),
require('./tests/Contacts'),
require('./tests/FileSystem'),
require('./tests/GoogleSignIn'),
require('./tests/Localization'),
require('./tests/Location'),
require('./tests/Linking'),
Expand Down
Loading