Web module returns undefined, why?

Hi,
As a part of the google OAuth process, I’m trying to exchange an authorization code for a refresh token.
When I try the code only in the frontend (in the last example here), it works great, but when i divide it to front and back, the function on the backend returns undefined.
Here’s the code for the frontend part:

import wixLocation from 'wix-location';
import {tokenSwap} from 'backend/tokenswap.jsw';

$w.onReady(function () {
//creating a variable from the url query which has the authorization //code from google.
    const code = wixLocation.query.code;
    tokenSwap(code)
    .then((confirm) => {
        console.log(confirm);
    })
    
})

Here’s the code for the backend:

import {fetch} from 'wix-fetch';

export function tokenSwap(authCode) {
//swapping the token using the authcode from the frontend
fetch("https ://oauth2.googleapis .com/token", {
        "method": "POST",
        "headers": {
        "Content-Type": "application/x-www-form-urlencoded"
    },
    "body": "code=" + authCode + "&grant_type=authorization_code&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&client_secret=xxxxxxxxxxxxxxxxxxxxxxxxxx&redirect_uri=https ://example.wixsite. com/example/success"
    })
//returning the response to the frontend
    .then((httpResponse) => {
        httpResponse.json()
        .then((result) => {
             return result.refresh_token;
        })

    })
}

Here the code that worked perfectly using only the frontend:

import wixLocation from 'wix-location';
import {fetch} from 'wix-fetch';
import wixData from 'wix-data';

$w.onReady(function () {
    const code = wixLocation.query.code;
    const state = wixLocation.query.state;
    fetch("https ://oauth2.googleapis. com/token", {
        "method": "POST",
        "headers": {
        "Content-Type": "application/x-www-form-urlencoded"
    },
    "body": "code=" + code + "&grant_type=authorization_code&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxx&client_secret=xxxxxxxxxxxxx&redirect_uri=https ://example.wixsite. com/example/success"
    })
    .then((httpResponse) => {
        httpResponse.json()
        .then ((result) => {
            console.log(result.refresh_token);
        })

    })
})

Why is this happening?
Somebody help?
Thanks!

Hey, it looks like the issue is in your backend function. You aren’t returning anything to the FE.

I would try something like this:
swap this line

export function tokenSwap(authCode)

with

export async function tokenSwap(authCode)

then for the fetch put something like

let results = await fetch ....

then before your last closing }

return results;

You can also test the backend code before hooking it up to the front by providing it whatever data it needs to run so you can confirm it’s working before hooking it up to the FE