Google Authentication

Ionic Cloud has been deprecated. To continue using Ionic services, migrate to Ionic Pro before Jan. 31, 2018.

Setup

Native Login

This is the documentation for implementing 3-legged authentication with the InAppBrowser plugin. For native login, proceed to Google Native.

It it highly recomended that you implement native login, as Google will begin to drop support for InAppBrowser authentication on October 20th, 2016 for new projects and will start refusing authentication requests for old ones on April 20th, 2017. Here’s their article discussing it: developers.googleblog.com

Create a Google Project

The first step in setting up your Google authentication integration is to create a project on Google.

You’ll use these details in the next section.

Client ID & Secret

Now that you’ve got an app setup, you need to let us know about the Client ID and Client Secret. You can find both details listed in the dialog referenced above.

App Implementation

Setup

Plugin Installation

The InAppBrowser plugin is used to login your users with Google authentication.

$ cordova plugin add cordova-plugin-inappbrowser --save

Injecting Auth and User

Import Auth and User from the Cloud Client and specify them as dependencies in your component constructor.

  • auth is a service that deals with user authentication.
  • user is a reference to the current user, whether that user is anonymous or authenticated.
import { Component } from '@angular/core';
import { Auth, User } from '@ionic/cloud-angular';

@Component( ... )
export class LoginPage {
  constructor(public auth: Auth, public user: User) {
    ...
  }
}

Specify ionic.cloud as a dependency for your module. This gives you access to $ionicAuth and $ionicUser.

  • $ionicAuth is a service that deals with user authentication.
  • $ionicUser is a reference to the current user, whether that user is anonymous or authenticated.
angular.module('myapp.controllers', ['ionic.cloud'])

.controller('MyCtrl', function($scope, $ionicAuth, $ionicUser) {
  ...
})

Login

this.auth.login('google').then( ... );
$ionicAuth.login('google').then( ... );

Logout

Logging out clears the current user context.

this.auth.logout();
$ionicAuth.logout();

Social Data

We store social data in user.social.google. Unlike your custom data which uses user.get() and user.set(), user.social.google is just an object with the following properties.

uid Google user ID
data Google data object
data.profile_picture Google profile picture
data.email Google email
data.full_name The user’s full name
data.raw_data The data object straight from Google (see these docs)

Example:

const full_name = this.user.social.google.data.full_name;
const profile_picture = this.user.social.google.data.profile_picture;
const google_raw_data = this.user.social.google.data.raw_data;
var full_name = $ionicUser.social.google.data.full_name
var profile_picture = $ionicUser.social.google.data.profile_picture
var google_raw_data = $ionicUser.social.google.data.raw_data;

Services

    API

      General