Facebook Logout Confusion - javascript

I have a strange problem, I have a logout link that displays only if the user is logged in. I have this code inside the the onclick event of this link (using a function):
FB.logout();
window.location = 'http://www.google.com';
If I click on this once, nothing happens, I know that the function gets executed because I've tested this with an alert. However, if I click on it a second time, the page reloads itself and the user is indeed logged out. The page is never directed to google.com, so the function never gets as far as the window.location part.
I have the window.location because I thought that if I could refresh the page using window.location.refresh once the logout is complete that it would successfully log the user out. However, if anybody has any other techniques on how to logout the user out of facebook, I would love to hear them! I've attempted to manually delete the cookie, but that didn't work, the cookie still existed for some reason. I've also tried this:
FB.logout(function(response) {
window.location = 'http://www.google.com';
});
I know a callback like this is possible because of the documentation:
http://developers.facebook.com/docs/reference/javascript/FB.logout
Has anybody else had this issue before? Any advice would help thanks!
UPDATE: After some debugging I've found out that if I click the link once. Then manually refresh the page it logs the user out.

From your description it looks to me that logout is working properly, but page refresh doesn't.
Try something like this maybe:
FB.logout(function(response) {
window.location.reload(true);
});

You might try putting an alert in the callback function to see if FB.logout is really completely successfully.
I was originally putting FB.logout directly in the onclick event of an anchor link and while that worked in FF and Chrome it did not work in IE or in my Android browser.
FF and Chrome will execute the FB.logout call quickly enough for this to work properly but that IE and mobile browsers (because of network speed in addition to differences in the JavaScript engine) will not complete the call successfully before the browser loads whatever page you're redirecting to.
I think I am trying to do the exact same thing you are doing and this code worked for me:
function mysignout(url)
{
FB.logout(function()
{
top.location.href = 'url';
});
}
Surprisingly, it take 2+ seconds for FB.logout to completely successfully in most environments. There is obviously some kind of ajax call involved to revoke authentication on the server, not just destroying the local cookie.

Related

window.onload not triggered when user navigates but does when refreshing the page

I have a Chrome extension with a content script that must be injected when the url matches the rule. the thing is, if I refresh the page, it's working, but if I navigate to the url (it does match the pattern) then the event is not triggered.
Any idea why?
Edit: Take into account that it's based on a web app and I've tried using the webNavigation event and still doesn't work.
Edit: Ok, so a working solution (in Chrome at least) is to use the onHistoryStateUpdated event handler.
Possibly due to the cache saved in your Browser. If you hard refresh it, then it will run the whole script again.
You can use the following code:
$(window).bind("load", function() {
// code here
});
You can also try this function:
window.onpageshow()

JavaScript page refresh issue

Is there any way to handle page refresh in JavaScript ? After browser close button is clicked my application should be logged out and after clicking on browser back button application should be logged out also.
Try this snippet by clicking run and reload or backwards in your browser.
window.onbeforeunload = function(e) {
//call your function
return "Attention Reload"
};
Have a look at this eventHandler
Try This Answer
This answer will help you out
Also this only shows how to access the back button functionality in cross browser, However in the scope of those functions,
You can destroy your token or whatever to logout or you can directly use window.location.href, description of the same is given in Window.location.href
Hope that helps, Also if this does not solve specify more details in question that how are you logging out,
Thanks & Regards
Shohil Sethia

IE11 alert after navigation begins cancels the navigation

We have some Javascript that is used for navigation when you click a tab in which the window.location is set to another URL with query string parameters. This URL basically:
checks if the user is logged in
If they are not, redirect them to an error page to login again
If they are, logs them out and continues to the next step
redirects them to another url of another system which then logs them in to that system
If the next system is taking some time completing the request, the browser is sitting there with the loading icon, and the user may get impatient and click the tab again. If they do, step 1 is done a second time, and now that they are logged out from the initial processing of step 1.2, they fail the check and end up following the path of 1.1.
The workaround we put in place is pretty simple. We put the window.location into an IF statement that checks if a person has already clicked on a tab and navigation has already been kicked off. If not, then it does the window.location setting. If so, then we call "alert('Please be patient');". This seems to work fine for every browser except for IE11, and even then, only when F12 dev console is not open. If you have it open, it works fine.
But if you don't, for some reason the alert popup ends up cancelling the navigation, and the page never goes anywhere. Almost the same as if you pressed escape, or clicked the "Stop" button. There's also sometimes this weird scenario where the next page loads, but it thinks that the alert window is still open, but it's not. I can see the extra task in task manager, but it's not visible.
Has anybody heard or seen anything like this? Please note, I know there are other ways of handling this situation other than using an alert. I'm not looking for suggestions about how to accomplish a message in a different manner. I'm more interested in finding out if there's any decent documentation that could explain to me why an alert after the page has started to unload may be behaving this way in IE11, and only when F12 isn't open.
EDIT Adding code Example
var navigatingTo;
function doNavigation(destination){
if (navigatingTo && navigatingTo != ""){
alert(navigatingTo);
} else {
navigatingTo = "Please be patient\nNavigating to " + destination;
window.location = "/somepage.aspx?d=" + destination;
}
}

