how to delete github oauth accessToken when user logout?

754 Views Asked by At

I'm having trouble with deleting github accessToken when user try to logout from my react app. I was trying to follow the instructions in github delete accesstoken documents, but I get an 404 bad request.

I was trying to handle github logout by javascript in React App. Here's the code.

const handleGithubLogout = async() => {
        
        await axios.delete('https://api.github.com/applications/${mygithubClientId}/token',
            {data : {access_token:"${mygithubAccessToken}"}}
            ,
            {headers: {
                    Accept: "application/vnd.github.v3+json",
                }
        })
        .then((data) => {
            console.log('succceed')
        })
        
        setTimeout(() => purge(), 100)

    }

The thing is ,in github document, they described how to delete github accessToken by cURL and javascript.

curl \
  -X DELETE \
  -H "Accept: application/vnd.github.v3+json" \
  https://api.github.com/applications/Iv1.8a61f9b3a7aba766/token \
  -d '{"access_token":"e72e16c7e42f292c6912e7710c838347ae178b4a"}'

so I tried javascript code like below. but still gets the 404 response.

const octokit = new Octokit({
             auth: '${myGithubAccessToken}'
           })
          
          await octokit.request(`DELETE /https://api.github.com/applications/{myClientID}/token`, {
             client_id: myClientID,
             access_token: myGithubAccessToken
           }).then(() => {
               console.log('success')
           })

I wonder how to change cURL code to javascript code. I'm not sure I wrote the code correctly. and also there is the same question in stackoverflow but I still don't get it how it works.

1

There are 1 best solutions below

1
On

See the provided javascript example code in the documentation you linked to:

// Octokit.js
// https://github.com/octokit/core.js#readme
const octokit = new Octokit({
  auth: 'personal-access-token123'
})

await octokit.request('DELETE /applications/{client_id}/token', {
  client_id: 'Iv1.8a61f9b3a7aba766',
  access_token: 'e72e16c7e42f292c6912e7710c838347ae178b4a'
})

Compared to that there are multiple issues in your code snippet:

  • auth: '${myGithubAccessToken}' will assign the literal string '${myGithubAccessToken}' and not the value of myGithubAccessToken. Either use template strings with backticks or no quoting at all: auth: myGithubAccessToken
  • the request URL specified is a domain-relative URL containing another absolute URL, i.e. it will be resolved to https://api.github.com/https://api.github.com/applications/{myClientID}/token. Either remove the leading / or - as in the example code - leave out the protocol and host part
  • in the URL you specify {myClientID} as placeholder but only pass the client_id, so it won't be replaced properly. You have to use {client_id} instead