Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use body instead of url for code exchange post #343

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/util/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,31 @@ export const exchangeCodeForToken = async (
codeVerifier,
code,
) => {
// eslint-disable-next-line no-unused-vars
const postData = async (url, _data) => {
const postData = async (url, data) => {
const formData = new URLSearchParams(data); // Convert data to URL-encoded form data
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded', // Set the content type
},
body: formData.toString(), // Convert the form data to a string
});
const json = response.json();
const json = await response.json(); // Parse the response JSON
return json;
};

const requestTokenUrl = 'https://identity.pagerduty.com/oauth/token?'
+ 'grant_type=authorization_code&'
+ `code=${code}&`
+ `redirect_uri=${redirectURL}&`
+ `client_id=${clientId}&`
+ `client_secret=${clientSecret}&`
+ `code_verifier=${codeVerifier}`;
const data = await postData(requestTokenUrl, {});
const requestTokenUrl = 'https://identity.pagerduty.com/oauth/token';
const formData = {
grant_type: 'authorization_code',
code,
redirect_uri: redirectURL,
client_id: clientId,
client_secret: clientSecret,
code_verifier: codeVerifier,
};

const data = await postData(requestTokenUrl, formData);

if (data.access_token) {
return data.access_token;
}
Expand Down