I'm setting up oauth for a Slack application where I need to do the following
- Receive the Slack sign in redirect
- HTTPS POST to Slack getting an auth token
- Saving token in psql record
- Posting a message to Slack once
Problem: No matter how many times I try to format, step 4 is happening before step 3. I've tried just a bunch of .thens and now to this weird promise thing that I completely guessed on.
When I callout to the endpoint, my console.logs (redacted from snippet) do not show results from the query, resulting in an error returned from slack message code but a record has been created in the table.
{ .get(async(req, res) => { let callCode = req.query.code; let state = req.query.state; var auth = new Oauth(); //prep params to send to slack api var params = new URLSearchParams(); params.append('code', callCode); params.append('client_id', process.env.SLACK_CLIENT_ID); params.append('client_secret', process.env.SLACK_CLIENT_SECRET); /******************** * GETS ACCESS TOKEN * *********************/ axios.post('https://slack.com/api/oauth.v2.access', params) .then(function(response){ var responseJSON = response.data; auth.my_app_user_id = state; auth.source_app = 'slack'; auth.source_user_id = responseJSON.authed_user.id; auth.source_auth_token = responseJSON.access_token; // CAN CONFIRM THAT AUTH IS POPULATED return auth; }) .then(async function(response){ var newAuth = response; var auth = newAuth; /***************************** * INITIATE: GET ACCESS TOKEN * *****************************/ insertOauth(newAuth) .then((result) => { /***************************** * SEND SLACK MESSAGE * * THIS KEEPS HAPPENING * * BEFORE QUERY RESULT RETURNS* *****************************/ sendSlackIntroMessage(result); }) .catch(function(error){ console.log(error); }) }) .catch(function(error){ console.log(error); }); })} /********************** * INSERTS DATA INTO DB * **********************/ var insertOauth = function(newAuth){ return new Promise(async function(resolve, reject){ //make api call to create auth record try { const client = await pool.connect(); const result = await client.query(`INSERT INTO oauth_tokens (my_app_user_id, source_app, source_user_id, source_auth_token, created_date, last_modified_date) VALUES(${newAuth.my_app_user_id}, '${newAuth.source_app}', '${newAuth.source_user_id}', '${newAuth.source_auth_token}', NOW(), NOW())`); results = { 'results': (result) ? result.rows : null}; client.release(); resolve(results); } catch (err) { console.error(err.error); reject(err); } }); } /********************** * SENDS SLACK MESSAGE * **********************/ function sendSlackIntroMessage(auth){ var response = auth; axios.post('https://slack.com/api/chat.postMessage', { token: response.source_auth_token, channel: response.buoy_user_id, text: "You've successfully connected Slack to Buoy! Using message actions, you may now create tasks from Slack messages. \n\n Be sure to invite @Buoy to channels which you will create tasks from." }) .then(function(response){ if(response.data.ok == false){ return(res); } if(response.data.ok == true){ return(res); } }) .catch(function(error){ return(error); }) }
没有评论:
发表评论