I’m at my wits end. I’m trying to implement PKCE flow in my react native application. Basically I want a user to authenticate, and then I want a pair of authorization token and refresh token for my custom API, so they wouldn’t ever need to authenticate again (unless physically choosing to log out of course) (my plan is to automatically retrieve new access and refresh tokens on their behalf, and I’m using the rotating refresh token scheme, if I can ever get this PKCE to work…).
So here is my problem.
My call to the /authorize endpoint works like a charm everytime, i.e. an example response is:
{“type”:“success”,“params”:{“code”:“ne34Q0cRDTykpn-J”,“state”:“jim7azbo25f”},“errorCode”:null,“url”:“exp://127.0.0.1:19000/–/expo-auth-session?code=ne34Q0cRDTykpn-J&state=jim7azbo25f”}
Should be a walk in the park to call that /oauth/token endpoint to get the access_token and refresh_token. Nope. Nothing works. Trying for a few hours now, every combination I can think of I’m getting a response like this:
{“error”:“access_denied”,“error_description”:“Unauthorized”}
The options for my fetch call look like this:
const options = {
method: 'POST',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
form: {
grant_type: 'authorization_code',
client_id: AUTH0_CLIENT_ID,
code_verifier: verifier,
code: code,
redirect_uri: base64URLEncode(redirectUrl),
},
};
Where the code comes off the code param in the response, the verifier is the same const variable as used in the /authorize endpoint, and the client ID is the client ID for my native app.
My first thought is that the ‘url encoding’ for the redirect_url field in the POST call is wrong. I’m using expo so my dev redirect URL is
By the official docs, the ‘URL encoding’ function is:
function base64URLEncode(str) {
return str.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}
so my URL becomes ‘https:_auth.expo.io@fullstackchris_xn–rst-fya’
Another concern is the client ID for both calls - this should be the same client ID and the one I’ve defined as my ‘Native Application’ in the auth0 dashboard, correct?
By the way, this GET and POST pair of calls can both (and should) be done on the client-side, correct?
Anyone have any common gotchya’s with this PKCE flow? I also have a feeling that I’ve forgotten some setting in the dashboard, but I’ve followed this tutorial 3 times over now:
https://auth0.com/docs/flows/call-your-api-using-the-authorization-code-flow-with-pkce