3/29/2016: Forked and Updated by Sam Muggleworth (PointSource, LLC)
ATTENTION: The NPM registry currently returns an older version of this plugin. This README contains documentation for the most recent version.
- Description
- Screenshots
- Google API setup
- Installation (CLI / Plugman)
- Installation (PhoneGap Build)
- Usage
- Troubleshooting
- Changelog
- License
This plugin allows you to authenticate and identify users with Google Sign-In on iOS and Android. Out of the box, you'll get email, display name, profile picture url, and user id. You can also configure it to get an idToken and serverAuthCode.
This plugin only wraps access to the Google Sign-In API. Further API access should be implemented per use-case, per developer.
Android
iOS
To communicate with Google you need to do some tedious setup, sorry.
It is (strongly) recommended that you use the same project for both iOS and Android.
To get your iOS REVERSED_CLIENT_ID
, generate a configuration file here.
This GoogleService-Info.plist
file contains the REVERSED_CLIENT_ID
you'll need during installation.
To configure Android, generate a configuration file here. Once Google Sign-In is enabled Google will automatically create necessary credentials in Developer Console. There is no need to add the generated google-services.json file into your cordova project.
Make sure you execute the keytool
steps as explained here or authentication will fail.
If you want to get an ID Token and/or serverAuthCode back from the Sign In Process, you will need to pass the client ID for your project's web application. This can be found on your project's API credentials page on the Google Developer's Console.
This plugin is compatible with Cordova Plugman, compatible with PhoneGap 3.0 CLI, here's how it works with the CLI (backup your project first!):
Using the Cordova CLI and npm
$ cordova plugin add cordova-plugin-googleplus --variable REVERSED_CLIENT_ID=myreversedclientid
$ cordova prepare
To fetch the latest version from GitHub, use
$ cordova plugin add https://github.com/EddyVerbruggen/cordova-plugin-googleplus --variable REVERSED_CLIENT_ID=myreversedclientid
$ cordova prepare
GooglePlus.js is brought in automatically. There is no need to change or add anything in your html.
Add this to your config.xml:
<gap:plugin name="cordova-plugin-googleplus" source="npm">
<param name="REVERSED_CLIENT_ID" value="myreversedclientid" />
</gap:plugin>
Check the demo app to get you going quickly, or hurt yourself and follow these steps.
Note that none of these methods should be called before deviceready
has fired.
You'll want to check this before showing a 'Sign in with Google+' button. On Android it will check whether or not Google Play Services is available. It's more likely than not that it is.
window.plugins.googleplus.isAvailable(
function (available) {
if (available) {
// show the Google+ sign-in button
}
}
);
3/29/16: It is no longer required to check this, as sign-in on iOS takes the user to a SafariViewController through the Google SDK, instead of the separate Safari browser. Android uses the accounts on the device.
The login function walks the user through the Google Auth process.
All of the options are optional, however there are a few caveats.
The space-separated string of scopes
will be requested exactly as passed in. Refer to the Google Scopes documentation for info on valid scopes that can be requested.
To get an idToken
on Android, you must pass in your webClientId
. On iOS, the idToken
is included in the sign in result by default.
The webClientId
and offline
options are optional, however offline
will only be evaluated if a webClientId
is passed in as well. That is to say, if offline is true, but no webClientId is provided, the serverAuthCode
will NOT be requested.
Recapping, pass in a webClientId
, get back on idToken
on iOS and Android. Pass in a webClientId
and set offline as true
, you'll get back an idToken
and a serverAuthCode
on iOS and Android. No webClientId
, no serverAuthCode
.
window.plugins.googleplus.login(
{
'scopes': '... ', // optional, space-separated list of scopes, If not included or empty, defaults to `profile` and `email`.
'webClientId': 'client id of the web app/server side', // optional clientId of your Web application from Credentials settings of your project - On Android, this MUST be included to get an idToken. On iOS, it is not required.
'offline': true, // optional, but requires the webClientId - if set to true the plugin will also return a serverAuthCode, which can be used to grant offline access to a non-Google server
},
function (obj) {
alert(JSON.stringify(obj)); // do something useful instead of alerting
},
function (msg) {
alert('error: ' + msg);
}
);
The success callback (second argument) gets a JSON object with the following contents, with example data of my Google+ account:
obj.email // '[email protected]'
obj.userId // user id
obj.displayName // 'Eddy Verbruggen'
obj.imageUrl // 'http://link-to-my-profilepic.google.com'
obj.idToken // idToken that can be exchanged to verify user identity.
obj.serverAuthCode // Auth code that can be exchanged for an access token and refresh token for offline access
Additional user information is available by use case. Add the scopes needed to the scopes option then return the info to the result object being created in the handleSignInResult
and didSignInForUser
functions on Android and iOS, respectively.
On Android, the error callback (third argument) receives an error status code if authentication was not successful. A description of those status codes can be found on Google's android developer website at GoogleSignInStatusCodes.
On iOS, the error callback will include an NSError localizedDescription.
When the user comes back to your app and you're not sure if he needs to log in,
you can call trySilentLogin
to try logging him in.
If it succeeds you will get the same object as the login
function gets,
but if it fails it will not show the authentication dialog to the user.
Calling trySilentLogin
is done the same as login
, except for the function name.
window.plugins.googleplus.trySilentLogin(
{
'scopes': '... ', // optional - space-separated list of scopes, If not included or empty, defaults to `profile` and `email`.
'webClientId': 'client id of the web app/server side', // optional - clientId of your Web application from Credentials settings of your project - On Android, this MUST be included to get an idToken. On iOS, it is not required.
'offline': true, // Optional, but requires the webClientId - if set to true the plugin will also return a serverAuthCode, which can be used to grant offline access to a non-Google server
},
function (obj) {
alert(JSON.stringify(obj)); // do something useful instead of alerting
},
function (msg) {
alert('error: ' + msg);
}
);
It is strongly recommended that trySilentLogin is implemented with the same options as login, to avoid any potential complications.
This will clear the OAuth2 token.
window.plugins.googleplus.logout(
function (msg) {
alert(msg); // do something useful instead of alerting
}
);
This will clear the OAuth2 token and forget which account was used to login. On Android this will always force the user to authenticate the app again, on iOS using logout seems to do the job already. Need to investigate this a bit more..
window.plugins.googleplus.disconnect(
function (msg) {
alert(msg); // do something useful instead of alerting
}
);
-
Q: After authentication I'm not redirected back to my app.
-
A: You probably changed the bundle id of your app after installing this plugin. Make sure that (on iOS) the
CFBundleURLTypes
bit in your.plist
file is the same as the actual bundle id originating fromconfig.xml
. -
Q: I can't get authentication to work on Android. And why is there no ANDROID API KEY?
-
A: On Android you need to execute the
keytool
steps, see the installation instructions for details. -
Q: OMG $@#*! the Android build is failing
-
A: You need to have Android Support Repository and Android Support Library installed in the Android SDK manager. Make sure you're using a fairly up to date version of those.
- [pre-release] 4.0.9: Android refactored to use the GoogleSignIn SDK. Modified usage. See #193
- 4.0.8: Fix for Android 6 where it would crash while asking for permission. Thx #166!
- 4.0.7: Re-added a missing framework for iOS. Thx #168!
- 4.0.6: Updated iOS GoogleSignIn SDK to 2.4.0. Thx #153!
- 4.0.5: Fixed a broken import on iOS
- 4.0.4: Using framework tags again for Android.
- 4.0.3: On iOS
isAvailable
always returns try since that should be fine with the new Google Sign-In framework. Re-added imageUrl to the result of Sign-In on iOS. - 4.0.1: Login on Android would crash the app if
isAvailable
was invoked beforehand. - 4.0.0: Removed the need for
iosApiKey
, reverted Android to Google playservices framework for wider compatibility, documented scopes feature a bit. - 3.0.0: Using Google Sign-In for iOS, instead of Google+.
- 1.1.0: Added
isAvailable
, for issue #37 - 1.0.0: Initial version supporting iOS and Android
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.