Oauth2 in Wix Corvid

Hey i am running into some problems with oauth2 :
First i used the native module of fetch in ‘wix-fetch’ to get my authorization token in oauth2

My frontend page which gets code as a query , and console logged it , the code is recorded perfectly
Frontend code:

import {getAuthToken , getUser, stringify} from 'backend/sever';
import wixLocation from 'wix-location';
import { session } from 'wix-storage';
$w.onReady(function () {
 let code = wixLocation.query.code;
    console.log(code);
 
 let authc = getAuthToken(code).then(auth => {
         console.log(auth)
     })
      getUser(authc).then(userobj => {
             console.log(userobj)
             session.setItem('user', userobj)
         })

});

the code for fetch goes like this:

 export function getAuthToken(code){
 fetch({URL}, qs.stringify({
 'client_id': CLIENT_ID,
 'client_secret': CLIENT_SECRET,
 'grant_type': 'authorization_code',
 'code': code,
 'redirect_uri': REDIRECT_URI, 
 'scope': 'identify'
      }), {
          headers: {
 'Content-Type': 'application/x-www-form-urlencoded'
          },
          method: 'POST'
      }).then(result => {

 let authcode = result.data.access_token;
 return authcode;
      });
  }

then it returned undefined
So i tried using a npm module i was familiar with : axios
This is the revised ‘server.jsw’ code in backend:

export function getAuthToken(code){
 axios.post({URL}, qs.stringify({
 'client_id': CLIENT_ID,
 'client_secret': CLIENT_SECRET,
 'grant_type': 'authorization_code',
 'code': code,
 'redirect_uri': REDIRECT_URI, 
 'scope': 'identify'
      }), {
          headers: {
 'Content-Type': 'application/x-www-form-urlencoded'
          }
      }).then(result => {

 let authcode = result.data.access_token;
 return authcode;
      });
}

I am really familiar with axios which i must say, i have used to make oauth2 requests and it worked perfectly in my local node js file i dont understand whats wrong
i keep getting “undefined” as the object returned

1 Like