1

I'm writing an app that needs to authenticate externally with a service. To do this, I'm using express to listen to the redirect and get the data I need. However, I would like to return that data to the main scope. Here's what I have so far:

const authorize = () => {
    app.get('/', (req, res) => {
        res.status(200).send('<script>window.close()</script>')
        return req.query.code;
    });

    app.listen(3000, () => {
        console.log('listening');
    });
}

const code = authorize();

req.query.code is the data I need, and I would like to return it from authorize(). Instead I get undefined. What is the best practice to accomplish what I want?

Rodrigo Leite
  • 596
  • 5
  • 22

1 Answers1

0

Fixed it by returning a Promise.

const authorize = () => {

    return new Promise((resolve, reject) => {
        app.get('/', (req, res) => {
            res.status(200).send('<script>window.close()</script>')
            resolve(req.query.code);
        });

        app.listen(3000, () => {
            console.log('listening');
        });
    })
}
Rodrigo Leite
  • 596
  • 5
  • 22