0

I'm making a system that uses authorization through an external API (VK). When the user clicks on "authorize via VK", they get a popup window where they can choose whether to grant permissions or cancel. Whatever they choose, the API just redirects them to my php script in the same popup window, and when that script is done, they are ending up with an empty popup window still open.

I need to do 2 things:

1) Close the popup window after the script is done.

2) Depending on what the function in the script returns, display the appropriate message for the user, not in that popup window, but in the initial window that initiated the popup (somewhere between the lines of the already existing text), after the popup has already closed.

Now, I don't know how to do this. There must me some javascript (preferrably jquery) that inserts a message to the initial window depending on the response obtained from the function that was called in a popup window that has already closed.

Here are some excerpts from the system:

http://example.com/vkcode?error=access_denied&error_reason=user_denied&error_description=User+denied+your+request&state=secret_state_code - this is the page the user gets redirected to (inside the popup) if they choose "cancel". And they keep staying on the blank page with that string in their address bar.

Here is some PHP code that handles the response from VK API:

public function vkAuthHandler() {
    if (isset($_GET['error'])) {
        if ($_GET['error_reason'] == 'user_denied' {
            return 'user_denied';
        }
        else return 'error';
    }
    else {
       // ... haven't written other logic yet, it's irrelevant anyway
   }
    return new Response();
}

Now, if I receive 'user_denied' response, I need to display a message telling the user that they refused the permissions. But not in that popup window where that function was called (it should already be closed by the time), but on the initial page, without reloading it.

COOLak
  • 379
  • 3
  • 15
  • 1
    Imagine if you will, that you are someone else. And they are reading your question without the knowledge of your application that you already have. Would it make sense to you? Would you have enough knowledge of the actual logic already developed to know what needed to be modified, with what you have provided? – Taplar Mar 08 '19 at 18:58
  • But I asked it as a general question. 1) How to close the popup window after a php script is done. 2) How to pass the response of that php script to a javascript that's on the initial page and not on the popup page. Totally makes sense to me. – COOLak Mar 08 '19 at 19:02
  • StackOverflow is not looking for general question, especially when it is about specific logic that you have already developed, that we have no idea about. General questions are appropriate for well known things, like existing public libraries or plugins perhaps. Not custom code that developers at large are unaware of the details about. You have to give us the details. Else we are limited to guessing. – Taplar Mar 08 '19 at 19:04
  • I added some excerpts to my question, but I have no idea how they help. – COOLak Mar 08 '19 at 19:08

1 Answers1

0

I solved it in a sophisticated way. Not going to accept this answer because maybe someone offers a simplier solution.

In PHP:

public function vkAuthHandler() {
    if (isset($_GET['error'])) {
        if ($_GET['error_reason'] == 'user_denied' {
            header('Set-cookie: vkresp=user_denied');
        }
        else header('Set-cookie: vkresp=error');
    }
    else {
       // ...
   }
    echo "<script>window.close();</script>"; //closing the window here
    return new Response();
}

In JavaScript (used jQuery and JS-Cookie), based on this solution:

var cookieRegistry = [];

function listenCookieChange(cookieName, callback) {
    setInterval(function() {
        if (cookieRegistry[cookieName] || Cookies.get(cookieName) != null) {
            if (Cookies.get(cookieName) != cookieRegistry[cookieName]) { 
                cookieRegistry[cookieName] = Cookies.get(cookieName); 
                return callback();
            } 
        } else {
            cookieRegistry[cookieName] = Cookies.get(cookieName);
        }
    }, 100);
}

listenCookieChange('vkresp', function() {
    if (Cookies.get('vkresp') == 'user_denied') {
        console.log('VK response is user_denied');
        $("#VKauth").append('<div style="color: red;">You denied authorization! Comments are blocked!</div>');
    }
    else if (Cookies.get('vkresp') == 'error') {
        console.log('VK response is user_denied');
        $("#VKauth").append('<div style="color: red;">Unknown authorization error. Try again.</div>');
    }
    Cookies.remove('vkresp');
});

$("#VKauth") is basically selecting an HTML element with the id VKauth on my page.

COOLak
  • 379
  • 3
  • 15