@hristo2612/capacitor-google-auth
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

CapacitorGoogleAuth

Capacitor plugin for Google Auth. ( This is a fixed version of @codetrix-studio/capacitor-google-auth for Capacitor 3+

Contributions

PRs are welcome and much appreciated that keeps this plugin up to date with Capacitor and official Google Auth platform library feature parity.

Try to follow good code practices. You can even help keeping the included demo updated.

PRs for features that are not aligned with the official Google Auth library are discouraged.

(We are beginner-friendly here)

Install

1. Install package

npm i @hristo2612/capacitor-google-auth

2. Update capacitor deps

npx cap update

Usage

Integration For the Browser

Add clientId meta tag to head in index.html.

<meta name="google-signin-client_id" content="{your client id here}" />

Register plugin and manually initialize

import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';

// use hook after platform dom ready
GoogleAuth.init();

Use it

GoogleAuth.signIn();

AngularFire & Firebase

init hook & sign in function

import { GoogleAuth } from '@hristo2612/capacitor-google-auth';
import { AngularFireAuth } from '@angular/fire/auth';
import firebase from 'firebase/app';
import 'firebase/auth';

// app.component.ts
constructor(private afAuth: AngularFireAuth) {
  this.initializeApp();
}

initializeApp() {
  this.platform.ready().then(() => {
    GoogleAuth.init();
  });
}

async googleSignIn() {
  let googleUser = await GoogleAuth.signIn();
  const credential = firebase.auth.GoogleAuthProvider.credential(googleUser.authentication.idToken);
  return this.afAuth.signInWithCredential(credential);
}

iOS Integration

Make sure you have GoogleService-Info.plist with CLIENT_ID

Add REVERSED_CLIENT_ID as url scheme to Info.plist

Android Integration

Inside your strings.xml

<resources>
  <string name="server_client_id">Your Web Client Key ( Same as the one in the index.html meta tag )</string>
</resources>

Import package inside your MainActivity

import com.hristo2612.capacitor.google.auth;

Register plugin inside your MainActivity.java

public class MainActivity extends BridgeActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerPlugin(GoogleAuth.class);
    }
}

Configure

Provide configuration in root capacitor.config.json

{
  "plugins": {
    "GoogleAuth": {
      "scopes": ["profile", "email"],
      "serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
      "forceCodeForRefreshToken": true
    }
  }
}

Note : forceCodeForRefreshToken force user to select email address to regenerate AuthCode used to get a valid refreshtoken (work on iOS and Android) (This is used for offline access and serverside handling)

Readme

Keywords

Package Sidebar

Install

npm i @hristo2612/capacitor-google-auth

Weekly Downloads

1

Version

0.0.1

License

MIT

Unpacked Size

22.7 kB

Total Files

27

Last publish

Collaborators

  • hristo2612