0

I would like to implement the TMDB authentication on my React App but I have some trouble after approving the request token.

TMDB authentication is done with 3 steps (more info here):

  • Request a token (done)
  • Get the token approved (this is the step I have issuses)
  • Request session id (I can do the easily)

My problem is that to approve the token I need to open a new window, log in to my tmdb account and approve the token. After that I need to say to my React client that the token is approved and request the session id.

How can I wait until the token is approved and then request session id?

Here is my code :

// This function called when I dispatch login action with Redux
async function login(username, password) {
    // Create a new request token
    const token = await getToken()

    // Get the user to authorize the request token
    getUserPermission(token.request_token)

    // --------------
    // How do I wait for the user to approve the token and close the window and then request the session id
    // --------------

    // Create a new session_id with the athorized request token
    const session = await getSession(token.request_token)
    user.session_id = session.session_id

    return user
}
Linda Paiste
  • 38,446
  • 6
  • 64
  • 102
kamal951
  • 167
  • 2
  • 15

1 Answers1

0

I have found the solution using promises:

In the following code I open the window and wait the window to close to resolve the promise.

function getUserPermission(token) {
    const urlUserLogin = 'https://www.themoviedb.org/authenticate/' + token

    var win = window.open(
        urlUserLogin,
        'Authentication' // <- This is what makes it open in a new window.
    );

    return new Promise(resolve => {
        var timer = setInterval(function () {
            if (win.closed) {
                clearInterval(timer);
                resolve(true)
            }
        }, 500);
    });
}

async function login() {
    // Create a new request token
    const token = await getToken()

    const user = {}
    user.request_token = token.request_token

    // Get Permissions
    await getUserPermission(token.request_token)

    // Create a new session_id with the athorized request token
    const session = await getSession(user.request_token)

    user.session_id = session.session_id

    const userDetails = await getUserDetail(session.session_id)

    user.userDetails = userDetails

    return user
}
kamal951
  • 167
  • 2
  • 15