@live-change/google-authentication-service 0.9.9 → 0.9.11

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.
package/account.js CHANGED
@@ -76,7 +76,7 @@ definition.event({
76
76
  validation: ['nonEmpty']
77
77
  }
78
78
  },
79
- async execute({ account }) {
79
+ async execute({ account, user }) {
80
80
  await Account.delete(account)
81
81
  }
82
82
  })
package/connect.js CHANGED
@@ -9,6 +9,7 @@ import { User, googleProperties, Account } from './account.js'
9
9
  import { getTokensWithCode, getUserInfo } from './googleClient.js'
10
10
 
11
11
  import { downloadData } from './downloadData.js'
12
+ import { OfflineAccess } from './offlineAccess.js'
12
13
 
13
14
  definition.trigger({
14
15
  name: "connectGoogle",
@@ -68,10 +69,23 @@ definition.trigger({
68
69
  validation: ['nonEmpty']
69
70
  }
70
71
  },
71
- async execute({ account }, { client, service }, emit) {
72
+ async execute({ account }, { client, service, triggerService }, emit) {
72
73
  const accountData = await Account.get(account)
73
74
  if(!accountData) throw 'notFound'
74
75
  const { user } = accountData
76
+
77
+ console.log("GET OFFLINE ACCESS!", account, user)
78
+ const offlineAccess = await OfflineAccess.indexObjectGet('byUserAccount', [user, account])
79
+ console.log("OFFLINE ACCESS", offlineAccess)
80
+ if(offlineAccess) {
81
+ await triggerService({
82
+ type: 'googleAuthentication_resetOfflineAccess',
83
+ service: definition.name
84
+ }, {
85
+ offlineAccess: offlineAccess.to ?? offlineAccess.id
86
+ })
87
+ }
88
+
75
89
  emit({
76
90
  type: 'accountDisconnected',
77
91
  account, user
package/googleClient.js CHANGED
@@ -35,7 +35,9 @@ export async function getTokensWithCode(code, redirectUri) {
35
35
  } catch(error) {
36
36
  console.error("OAUTH ERROR", error?.stack || error?.message || JSON.stringify(error))
37
37
  console.error("OAUTH ERROR RESPONSE", error?.response?.data)
38
- throw error?.response?.data ? new Error(error?.response?.data) : error
38
+ throw error?.response?.data
39
+ ? new Error(error?.response?.data?.error || error?.response?.data)
40
+ : new Error(error.message || error.toString())
39
41
  }
40
42
  }
41
43
 
package/index.js CHANGED
@@ -6,7 +6,6 @@ import "./account.js"
6
6
  import "./connect.js"
7
7
  import "./sign.js"
8
8
  import "./offlineAccess.js"
9
-
10
-
9
+ import "./userDeleted.js"
11
10
 
12
11
  export default definition
package/offlineAccess.js CHANGED
@@ -3,6 +3,7 @@ const app = App.app()
3
3
  import definition from './definition.js'
4
4
  import Debug from 'debug'
5
5
  import { getTokensWithCode, getUserInfo } from './googleClient.js'
6
+ import { Account } from './account.js'
6
7
  const debug = Debug('services:googleAuthentication')
7
8
 
8
9
  export const OfflineAccess = definition.model({
@@ -34,12 +35,22 @@ export const OfflineAccess = definition.model({
34
35
  },
35
36
  lastRefresh: {
36
37
  type: Date
38
+ },
39
+ account: {
40
+ type: String,
41
+ validation: ['nonEmpty']
37
42
  }
38
43
  },
39
44
  indexes: {
40
45
  byUserAndScope: {
41
46
  multi: true,
42
47
  property: ['user', 'scopes']
48
+ },
49
+ byUser: {
50
+ property: 'user'
51
+ },
52
+ byUserAccount: {
53
+ property: ['user', 'account']
43
54
  }
44
55
  }
45
56
  })
@@ -130,29 +141,47 @@ definition.action({
130
141
  if(!user) throw 'notAuthorized'
131
142
  const tokens = await getTokensWithCode(code, redirectUri)
132
143
  console.log("TOKENS", tokens)
144
+ if(!tokens.refresh_token) throw new Error("No refresh token")
133
145
  const scopes = tokens.scope.split(' ')
134
146
  if(tokens.token_type !== 'Bearer') throw new Error("Invalid token type "+tokens.token_type)
147
+ const googleUser = await getUserInfo(tokens.access_token)
148
+ console.log("GOOGLE USER", googleUser)
149
+ const account = googleUser.sub
150
+ const accountData = await Account.get(account)
151
+ console.log("ACCOUNT DATA", accountData, 'CURRENT USER', user)
152
+ if(accountData) {
153
+ if(accountData.user !== user) throw 'connectedToAnotherUser'
154
+ } else {
155
+ await service.trigger({ type: 'connectGoogle' }, {
156
+ user, account, data: googleUser,
157
+ })
158
+ console.log("CONNECTED")
159
+ }
135
160
  await OfflineAccess.update(user, {
136
161
  id: user,
137
162
  user,
138
163
  scopes,
164
+ account,
139
165
  refreshToken: tokens.refresh_token,
140
166
  accessToken: tokens.access_token,
141
167
  accessTokenExpire: tokens.expires_in ? new Date(Date.now() + tokens.expires_in * 1000) : null,
142
168
  lastRefresh: new Date()
143
169
  })
170
+ console.log("OFFLINE ACCESS SAVED")
144
171
  await service.triggerService({ type: 'googleAuthentication_setOfflineAccess', service: definition.name }, {
145
- user, scopes,
172
+ user, scopes, account,
146
173
  accessToken: tokens.access_token,
147
174
  accessTokenExpire: tokens.expires_in ? new Date(Date.now() + tokens.expires_in * 1000) : null,
148
175
  refreshToken: tokens.refresh_token,
149
176
  })
177
+ console.log("TRIGGER other services")
150
178
  await service.trigger({ type: 'googleOfflineAccessGained' }, {
151
179
  user, scopes,
152
180
  accessToken: tokens.access_token,
153
181
  accessTokenExpire: tokens.expires_in ? new Date(Date.now() + tokens.expires_in * 1000) : null,
154
182
  refreshToken: tokens.refresh_token,
155
183
  })
184
+ console.log("TRIGGER FINISHED")
156
185
  return {
157
186
  action: 'addOfflineAccessToken'
158
187
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/google-authentication-service",
3
- "version": "0.9.9",
3
+ "version": "0.9.11",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,10 +21,10 @@
21
21
  "url": "https://www.viamage.com/"
22
22
  },
23
23
  "dependencies": {
24
- "@live-change/framework": "^0.9.9",
25
- "@live-change/relations-plugin": "^0.9.9",
24
+ "@live-change/framework": "^0.9.11",
25
+ "@live-change/relations-plugin": "^0.9.11",
26
26
  "google-auth-library": "9.0.0"
27
27
  },
28
- "gitHead": "150ef8008399a3b6a160985a4a8d4e91255c0e99",
28
+ "gitHead": "a1bd367ae175f0937d999577f1b10bf10e7df0fb",
29
29
  "type": "module"
30
30
  }
package/userDeleted.js ADDED
@@ -0,0 +1,36 @@
1
+ import definition from './definition.js'
2
+ import { Account, User } from './account.js'
3
+ import { OfflineAccess } from './offlineAccess.js'
4
+
5
+ definition.event({
6
+ name: "userDeleted",
7
+ properties: {
8
+ user: {
9
+ type: User,
10
+ validation: ['nonEmpty']
11
+ }
12
+ },
13
+ async execute({ user }) {
14
+ const accounts = await Account.indexRangeGet('byUser', user)
15
+ const offlineAccesses = await OfflineAccess.indexRangeGet('byUser', user)
16
+ await Promise.all([
17
+ Promise.all(accounts.map(account => Account.delete(account.to ?? account.id))),
18
+ Promise.all(offlineAccesses.map(offlineAccess => OfflineAccess.delete(offlineAccess.to ?? offlineAccess.id)))
19
+ ])
20
+ }
21
+ })
22
+
23
+ definition.trigger({
24
+ name: 'userDeleted',
25
+ properties: {
26
+ user: {
27
+ type: User
28
+ }
29
+ },
30
+ async execute({ user }, { service }, emit) {
31
+ emit([{
32
+ type: "userDeleted",
33
+ user
34
+ }])
35
+ }
36
+ })