Skip to content

Commit

Permalink
fix(handle-callback): try to save firestore doc with application info…
Browse files Browse the repository at this point in the history
… even on token refresh
  • Loading branch information
leomp12 committed Jun 17, 2021
1 parent f5277ae commit a8b706b
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions lib/methods/handle-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const handleCallback = client => {

return async (storeId, reqBody) => {
return new Promise((resolve, reject) => {
let isNew, authenticationId, sql, values, firestoreDoc
let isNew, authenticationId, sql, values

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

// preset Firestore document common values
const firestoreDoc = collRef
? {
store_id: storeId,
authentication_id: authenticationId
}
: null
if (collRef && application) {
Object.assign(application, {
application_id: application._id,
application_app_id: application.app_id,
application_title: application.title
})
}

if (application && reqBody.store_id === storeId) {
// new app installed
isNew = true
Expand All @@ -29,16 +44,11 @@ const handleCallback = client => {
// insert application with respective authentication data
if (collRef) {
const { Timestamp } = require('firebase-admin').firestore
firestoreDoc = {
application_id: application._id,
application_app_id: application.app_id,
application_title: application.title,
authentication_id: authenticationId,
Object.assign(application, {
authentication_permissions: JSON.stringify(authentication.permissions),
store_id: storeId,
created_at: Timestamp.now(),
updated_at: new Timestamp(1500000000, 0) // Jul 13 2017
}
})
} else {
values = [
application._id,
Expand All @@ -65,13 +75,11 @@ const handleCallback = client => {
// authentication flux callback
// should update access token for current authentication
if (collRef) {
firestoreDoc = {
Object.assign(application, {
access_token: reqBody.access_token,
expires: reqBody.expires,
updated_at: require('firebase-admin').firestore.Timestamp.now(),
authentication_id: authenticationId,
store_id: storeId
}
updated_at: require('firebase-admin').firestore.Timestamp.now()
})
} else {
values = [
reqBody.access_token,
Expand Down

0 comments on commit a8b706b

Please sign in to comment.