# Universal Google Sign-In

> One API, seven platforms. Google OAuth2 for Unity that handles Android, iOS, WebGL, Windows, macOS, UWP, and Editor with a single method call.

## Overview

Universal Integration for Google Sign-In gives you a single, platform-agnostic API for Google authentication in Unity. It handles the fiddly, per-platform OAuth flows under the hood:

-   **Android** via native Google Play Services,
-   **iOS** via `ASWebAuthenticationSession` + deep link,
-   **macOS / UWP** via system browser + deep link,
-   **Windows / Editor** via a loopback HTTP server,
-   **WebGL** via a JavaScript OAuth 2.0 implicit flow.

You write one `Init + SignIn` flow. The plugin routes to the right code path per platform.

## Supported platforms

| Platform | Flow | Client ID type |
| --- | --- | --- |
| Android | Google Play Services native | Web Client ID (Android ID auto-detected) |
| iOS | `ASWebAuthenticationSession` + Deep Link | iOS Client ID |
| macOS | System browser + Deep Link | iOS Client ID |
| UWP | System browser + Deep Link | iOS Client ID |
| Windows / Editor | Loopback HTTP server | Desktop Client ID |
| WebGL | OAuth 2.0 implicit flow | Web Client ID |

## Requirements

-   Unity **2020.3 LTS** or newer
-   A project registered in **Google Cloud Console** with the right client IDs (see below)
-   For Android: Google Play Services on the test device

## Google Cloud setup

### Create the OAuth consent screen

1.  Open [console.cloud.google.com](https://console.cloud.google.com/).
2.  Create a project and open **APIs & Services → OAuth consent screen**.
3.  Fill in app name, support email, and authorised domains. For testing, add your Gmail as a test user.

### Create client IDs

Go to **APIs & Services → Credentials → Create credentials → OAuth client ID** and create:

-   **Web Client ID** — required for Android, WebGL.
-   **iOS Client ID** — required for iOS, macOS, UWP. Note the auto-generated URL scheme (`com.googleusercontent.apps.<number>`).
-   **Android Client ID** — required for Android. Provide your package name and SHA-1 fingerprint (`keytool -list -v -keystore <your.keystore>`).
-   **Desktop Client ID** — required for Windows / Editor. Note the client secret.

Keep the Web Client ID handy — it's what you pass to Unity on most platforms, even Android. Google Play Services resolves the Android Client ID automatically at runtime.

## Installation

Import [Universal Google Sign-In](https://assetstore.unity.com/packages/slug/293326) from the Unity Asset Store, or add via Package Manager → Git URL:

```arduino
https://github.com/simple-yet-efficient/google-sign-in.git
```

## Per-platform configuration

### WebGL

```csharp
UniversalGSignIn.Init("YOUR_WEB_CLIENT_ID", OnInit);
```

In Google Cloud Console, set **Authorized JavaScript origins** and **Authorized redirect URIs** to the domain that will host your build.

### Android

```csharp
UniversalGSignIn.Init("YOUR_WEB_CLIENT_ID", OnInit);
```

> Do **not** pass the Android client ID to Unity. Google Play Services picks it up from the keystore signature at runtime.

### iOS / macOS / UWP

```csharp
UniversalGSignIn.Init(
    "YOUR_IOS_CLIENT_ID",
    OnInit,
    urlScheme: "com.googleusercontent.apps.XXXXXX-YYYYYY"
);
```

Follow Unity's [deep linking guide](https://docs.unity3d.com/Manual/deep-linking.html) to add the scheme to your Info.plist / Package.appxmanifest.

### Windows / Editor

```csharp
UniversalGSignIn.Init(
    "YOUR_DESKTOP_CLIENT_ID",
    OnInit,
    clientSecret: "YOUR_CLIENT_SECRET",
    loopbackLink: "http://localhost:3000"
);
```

The `clientSecret` is only required if you need a refresh token (offline access). For interactive-only flows, omit it.

## Usage

### Sign in

```csharp
void OnInit(bool ready) {
    if (!ready) return;
    UniversalGSignIn.SignIn(OnSignIn);
}

void OnSignIn(UniversalGSignIn.GoogleUser user) {
    if (!string.IsNullOrEmpty(user.error)) {
        Debug.LogError($"Sign-In failed: {user.error}");
        return;
    }

    var profile = user.basicProfile;
    Debug.Log($"Hello, {profile.name} <{profile.email}>");
}
```

### Offline access (refresh tokens)

```csharp
UniversalGSignIn.GrantOfflineAccess(onGranted: refreshToken => {
    // Send this to your backend to mint short-lived access tokens later.
    PlayerPrefs.SetString("google_refresh", refreshToken);
});
```

### Sign out

```csharp
UniversalGSignIn.SignOut();
bool signedIn = UniversalGSignIn.IsSignedIn();
```

### Silent sign-in on startup

```csharp
void Start() {
    UniversalGSignIn.Init(CLIENT_ID, ready => {
        if (ready && UniversalGSignIn.IsSignedIn()) {
            var profile = UniversalGSignIn.GetCurrentUserBasicProfile();
            ShowHome(profile);
        } else {
            ShowLoginButton();
        }
    });
}
```

## Error handling

Every callback carries an `error` string. Always check it before using the result:

```csharp
void OnSignIn(UniversalGSignIn.GoogleUser user) {
    if (!string.IsNullOrEmpty(user.error)) {
        // User cancelled, network offline, or config mismatch.
        return;
    }
    // Success — user.idToken can be sent to your backend.
}
```

## Troubleshooting

| Error | Likely cause | Fix |
| --- | --- | --- |
| `INVALID_SCHEME` on iOS | URL scheme not in Info.plist | Add scheme via Unity's deep-link UI |
| `OAUTH2_CONFIG_ERROR` on Android | SHA-1 mismatch in Cloud Console | Regenerate with the keystore you'll actually ship with |
| `REDIRECT_URI_MISMATCH` on Windows | Port conflict | Change `loopbackLink` to a free port |
| Sign-in returns empty user on WebGL | Origin not whitelisted | Add your domain under Authorized JavaScript origins |

## Support

-   **Issues:** [github.com/simple-yet-efficient/google-sign-in/issues](https://github.com/simple-yet-efficient/google-sign-in/issues)
-   **Email:** [mohelm97@gmail.com](mailto:mohelm97@gmail.com)

The plugin is covered by the [Unity Asset Store Standard EULA](https://unity.com/legal/as-terms).
