Skip to content

Commit

Permalink
refactor(web): supporting .ts and.js config, exported types (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
reslear authored Sep 6, 2021
1 parent 4859f9c commit eaabf9d
Show file tree
Hide file tree
Showing 9 changed files with 1,861 additions and 167 deletions.
83 changes: 64 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,32 @@ for capacitor 2.x.x use [instruction](https://github.com/CodetrixStudio/Capacito

### WEB

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

```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();
GoogleAuth.initialize({
client_id: 'CLIENT_ID.apps.googleusercontent.com',
scopes: ['profile', 'email'],
grantOfflineAccess: true,
});
```

or if need use meta tags

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

#### Options

- `client_id` - The app's client ID, found and created in the Google Developers Console.
- `scopes` – same as [Configure](#Configure) scopes
- `grantOfflineAccess` – boolean, default `false`, Set if your application needs to refresh access tokens when the user is not present at the browser.

Use it

```ts
Expand All @@ -72,7 +83,7 @@ constructor() {

initializeApp() {
this.platform.ready().then(() => {
GoogleAuth.init()
GoogleAuth.initialize()
})
}
```
Expand All @@ -97,7 +108,7 @@ import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
export default defineComponent({
setup() {
onMounted(() => {
GoogleAuth.init();
GoogleAuth.initialize();
});

const logIn = async () => {
Expand All @@ -111,32 +122,37 @@ export default defineComponent({
},
});
```

or see more [CapacitorGoogleAuth-Vue3-example](https://github.com/reslear/CapacitorGoogleAuth-Vue3-example)

### iOS

1. Create in Google cloud console credential **Client ID for iOS** and get **Client ID** and **iOS URL scheme**

2. Add **identifier** `REVERSED_CLIENT_ID` as **URL schemes** to `Info.plist` from **iOS URL scheme**<br>
(Xcode: App - Targets/App - Info - URL Types, click plus icon)

3. Set **Client ID** one of the ways:
1. Set in `capacitor.config.json`
- `iosClientId` - specific key for iOS
- `clientId` - or common key for Android and iOS
3. Download `GoogleService-Info.plist` file with `CLIENT_ID` and copy to **ios/App/App** necessarily through Xcode for indexing.
(Xcode: App - Targets/App - Info - URL Types, click plus icon)

3. Set **Client ID** one of the ways:
1. Set in `capacitor.config.json`
- `iosClientId` - specific key for iOS
- `clientId` - or common key for Android and iOS
2. Download `GoogleService-Info.plist` file with `CLIENT_ID` and copy to **ios/App/App** necessarily through Xcode for indexing.

plugin first use `iosClientId` if not found use `clientId` if not found use value `CLIENT_ID` from file `GoogleService-Info.plist`

### Android

Set **Client ID** :

1. In `capacitor.config.json`
- `androidClientId` - specific key for Android
- `clientId` - or common key for Android and iOS

- `androidClientId` - specific key for Android
- `clientId` - or common key for Android and iOS

2. or set inside your `strings.xml`

plugin first use `androidClientId` if not found use `clientId` if not found use value `server_client_id` from file `strings.xml`

```xml
<resources>
<string name="server_client_id">Your Web Client Key</string>
Expand All @@ -159,6 +175,12 @@ this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{

## Configure

| Name | Type | Default | Description |
| ------------------------ | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| scopes | string[] | [] | Scopes that you might need to request to access Google APIs<br>https://developers.google.com/identity/protocols/oauth2/scopes<br><br>example: `["profile", "email"]` |
| serverClientId | string | '' | This is used for offline access and serverside handling<br><br>example: `xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com` |
| forceCodeForRefreshToken | boolean | false | Force user to select email address to regenerate AuthCode <br>used to get a valid refreshtoken (work on iOS and Android) |

Provide configuration in root `capacitor.config.json`

```json
Expand All @@ -173,10 +195,33 @@ Provide configuration in root `capacitor.config.json`
}
```

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)
or in `capacitor.config.ts`

```ts
/// <reference types="'@codetrix-studio/capacitor-google-auth'" />

const config: CapacitorConfig = {
plugins: {
GoogleAuth: {
scopes: ['profile', 'email'],
serverClientId: 'xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com',
forceCodeForRefreshToken: true,
},
},
};

export default config;
```

## Migration guide

#### Migrate from 3.0.2 to 3.1.0

```diff
- GoogleAuth.init()
+ GoogleAuth.initialize()
```

#### Migrate from 2 to 3

After [migrate to Capcitor 3](https://capacitorjs.com/docs/updating/3-0) updating you projects, see diff:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ public void load() {
googleSignInClient = GoogleSignIn.getClient(this.getContext(), googleSignInOptions);
}

@PluginMethod
public void init(PluginCall call) {
call.unimplemented();
}

@PluginMethod()
public void signIn(PluginCall call) {
saveCall(call);
Expand Down Expand Up @@ -105,4 +100,10 @@ public void signOut(final PluginCall call) {
googleSignInClient.signOut();
call.resolve();
}

@PluginMethod()
public void initialize(final PluginCall call) {
call.resolve();
}

}
2 changes: 1 addition & 1 deletion ios/Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
CAP_PLUGIN_METHOD(signIn, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(refresh, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(signOut, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(init, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(initialize, CAPPluginReturnPromise);
)
4 changes: 2 additions & 2 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class GoogleAuth: CAPPlugin {
}

@objc
func `init`(_ call: CAPPluginCall) {
call.unimplemented("Not available on iOS")
func initialize(_ call: CAPPluginCall) {
call.resolve();
}

@objc
Expand Down
Loading

0 comments on commit eaabf9d

Please sign in to comment.