Google Auth plugin for capacitor.
#### 1. Install package
``bash
npm i --save capacitor-plugin-google-auth
#### 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 versionUsage
for capacitor 2.x.x use instruction$3
Add clientId meta tag to head.
`html
`scope meta tag to head.
`html
`Register plugin and manually initialize
`ts
import { GoogleAuth } from 'capacitor-plugin-google-auth';GoogleAuth.init()
`Use it
`ts
GoogleAuth.signIn()
`#### AngularFire2
`ts
async googleSignIn() {
let googleUser = await Plugins.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 'capacitor-plugin-google-auth'export default defineComponent({
setup() {
onMounted(() => {
GoogleAuth.init()
})
const logIn = async () => {
const response = await GoogleAuth.signIn()
console.log(response)
}
return {
logIn
}
}
})
`$3
Make sure you have GoogleService-Info.plist with CLIENT_IDAdd
REVERSED_CLIENT_ID as url scheme to Info.plist$3
Inside your strings.xml
`xml
Your Web Client Key
`Import package inside your
MainActivity
`java
import com.codetrixstudio.capacitor.GoogleAuth.GoogleAuth;
`Register plugin inside your
MainActivity.onCreate
`java
this.init(savedInstanceState, new ArrayList>() {{
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
}
}
}`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)
$3
#### Migrate from 2 to 3
After migrate to Capcitor 3 updating you projects, see diff:
##### WEB
`diff
- import "capacitor-plugin-google-auth";
- import { Plugins } from '@capacitor/core';
+ import { GoogleAuth } from 'capacitor-plugin-google-auth';- Plugins.GoogleAuth.signIn();
+ GoogleAuth.init()
+ GoogleAuth.signIn()
``---
Forked from CodetrixStudio/CapacitorGoogleAuth