Cross Domain Pop Ups

My flow is as follows: the user clicks sign in on site 1. a pop up is opened from site 2 asking him to login using twitter. he then logs in - using oAuth, so the page changes. After a successful login the pop up should close and the code on site 1 should receive a notification.
What didn't work:
WebIntents - well, the examples on their site didn't even work, so I didn't try it locally..
easyXDM - communicates with an iframe, not a popup.
porthole - same, uses an iframe.
A horrible solution is refreshing the iframe every couple of seconds, to check if the user logged in already.
Is there a better way to do this? better libraries?
if you can refer the popup to another page after the user is logged in, you could use this:
main page:
localStorage.setItem('user_signed_in', false); // signed out by default
window.open("http://www.google.com/", "google_window", "menubar=no,resizable=no,scrollbars=no,status=no,width=400,height=300");
(function look_up() {
if(localStorage.getItem('user_signed_in')) {
go_on();
} else {
setTimeout(look_up, 500)
}
}());
function go_on() {
...
}
refer page:
localStorage.setItem('user_signed_in', true);
window.close();
Keep in mind that the refer page has to be on the same domain as your main page.
And, don't be afraid of bad support for localstorage in other browsers,
but if you really want to support oldies, you can use cookies, I believe.
When a user clicks a sign in button on site1 a pop up is opened from site2.
I'm assuming your using window.open thru an iframe to do this, and that you have already figured out how to bypass most browsers spam blockers etc.
Since you are opening this pop up as a new window, you are now in control of that window, and you can actually send stuff back from the new window.
This will be somewhat pseudo code, but just to make an example!
Lets say a user signs in with a link looking something like this:
<a href="" onclick="window.popup=window.open('/twitter/login.php', 'Twitter login', 'width=450,height=500'"</a>
Your pop up can now be refferenced by window.popup, and inside window.popup the original page is now called the window.opener.
On the same site that opens the popup you have a function, like this:
document.handleLogin = function (returnedDataFromPopup) {
console.log(returnedDataFromPopup);
}
After the user has logged in with oAuth, you need to redirect to a new page, this is explained in both the oAuth and Twitter guides, and you need to make that redirect happen inside the popup, and then capture the information from the login on that page and send it back to the original document and the handleLogin function.
Depending on what your using, in most PHP implentations you do something like this to get the data from the login, and this is of course after doing all the token and consumer key stuff:
$userinfo = $oAuth->getAttributes(); //or something similar, depends
So when redirecting from Twitter to a new page, the new page would look something like:
<? php
$userinfo = $oAuth->getAttributes();
?>
<script type="text/javascript">
window.opener.document.handleLogin(<?php echo json_encode($userinfo) ?>);
window.close();
</script>
This will actually run the handleLogin() function on the page that opened the popup, and it will send the userinfo from the popup to that function on the original page and then close the popup window.
I've used this technique with oAuth, OpenID, Google etc. and it works just fine without any need for local storage, cookies or page refresh at all, since you are in control of the popup window you can send information back and forth and you could even change the adress of the popup from the opening document on the fly if you wanted by doing something like this:
window.popup.location.href = 'google.com';
This is handy in some cases, for instance OpenID will by default close the popup and redirect the document.opener to the page specified, this is not what you want, and to overcome this you would have to open the popup on some random page, preferably an empty page that you control, and then redirect the popup's href to Twitter immediately after the popup is opened.
It all looks very complicated, but it is doable, and if you get this far, you now have the data, an all you have to do is somehow push it to site1 thru the iframe that holds site2. As pushing is'nt really possible without websockets or some sort of event driven server, like node.js, you will probably have to rely on long polling or something else, there are many ways to do this, and I'm sure you'll figure it out, but if you have some control over scripts running on site1, and you obviously have control over site2, then you can actually access some data thru an iframe with a little javascript, but I've never actually done that so I do not know exactly how it works.
It's not really relevant, but I don't really see why it would be useful for someone to login thru your site with an iframe from some other site, and it all seems strange to me, but thats up to you to figure out.

Facebook FB.ui oauth popup on Canvas Page

I am trying to use the FB.ui oauth popup on a Facebook Canvas Page. I am using the latest Javascript SDK.
On a Page Tab, it works great to just do: FB.ui({method: 'oauth'}, callback); That gives me the allow access popup and then calls my callback with the response perfectly. No redirect is necessary.
However when I try the exact same thing on a Canvas, I get a FB dialog that says "An error occurred. Please try again later". Has anyone gotten it to work, or know of any workarounds?
I did have success with the top.location.href = "http://www.facebook.com/dialog/oauth?client_id=xxx&redirect_uri=xxx approach, but I would prefer to not have to redirect if possible.
It turns out that FB.login(callback) works fine on a canvas page, and it avoids the redirect as well.
So the answer for me was to just change the FB.ui to instead do FB.login. The response sent to the callback is slightly different from FB.ui, but very close.
FB.login(function(response) {
if(response && response.session) {
//do stuff with session
}
});
What does "it does not work" mean? The popup might be blocked if it's not called in reaction to a user event (like a a mouse click).

Categories

Resources