npm package: @hristo2612/capacitor-google-auth
Description: Capacitor Plugin for authenticating Google Users
Author: hristo2612
Latest version: 0.0.1

# CapacitorGoogleAuth

Capacitor plugin for Google Auth. ( This is a fixed version of [@codetrix-studio/capacitor-google-auth](https://github.com/CodetrixStudio/CapacitorGoogleAuth) 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

```bash
npm i @hristo2612/capacitor-google-auth
```

#### 2. Update capacitor deps

```sh
npx cap update
```

## Usage

### Integration For the Browser

Add [`clientId`](https://developers.google.com/identity/sign-in/web/sign-in#specify_your_apps_client_id) meta tag to head in index.html.

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

Register plugin and manually initialize

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

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

Use it

```ts
GoogleAuth.signIn();
```

#### AngularFire & Firebase

init hook & sign in function

```ts
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`

```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`

```java
import com.hristo2612.capacitor.google.auth;
```

Register plugin inside your `MainActivity.java`

```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`

```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)