Skip to content

Commit eaabf9d

Browse files
authored
refactor(web): supporting .ts and.js config, exported types (#114)
1 parent 4859f9c commit eaabf9d

File tree

9 files changed

+1861
-167
lines changed

9 files changed

+1861
-167
lines changed

README.md

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,32 @@ for capacitor 2.x.x use [instruction](https://github.com/CodetrixStudio/Capacito
3939

4040
### WEB
4141

42-
Add [`clientId`](https://developers.google.com/identity/sign-in/web/sign-in#specify_your_apps_client_id) meta tag to head.
43-
44-
```html
45-
<meta name="google-signin-client_id" content="{your client id here}" />
46-
```
47-
4842
Register plugin and manually initialize
4943

5044
```ts
5145
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
5246

5347
// use hook after platform dom ready
54-
GoogleAuth.init();
48+
GoogleAuth.initialize({
49+
client_id: 'CLIENT_ID.apps.googleusercontent.com',
50+
scopes: ['profile', 'email'],
51+
grantOfflineAccess: true,
52+
});
53+
```
54+
55+
or if need use meta tags
56+
57+
```html
58+
<meta name="google-signin-client_id" content="{your client id here}" />
59+
<meta name="google-signin-scope" content="profile email" />
5560
```
5661

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

5970
```ts
@@ -72,7 +83,7 @@ constructor() {
7283

7384
initializeApp() {
7485
this.platform.ready().then(() => {
75-
GoogleAuth.init()
86+
GoogleAuth.initialize()
7687
})
7788
}
7889
```
@@ -97,7 +108,7 @@ import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
97108
export default defineComponent({
98109
setup() {
99110
onMounted(() => {
100-
GoogleAuth.init();
111+
GoogleAuth.initialize();
101112
});
102113

103114
const logIn = async () => {
@@ -111,32 +122,37 @@ export default defineComponent({
111122
},
112123
});
113124
```
125+
114126
or see more [CapacitorGoogleAuth-Vue3-example](https://github.com/reslear/CapacitorGoogleAuth-Vue3-example)
115127

116128
### iOS
117129

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

120132
2. Add **identifier** `REVERSED_CLIENT_ID` as **URL schemes** to `Info.plist` from **iOS URL scheme**<br>
121-
(Xcode: App - Targets/App - Info - URL Types, click plus icon)
122-
123-
3. Set **Client ID** one of the ways:
124-
1. Set in `capacitor.config.json`
125-
- `iosClientId` - specific key for iOS
126-
- `clientId` - or common key for Android and iOS
127-
3. Download `GoogleService-Info.plist` file with `CLIENT_ID` and copy to **ios/App/App** necessarily through Xcode for indexing.
133+
(Xcode: App - Targets/App - Info - URL Types, click plus icon)
128134

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

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

131143
### Android
132144

133145
Set **Client ID** :
134146

135147
1. In `capacitor.config.json`
136-
- `androidClientId` - specific key for Android
137-
- `clientId` - or common key for Android and iOS
148+
149+
- `androidClientId` - specific key for Android
150+
- `clientId` - or common key for Android and iOS
138151

139152
2. or set inside your `strings.xml`
153+
154+
plugin first use `androidClientId` if not found use `clientId` if not found use value `server_client_id` from file `strings.xml`
155+
140156
```xml
141157
<resources>
142158
<string name="server_client_id">Your Web Client Key</string>
@@ -159,6 +175,12 @@ this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
159175

160176
## Configure
161177

178+
| Name | Type | Default | Description |
179+
| ------------------------ | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
180+
| 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"]` |
181+
| serverClientId | string | '' | This is used for offline access and serverside handling<br><br>example: `xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com` |
182+
| forceCodeForRefreshToken | boolean | false | Force user to select email address to regenerate AuthCode <br>used to get a valid refreshtoken (work on iOS and Android) |
183+
162184
Provide configuration in root `capacitor.config.json`
163185

164186
```json
@@ -173,10 +195,33 @@ Provide configuration in root `capacitor.config.json`
173195
}
174196
```
175197

176-
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)
198+
or in `capacitor.config.ts`
199+
200+
```ts
201+
/// <reference types="'@codetrix-studio/capacitor-google-auth'" />
202+
203+
const config: CapacitorConfig = {
204+
plugins: {
205+
GoogleAuth: {
206+
scopes: ['profile', 'email'],
207+
serverClientId: 'xxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com',
208+
forceCodeForRefreshToken: true,
209+
},
210+
},
211+
};
212+
213+
export default config;
214+
```
177215

178216
## Migration guide
179217

218+
#### Migrate from 3.0.2 to 3.1.0
219+
220+
```diff
221+
- GoogleAuth.init()
222+
+ GoogleAuth.initialize()
223+
```
224+
180225
#### Migrate from 2 to 3
181226

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

android/src/main/java/com/codetrixstudio/capacitor/GoogleAuth/GoogleAuth.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ public void load() {
5353
googleSignInClient = GoogleSignIn.getClient(this.getContext(), googleSignInOptions);
5454
}
5555

56-
@PluginMethod
57-
public void init(PluginCall call) {
58-
call.unimplemented();
59-
}
60-
6156
@PluginMethod()
6257
public void signIn(PluginCall call) {
6358
saveCall(call);
@@ -105,4 +100,10 @@ public void signOut(final PluginCall call) {
105100
googleSignInClient.signOut();
106101
call.resolve();
107102
}
103+
104+
@PluginMethod()
105+
public void initialize(final PluginCall call) {
106+
call.resolve();
107+
}
108+
108109
}

ios/Plugin/Plugin.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
CAP_PLUGIN_METHOD(signIn, CAPPluginReturnPromise);
88
CAP_PLUGIN_METHOD(refresh, CAPPluginReturnPromise);
99
CAP_PLUGIN_METHOD(signOut, CAPPluginReturnPromise);
10-
CAP_PLUGIN_METHOD(init, CAPPluginReturnPromise);
10+
CAP_PLUGIN_METHOD(initialize, CAPPluginReturnPromise);
1111
)

ios/Plugin/Plugin.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public class GoogleAuth: CAPPlugin {
3737
}
3838

3939
@objc
40-
func `init`(_ call: CAPPluginCall) {
41-
call.unimplemented("Not available on iOS")
40+
func initialize(_ call: CAPPluginCall) {
41+
call.resolve();
4242
}
4343

4444
@objc

0 commit comments

Comments
 (0)