Skip to content

Commit

Permalink
Merge pull request #2 from reslear/pr/136
Browse files Browse the repository at this point in the history
fix: any conflicts
  • Loading branch information
joeflateau authored Aug 27, 2021
2 parents 1085f07 + f24cacc commit 896de7f
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 81 deletions.
81 changes: 57 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# CapacitorGoogleAuth

Capacitor plugin for Google Auth.

## 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.
Expand All @@ -10,11 +12,10 @@ PRs for features that are not aligned with the official Google Auth library are

(We are beginner-friendly here)



## Install

#### 1. Install package

```bash
npm i --save @codetrix-studio/capacitor-google-auth

Expand All @@ -23,65 +24,94 @@ npm i --save @codetrix-studio/[email protected]
```

#### 2. Update capacitor deps

```sh
npx cap update
```

#### 3. Migrate from 2 to 3 version

if your migrate from Capacitor 2 to Capacitor 3 [see instruction for migrate plugin to new version](#migrate-from-2-to-3)

## Usage

for capacitor 2.x.x use [instruction](https://github.com/CodetrixStudio/CapacitorGoogleAuth/blob/79129ab37288f5f5d0bb9a568a95890e852cebc2/README.md)

### 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}">
<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';

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

Use it

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

#### AngularFire2

init hook

```ts
// app.component.ts
constructor() {
this.initializeApp();
}

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

sign in function

```ts
async googleSignIn() {
let googleUser = await Plugins.GoogleAuth.signIn();
let googleUser = await GoogleAuth.signIn();
const credential = auth.GoogleAuthProvider.credential(googleUser.authentication.idToken);
return this.afAuth.auth.signInAndRetrieveDataWithCredential(credential);
}
```

#### Vue 3

```ts
// App.vue
import { defineComponent, onMounted } from 'vue'
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth'
import { defineComponent, onMounted } from 'vue';
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';

export default defineComponent({
setup() {
onMounted(() => {
GoogleAuth.init()
})
GoogleAuth.init();
});

const logIn = async () => {
const response = await GoogleAuth.signIn()
console.log(response)
}
const response = await GoogleAuth.signIn();
console.log(response);
};

return {
logIn
}
}
})
logIn,
};
},
});
```
or see more [CapacitorGoogleAuth-Vue3-example](https://github.com/reslear/CapacitorGoogleAuth-Vue3-example)

### iOS

Expand Down Expand Up @@ -114,46 +144,49 @@ Set **Client ID** :
```

Import package inside your `MainActivity`

```java
import com.codetrixstudio.capacitor.GoogleAuth.GoogleAuth;
```

Register plugin inside your `MainActivity.onCreate`

```java
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
add(GoogleAuth.class);
}});
```

## Configure

Provide configuration in root `capacitor.config.json`

```json
{
"plugins": {
"GoogleAuth": {
"scopes": ["profile", "email"],
"serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
"forceCodeForRefreshToken" : true
"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)


### Migration guide
## Migration guide

#### Migrate from 2 to 3

After [migrate to Capcitor 3](https://capacitorjs.com/docs/updating/3-0) updating you projects, see diff:

##### WEB

```diff
- import "@codetrix-studio/capacitor-google-auth";
- import { Plugins } from '@capacitor/core';
+ import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
+ import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth'

- Plugins.GoogleAuth.signIn();
+ GoogleAuth.init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ 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
1 change: 1 addition & 0 deletions ios/Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
CAP_PLUGIN_METHOD(signIn, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(refresh, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(signOut, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(init, CAPPluginReturnPromise);
)
5 changes: 5 additions & 0 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public class GoogleAuth: CAPPlugin {
NotificationCenter.default.addObserver(self, selector: #selector(handleOpenUrl(_ :)), name: Notification.Name(Notification.Name.capacitorOpenURL.rawValue), object: nil);
}

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

@objc
func signIn(_ call: CAPPluginCall) {
signInCall = call;
Expand Down
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
"author": "CodetrixStudio",
"license": "MIT",
"devDependencies": {
"@capacitor/core": "^3.0.1",
"@capacitor/android": "^3.0.1",
"@capacitor/ios": "^3.0.1",
"@types/gapi": "0.0.39",
"@types/gapi.auth2": "0.0.54",
"typescript": "^4.3.2"
"@capacitor/core": "^3.2.0",
"@capacitor/android": "^3.2.0",
"@capacitor/ios": "^3.2.0",
"@ionic/prettier-config": "^2.0.0",
"@types/gapi": "0.0.41",
"@types/gapi.auth2": "0.0.55",
"prettier": "^2.3.2",
"typescript": "^4.3.5"
},
"peerDependencies": {
"@capacitor/core": "^3"
Expand Down Expand Up @@ -56,5 +58,6 @@
},
"bugs": {
"url": "https://github.com/CodetrixStudio/CapacitorGoogleAuth.git/issues"
}
},
"prettier": "@ionic/prettier-config"
}
8 changes: 3 additions & 5 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ export default {
file: 'dist/plugin.js',
format: 'iife',
name: 'capacitorPlugin',
sourcemap: true
sourcemap: true,
},
plugins: [
nodeResolve()
]
};
plugins: [nodeResolve()],
};
2 changes: 1 addition & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Authentication, User } from "./user";
import { Authentication, User } from './user';

export interface GoogleAuthPlugin {
signIn(): Promise<User>;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { registerPlugin } from '@capacitor/core';
import type { GoogleAuthPlugin } from './definitions';

const GoogleAuth = registerPlugin<GoogleAuthPlugin>('GoogleAuth', {
web: () => import('./web').then(m => new m.GoogleAuthWeb()),
web: () => import('./web').then((m) => new m.GoogleAuthWeb()),
});

export * from './definitions';
Expand Down
24 changes: 12 additions & 12 deletions src/user.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
export interface User {
id: string;
email: string;

name: string;
familyName: string;
givenName: string;
imageUrl: string;
id: string;
email: string;

serverAuthCode: string;
authentication: Authentication;
name: string;
familyName: string;
givenName: string;
imageUrl: string;

serverAuthCode: string;
authentication: Authentication;
}

export interface Authentication {
accessToken: string;
idToken: string;
}
accessToken: string;
idToken: string;
}
Loading

0 comments on commit 896de7f

Please sign in to comment.