Google Auth plugin for capacitor.
npm install @codetrix-studio/capacitor-google-auth@codetrix-studio/capacitor-google-auth
CAPACITOR 6
Capacitor plugin for Google Auth.
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)
#### 1. Install package
``sh
npm i --save @codetrix-studio/capacitor-google-auth
#### 2. Update capacitor deps
`sh
npx cap update
`Updating
If need migrate to different Capacitor versions see instruction for migrate plugin to new version.
Usage
$3
Register plugin and manually initialize
`ts
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';// use hook after platform dom ready
GoogleAuth.initialize({
clientId: 'CLIENT_ID.apps.googleusercontent.com',
scopes: ['profile', 'email'],
grantOfflineAccess: true,
});
`or if need use meta tags (Optional):
`html
`#### Options
-
clientId - The app's client ID, found and created in the Google Developers Console.
- scopes – same as 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
GoogleAuth.signIn();
`#### Angular
init hook
`ts
// app.component.ts
constructor() {
this.initializeApp();
}initializeApp() {
this.platform.ready().then(() => {
GoogleAuth.initialize()
})
}
`sign in function
`ts
async googleSignIn() {
let googleUser = await GoogleAuth.signIn(); /*
If you use Firebase you can forward and use the logged in Google user like this:
*/
const credential = auth.GoogleAuthProvider.credential(googleUser.authentication.idToken);
return this.afAuth.auth.signInAndRetrieveDataWithCredential(credential);
}
`#### Vue 3
`vue
`or see more CapacitorGoogleAuth-Vue3-example
$3
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
(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$3
Set Client ID :
1. In
capacitor.config.json -
androidClientId - specific key for Android
- clientId - or common key for Android and iOS2. or set inside your
strings.xmlplugin first use
androidClientId if not found use clientId if not found use value server_client_id from file strings.xml`xml
Your Web Client Key
`Refresh method
This method should be called when the app is initialized to establish if the user is currently logged in. If true, the method will return an accessToken, idToken and an empty refreshToken.
`ts
checkLoggedIn() {
GoogleAuth.refresh()
.then((data) => {
if (data.accessToken) {
this.currentTokens = data;
}
})
.catch((error) => {
if (error.type === 'userLoggedOut') {
this.signin()
}
});
}
`Configure
| Name | Type | Description |
| ------------------------ | -------- | ----------------------------------------------------------------------------------------------------------------------------- |
| clientId | string | The app's client ID, found and created in the Google Developers Console. |
| iosClientId | string | Specific client ID key for iOS |
| androidClientId | string | Specific client ID key for Android |
| scopes | string[] | Scopes that you might need to request to access Google APIs
https://developers.google.com/identity/protocols/oauth2/scopes |
| serverClientId | string | This ClientId used for offline access and server side handling |
| forceCodeForRefreshToken | boolean | Force user to select email address to regenerate AuthCode
used to get a valid refreshtoken (work on iOS and Android) |
Provide configuration in root
capacitor.config.json`json
{
"plugins": {
"GoogleAuth": {
"scopes": ["profile", "email"],
"serverClientId": "xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
"forceCodeForRefreshToken": true
}
}
}
`or in
capacitor.config.ts`ts
/// const config: CapacitorConfig = {
plugins: {
GoogleAuth: {
scopes: ['profile', 'email'],
serverClientId: 'xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com',
forceCodeForRefreshToken: true,
},
},
};
export default config;
`API
initialize(...)
* signIn()
* refresh()
* signOut()
* Interfaces
$3
`typescript
initialize(options?: InitOptions) => void
`Initializes the GoogleAuthPlugin, loading the gapi library and setting up the plugin.
| Param | Type | Description |
| ------------- | --------------------------------------------------- | ---------------------------------- |
|
options | InitOptions | - Optional initialization options. |Since: 3.1.0
--------------------
$3
`typescript
signIn() => Promise
`Initiates the sign-in process and returns a Promise that resolves with the user information.
Returns: Promise<User>
--------------------
$3
`typescript
refresh() => Promise
`Refreshes the authentication token and returns a Promise that resolves with the updated authentication details.
Returns: Promise<Authentication>
--------------------
$3
`typescript
signOut() => Promise
`Signs out the user and returns a Promise.
Returns: Promise<any>
--------------------
$3
#### InitOptions
| Prop | Type | Description | Default | Since |
| ------------------------ | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------ | ----- |
|
clientId | string | The app's client ID, found and created in the Google Developers Console. Common for Android or iOS. The default is defined in the configuration. | | 3.1.0 |
| scopes | string[] | Specifies the scopes required for accessing Google APIs The default is defined in the configuration. | | |
| grantOfflineAccess | boolean | Set if your application needs to refresh access tokens when the user is not present at the browser. In response use serverAuthCode key | false | 3.1.0 |
#### User
| Prop | Type | Description |
| -------------------- | --------------------------------------------------------- | ------------------------------------------------------------------- |
|
id | string | The unique identifier for the user. |
| email | string | The email address associated with the user. |
| name | string | The user's full name. |
| familyName | string | The family name (last name) of the user. |
| givenName | string | The given name (first name) of the user. |
| imageUrl | string | The URL of the user's profile picture. |
| serverAuthCode | string | The server authentication code. |
| authentication | Authentication | The authentication details including access, refresh and ID tokens. |
#### Authentication
| Prop | Type | Description |
| ------------------ | ------------------- | ------------------------------------------------ |
|
accessToken | string | The access token obtained during authentication. |
| idToken | string | The ID token obtained during authentication. |
| refreshToken | string | The refresh token. |Migration guide
#### Migrate from 3.3.x to 3.4.x
Install version 3.4.x:
`sh
npm i --save @codetrix-studio/capacitor-google-auth@^3.4
`Follow instruction for you project Updating to Capacitor 6.
#### Migrate from 3.2.x to 3.3.x
Install version 3.3.x:
`sh
npm i --save @codetrix-studio/capacitor-google-auth^3.3
`Follow instruction for you project Updating from Capacitor 4 to Capacitor 5.
#### Migrate from 3.2.1 to 3.2.2
for
Android in file MainActivity.onCreate`diff
- this.init(savedInstanceState, new ArrayList>() {{
- add(GoogleAuth.class);
- }});
+ this.registerPlugin(GoogleAuth.class);
`#### Migrate from 3.1.x to 3.2.x
Install version 3.2.x:
`sh
npm i --save @codetrix-studio/capacitor-google-auth^3.2
`Follow instruction for you project Updating from Capacitor 3 to Capacitor 4.
#### Migrate from 3.0.2 to 3.1.0
`diff
- GoogleAuth.init()
+ GoogleAuth.initialize()
`#### Migrate from 2 to 3
Install version 3.x.x:
`sh
npm i --save @codetrix-studio/capacitor-google-auth^3.0
`After migrate to Capcitor 3 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'- Plugins.GoogleAuth.signIn();
+ GoogleAuth.init()
+ GoogleAuth.signIn()
`#### Migrate from 1 to 2
Install version 2.x.x:
`sh
npm i --save @codetrix-studio/capacitor-google-auth@2
``for capacitor 2.x.x use instruction