Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidWells committed Jul 16, 2018
1 parent 308d45a commit b0fddc3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 45 deletions.
31 changes: 3 additions & 28 deletions functions/auth-callback.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import querystring from 'querystring'
import requestWrapper from './utils/request'
import getUserData from './utils/getUserData'
import oauth2, { config } from './utils/oauth'

/* Function to handle intercom auth callback */
Expand All @@ -20,9 +19,10 @@ exports.handler = (event, context, callback) => {
console.log('accessToken', token)
return token
})
// Get more info about intercom user
.then(getUserData)
// Do stuff with user data & token
.then((result) => {
// Do stuff with token
console.log('auth token', result.token)
// Do stuff with user data
console.log('user data', result.data)
Expand All @@ -45,28 +45,3 @@ exports.handler = (event, context, callback) => {
})
})
}

function getUserData(token) {
const params = {
client_id: config.clientId,
client_secret: config.clientSecret,
app_id: config.appId
}

const postData = querystring.stringify(params)

const requestOptions = {
url: `${config.profilePath}?${postData}`,
json: true,
auth: {
user: token.token.token,
pass: '',
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
}
}

return requestWrapper(requestOptions, token)
}
43 changes: 43 additions & 0 deletions functions/utils/getUserData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import request from 'request'
import querystring from 'querystring'
import { config } from './oauth'

/* Call into https://app.intercom.io/me and return user data */
export default function getUserData(token) {
const postData = querystring.stringify({
client_id: config.clientId,
client_secret: config.clientSecret,
app_id: config.appId
})

const requestOptions = {
url: `${config.profilePath}?${postData}`,
json: true,
auth: {
user: token.token.token,
pass: '',
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
}
}

return requestWrapper(requestOptions, token)
}

/* promisify request call */
function requestWrapper(requestOptions, token) {
return new Promise((resolve, reject) => {
request(requestOptions, (err, response, body) => {
if (err) {
return reject(err)
}
// return data
return resolve({
token: token,
data: body,
})
})
})
}
17 changes: 0 additions & 17 deletions functions/utils/request.js

This file was deleted.

0 comments on commit b0fddc3

Please sign in to comment.