Open RylanSchaeffer opened 2 years ago
This is the problematic code:
exports.extractAccessToken = function (session, getAccessToken) {
if (!session) {
return null;
}
if (getAccessToken) {
return getAccessToken(session);
}
if (typeof session !== 'string') {
throw Error("Access token can not be extracted. Either whole 'session' must be a string (access token only) or 'getAccessToken' function should be provided");
}
return session;
};
My session
is:
Object { access_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NTUyNDkwMjYsImV4cCI6MTY1NTMzNTQyNiwianRpIjoiODkzYzUwZTItMjkwOC00MDMxLWJhMGQtOWMzZWIxMzdlMDNkIiwiaWQiOjUsInJscyI6ImV4cGxvcmVyIiwicmZfZXhwIjoxNjU3ODQxMDI2fQ.wEJE9qZZZdwHuWnIm2y5XblkNiWeMmD1zmaIfkijlic" }
but getAccessToken
is undefined, so the error is thrown.
I just checked the following: If I use
let [isSignedIn, session] = useAuth();
session
is indeed:
Object { access_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NTUyNDkwMjYsImV4cCI6MTY1NTMzNTQyNiwianRpIjoiODkzYzUwZTItMjkwOC00MDMxLWJhMGQtOWMzZWIxMzdlMDNkIiwiaWQiOjUsInJscyI6ImV4cGxvcmVyIiwicmZfZXhwIjoxNjU3ODQxMDI2fQ.wEJE9qZZZdwHuWnIm2y5XblkNiWeMmD1zmaIfkijlic" }
The problem seems to be that this line leaves getAccessToken
undefined:
var _c = _a.storageKey, storageKey = _c === void 0 ? 'REACT_TOKEN_AUTH_KEY' : _c, onUpdateToken = _a.onUpdateToken, onHydratation = _a.onHydratation, _d = _a.storage, storage = _d === void 0 ? defaultStore_1.createDefaultStore((_b = {}, _b[storageKey] = localStorage.getItem(storageKey), _b)) : _d, _e = _a.fetchFunction, fetchFunction = _e === void 0 ? fetch : _e, getAccessToken = _a.getAccessToken, _f = _a.expirationThresholdMillisec, expirationThresholdMillisec = _f === void 0 ? 5000 : _f;
This is my current workaround:
fetch(
`${process.env.REACT_APP_URL_BACKEND}/create-checkout-session`,
{
method: 'post',
headers: {
'Authorization': `Bearer ${session.access_token}`
},
body: JSON.stringify(opts),
}
)
.then(r => r.json())
.then(response => {
console.log(response)
})
This is my current workaround:
fetch( `${process.env.REACT_APP_URL_BACKEND}/create-checkout-session`, { method: 'post', headers: { 'Authorization': `Bearer ${session.access_token}` }, body: JSON.stringify(opts), } ) .then(r => r.json()) .then(response => { console.log(response) })
When i attempt this workaround i'm getting Uncaught TypeError: Cannot read properties of null (reading 'access_token')
. Have you had that happen before?
Hi! I'm a novice to React and JS, and I'm trying to get around the following error:
Uncaught (in promise) Error: Access token can not be extracted. Either whole 'session' must be a string (access token only) or 'getAccessToken' function should be provided
.My error occurs in the following code:
Can someone explain to me why this occurs and how to fix it?