Skip to content

Commit a8b706b

Browse files
committed
fix(handle-callback): try to save firestore doc with application info even on token refresh
1 parent f5277ae commit a8b706b

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

lib/methods/handle-callback.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const handleCallback = client => {
77

88
return async (storeId, reqBody) => {
99
return new Promise((resolve, reject) => {
10-
let isNew, authenticationId, sql, values, firestoreDoc
10+
let isNew, authenticationId, sql, values
1111

1212
// first validation of function params
1313
if (typeof storeId !== 'number' || isNaN(storeId) || storeId <= 0) {
@@ -21,6 +21,21 @@ const handleCallback = client => {
2121
// application and authentication objects from body if any
2222
const { application, authentication } = reqBody
2323

24+
// preset Firestore document common values
25+
const firestoreDoc = collRef
26+
? {
27+
store_id: storeId,
28+
authentication_id: authenticationId
29+
}
30+
: null
31+
if (collRef && application) {
32+
Object.assign(application, {
33+
application_id: application._id,
34+
application_app_id: application.app_id,
35+
application_title: application.title
36+
})
37+
}
38+
2439
if (application && reqBody.store_id === storeId) {
2540
// new app installed
2641
isNew = true
@@ -29,16 +44,11 @@ const handleCallback = client => {
2944
// insert application with respective authentication data
3045
if (collRef) {
3146
const { Timestamp } = require('firebase-admin').firestore
32-
firestoreDoc = {
33-
application_id: application._id,
34-
application_app_id: application.app_id,
35-
application_title: application.title,
36-
authentication_id: authenticationId,
47+
Object.assign(application, {
3748
authentication_permissions: JSON.stringify(authentication.permissions),
38-
store_id: storeId,
3949
created_at: Timestamp.now(),
4050
updated_at: new Timestamp(1500000000, 0) // Jul 13 2017
41-
}
51+
})
4252
} else {
4353
values = [
4454
application._id,
@@ -65,13 +75,11 @@ const handleCallback = client => {
6575
// authentication flux callback
6676
// should update access token for current authentication
6777
if (collRef) {
68-
firestoreDoc = {
78+
Object.assign(application, {
6979
access_token: reqBody.access_token,
7080
expires: reqBody.expires,
71-
updated_at: require('firebase-admin').firestore.Timestamp.now(),
72-
authentication_id: authenticationId,
73-
store_id: storeId
74-
}
81+
updated_at: require('firebase-admin').firestore.Timestamp.now()
82+
})
7583
} else {
7684
values = [
7785
reqBody.access_token,

0 commit comments

Comments
 (0)