@tapni/auth 0.0.62 → 0.0.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,86 @@
1
+ import { WebPlugin as c } from "@capacitor/core";
2
+ class g extends c {
3
+ constructor() {
4
+ super();
5
+ }
6
+ loadScript() {
7
+ if (typeof document > "u")
8
+ return;
9
+ const e = "gapi";
10
+ if (document == null ? void 0 : document.getElementById(e))
11
+ return;
12
+ const n = document.getElementsByTagName("head")[0], t = document.createElement("script");
13
+ t.type = "text/javascript", t.defer = !0, t.async = !0, t.id = e, t.onload = this.platformJsLoaded.bind(this), t.src = "https://apis.google.com/js/platform.js", n.appendChild(t);
14
+ }
15
+ initialize(e = {
16
+ clientId: "",
17
+ scopes: [],
18
+ grantOfflineAccess: !1
19
+ }) {
20
+ var s, n;
21
+ if (typeof window > "u")
22
+ return;
23
+ const t = (s = document.getElementsByName("google-signin-client_id")[0]) === null || s === void 0 ? void 0 : s.content, i = e.clientId || t || "";
24
+ i || console.warn("GoogleAuthPlugin - clientId is empty"), this.options = {
25
+ clientId: i,
26
+ grantOfflineAccess: (n = e.grantOfflineAccess) !== null && n !== void 0 ? n : !1,
27
+ scopes: e.scopes || []
28
+ }, this.gapiLoaded = new Promise((o) => {
29
+ window.gapiResolve = o, this.loadScript();
30
+ }), this.addUserChangeListener();
31
+ }
32
+ platformJsLoaded() {
33
+ gapi.load("auth2", () => {
34
+ const e = {
35
+ client_id: this.options.clientId,
36
+ plugin_name: "CodetrixStudioCapacitorGoogleAuth"
37
+ };
38
+ this.options.scopes.length && (e.scope = this.options.scopes.join(" ")), gapi.auth2.init(e), window.gapiResolve();
39
+ });
40
+ }
41
+ async signIn() {
42
+ return new Promise(async (e, s) => {
43
+ var n;
44
+ try {
45
+ let t;
46
+ const i = (n = this.options.grantOfflineAccess) !== null && n !== void 0 ? n : !1;
47
+ i ? t = (await gapi.auth2.getAuthInstance().grantOfflineAccess()).code : await gapi.auth2.getAuthInstance().signIn();
48
+ const o = gapi.auth2.getAuthInstance().currentUser.get();
49
+ i && await o.reloadAuthResponse();
50
+ const a = this.getUserFrom(o);
51
+ a.serverAuthCode = t, e(a);
52
+ } catch (t) {
53
+ s(t);
54
+ }
55
+ });
56
+ }
57
+ async refresh() {
58
+ const e = await gapi.auth2.getAuthInstance().currentUser.get().reloadAuthResponse();
59
+ return {
60
+ accessToken: e.access_token,
61
+ idToken: e.id_token,
62
+ refreshToken: ""
63
+ };
64
+ }
65
+ async signOut() {
66
+ return gapi.auth2.getAuthInstance().signOut();
67
+ }
68
+ async addUserChangeListener() {
69
+ await this.gapiLoaded, gapi.auth2.getAuthInstance().currentUser.listen((e) => {
70
+ this.notifyListeners("userChange", e.isSignedIn() ? this.getUserFrom(e) : null);
71
+ });
72
+ }
73
+ getUserFrom(e) {
74
+ const s = {}, n = e.getBasicProfile();
75
+ s.email = n.getEmail(), s.familyName = n.getFamilyName(), s.givenName = n.getGivenName(), s.id = n.getId(), s.imageUrl = n.getImageUrl(), s.name = n.getName();
76
+ const t = e.getAuthResponse(!0);
77
+ return s.authentication = {
78
+ accessToken: t.access_token,
79
+ idToken: t.id_token,
80
+ refreshToken: ""
81
+ }, s;
82
+ }
83
+ }
84
+ export {
85
+ g as GoogleAuthWeb
86
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tapni/auth",
3
- "version": "0.0.62",
3
+ "version": "0.0.63",
4
4
  "type": "module",
5
5
  "main": "./dist/TapniAuth.umd.js",
6
6
  "module": "./dist/TapniAuth.es.js",