0

At the moment I am working on an Electron app that is supplied with data via an API. The renderer calls a "backend function", which first gets the API key via Keytar and then executes the API call via axios.

The problem here is that Keytar always returns null/undefined, even if a similar function with the same functionality works without any problems, also because this point can only be reached if a valid API key is stored at all and this will also be queried by Keytar.

I am new to async/await-functions, maybe I didn't get something.

btw: Maybe the title doesn't fit too well, but I was a bit at a loss about this one.

(keytarService, username, baseUrl are globals)

Here is my code:

// Api-calling function

async function makeCall(method_type, url_path, data_array) {
    keytar.getPassword(keytarService, username).then((apiKey) => {
        if (apiKey == null || apiKey == undefined) {
            return false;
        }
        axios({
            method: method_type,
            url: baseUrl + url_path,
            headers: {
                'content-type': 'application/json',
                'X-AUTH-TOKEN': apiKey,
            },
            data: data_array,
        }).then(
            (response) => {
                return response.data;
            },
            (error) => {
                return false;
            }
        );
    });
}

//index_renderer.js

webContents.on('dom-ready', () => {
    apiMain
        .makeCall('GET', 'user/self')
        .then((data) => {
            console.log(data);
            document.getElementById('username_text').innerText =
                data.firstName + '' + data.lastName;
        })
        .catch((err) => console.log(err));
});

Similar function which is working:

async function isAuthenticated() {
    apiKey = await keytar.getPassword(keytarService, username);

    if (apiKey == null || apiKey == undefined) {
        return false;
    }

    axios({
        method: 'GET',
        url: baseUrl + '/api/isAuthenticated',
        headers: {
            'content-type': 'application/json',
            'X-AUTH-TOKEN': apiKey,
        },
        data: {},
    }).then(
        (response) => {
            console.log(response);
            if (!response.data.authenticated) {
                logout();
            }
            return response;
        },
        (error) => {
            console.log(error);
            logout();
            return error;
        }
    );
}

// call of the working function in main.js

if (authProcess.isAuthenticated()) {
        mainwin.loadFile('index.html');
    } else {
        mainwin.loadFile('login.html');
    }

Thanks in advance.

1 Answer 1

1

You are missing important returns in MakeCall().

Try:

function makeCall(method_type, url_path, data_array) {

    // return this promise to MakeCall
    return  keytar.getPassword(keytarService, username).then((apiKey) => {
        if (apiKey == null || apiKey == undefined) {
            return false;
        }
        
        // return this promise to keytar.getPassword then()     
       return  axios({
            method: method_type,
            url: baseUrl + url_path,
            headers: {
                'content-type': 'application/json',
                'X-AUTH-TOKEN': apiKey,
            },
            data: data_array,
        }).then(
            (response) => {
                return response.data;
            },
            (error) => {
                return false;
            }
        );
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

This was the solution, wow. So damn easy. I think Javascript is not that easy for a die-hard PHP developer. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.