forked from CodetrixStudio/CapacitorGoogleAuth
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from reslear/pr/136
fix: any conflicts
- Loading branch information
Showing
12 changed files
with
153 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
||
|
@@ -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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.