Problems with logout if without session in wordpress - wordpress

I implement the facebook in my wordpress website and everything is working ok except one situation.
If I logout from facebook, and then I click logout in website, I get the error:
FB.logout() called without an access token.
So I replace the logout code with this:
return javascript:if(FB.getAccessToken()){FB.logout(function(){location.href='" . $url . "'})}else{location.href='" . $url . "'}";
(This is done in a wordpress hook, so that I can have the wordpress logout url)
But now, when I click logout in this situation I get a js error:
Unsafe JavaScript attempt to access frame...
How can I be able to logout safely from facebook and wordpress.
FB.Logout doesn't have an error callback and doesn't throw any js error, so it's difficult to check that situation.
Thank you!

You need to get the login status first from Facebook, and only if logged in can you call FB.logout. Try the following code.
FB.getLoginStatus(handleSessionResponse);
function handleSessionResponse(response) {
//if we dont have a session (which means the user has been logged out, redirect the user)
if (!response.authResponse) {
return;
}
//if we do have a non-null response.session, call FB.logout(),
//the JS method will log the user out of Facebook and remove any authorization cookies
FB.logout(response.authResponse);
}

Related

How to Logout user after resetting the password

I want my user to logout after they reset their password from my account page.
I have tried to reset cookies, tried wp-logout method.. It worked for few tests I have done then again, it's not working..
You can try by redirecting the user to the logout page link if their password reset is successful using
wp_redirect ()
After trying so many ways to do this, finally changed in "class-wc-form-handler.php" file in woocommerce plugin under "includes" folder. Or you can also override this in your functions.php Under "public static function save_account_details()" use wp_logout(); before "wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );"

Meteor Iron Router Login Route Pass-thru for Facebook accounts not working

The Issue:
When my regular users using the standard email/password 'accounts-password' go to a login redirected page , they can enter the credentials and the router will proceed to render the requested page.
For example:
/private_page (requires login). The user attempts to go to http://foo.com/private_page they are presented with the Login page. The user enters username/password correctly and now the /private_page displays.
This works, what does NOT work...
Same example as above, but now the user is authenticating with Facebook 'accounts-facebook'. Everything is the same as before and the user has succesful login with Facebook and is able to get into my web application except the route never displays the /private_page. It stays on the authenticated /login page without showing the login template.
IN A NUTSHELL
How do I make the facebook authenticated users pass-thru and route to the requested route like the regular password based users operate ?
Iron Router Config:
Router.onBeforeAction(function () {
if (!Meteor.userId() && !Meteor.loggingIn()) {
this.redirect('login');
this.stop();
} else {
this.next();
}
},{except: ['login', 'contact, 'terms']});
Meteor packages:
accounts-password#1.3.6
accounts-facebook#1.2.0
service-configuration#1.0.11
useraccounts:bootstrap
useraccounts:iron-routing
I finally came to a solution.
After reviewing the Github issues for the Meteor package: (meteor-useraccounts)I found the exact issue I have been having.
https://github.com/meteor-useraccounts/core/issues/685
I spent too much time trying to make the hooks fire correctly for my usage of oAuth Facebook with Meteor. My final solution was too just connect directly into the Meteor method calls and create my own login, registration, password reset, etc forms.
The up-side is that I now have full control of the forms and I don't need to deal with extra package.
If anyone comes finds this posting and is having issues making 'meteor-useraccounts' fire hooks like the postSignUpHook , you may decide to just scrap the package and make your own user account templates and connect the logic to use the native Meteor methods.
This particular question I submitted was because the oAuth Facebook would login but I was unable to make it redirect to the originally requested route. The oAuth works and my user can login to my web app, but I cannot get them to the originally requested url.
How did I solve this:
lib/routes
Router.onBeforeAction(function () {
if (!Meteor.userId() && !Meteor.loggingIn()) {
originalUrl = this.originalUrl;
this.redirect('login');
this.stop();
} else {
this.next();
}
},{except: ['login', 'resetPwd', 'help'] });
The key point to take away from the snippet above is the global variable I declare originalUrl. This is using the routers this.originalUrl. This url contains the original url that the user entered and is captured the moment before the iron-router redirects to the login page.
Now on the login page I created my two different login methods using my own custom template. They both are using the Meteor.loginWithPassword and Meteor.loginWithFacebook methods.
client/login.js
'click #fb-login' : function(e){
e.preventDefault();
Meteor.loginWithFacebook({}, function(err){
if(err) {
// some error occured
}
else {
if(Router.current().route._path == "/login" && typeof originalUrl == "undefined")
Router.go('/');
else
Router.go(originalUrl);
}
});
}
Hope this helps anyone else who might come across this issue.

wp_login and is_user_logged_in

I am developing an extension for a plugin and would like to run some code after every time a user logs in. Because I extend a plugin, I wanted to use the already written functions, which inside use is_user_logged_in() calls. If I register for the wp_login action and run is_user_logged_in in my action hook it returns false, which sounds really weird.
Code I was running:
add_action('wp_login', 'exhib_persist_cookies_after_login');
/*
* This method will persist the favorite posts from the cookies just after someone logs in.
*/
function exhib_persist_cookies_after_login() {
//Check if all the required functions are available
if (is_user_logged_in()) {
error_log("persist: USER LOGGED IN");
}
else {
error_log("persist: USER NOT LOGGED IN");
}
}
And in the log I see USER NOT LOGGED IN.
Anyone has a clue why is it happening? I thought is_user_logged_in is checking for the auth cookie, which is according to the doc is already set before wp_login is getting called.
Or anyone has an another idea what action should I register, which only fires once a user logged in and the is_user_logged_in returns there true?
Before you even look at why the modification isn't working, you should think about changing how you're modifying the plugin. Directly modifying a plugin is dangerous. It breaks the upgrade path preventing you from applying upgrades in the future which could resolve critical issues with the plugin itself. The same functionality could be achieved by creating a simple plugin that contains nothing but the code you want to run.

Wordpress Delete User after Success

I have a form we want users to access only once, anonymously. We hand out randomly generated usernames and passwords to allow anonymity. I would like to delete user, log off and redirect after successful submission.
I am able to delete the user with wp_delete_user($thisId); but alwyas have a "Cannot modify header information - headers already sent" error. I'm not sure how to approach this one.
I am processing in header.php
If you process in header.php it's too late because the server is already sending the page.
Try hooking your "delete_user" function in a previous action such as init or wp like this (in functions.php):
add_action('init', 'my_delete_user_process');
function my_delete_user_process(){
// Do your stuff
$user_id = get_current_user_id()
wp_delete_user($user_id);
// Do your stuff
}

Facebook php SDK getUser returns 0 apart from my laptop and the app developer

I've looked over hundreds of answers for similar issues to this but can't find anything that seems to help.
I'm running the latest version of the PHP SDK and a login to facebook button which has a generated link from getLoginUrl().
Running on my development laptop and logged in as the application developer it passes me back to the redirect url (Both the callback url when calling getLoginUrl() and the URL set in my application settings are exactly the same) I then do a getUser call which will function in these circumstances.
If i try the same process using my Iphone on the same network, logged in as the same user on facebook getUser() returns 0.
It also does the same for any other user trying to login with facebook.
Sandbox mode is disabled.
my app domains seem to be set up correctly.
I'm really unsure of what to do next.
Many thanks for your responses guys - Turns out the issue was i was sending the request from one page and redirecting back to another. This seems to upset facebook (I'm guessing it will only re-direct back to the page it was called from).
That solved the problem anyway - but many thanks for your responses.
First make sure you are maintaining sessions in your scripts with:
session_start();
at the top of your php file.
Next use something like this to test if you have a fb user and if not, redirect them to the oauth, which will just renew their token if they've already authorized by it's expired.
require_once('facebook/fb.inc');
session_start();
if (!$fbUser) {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,publish_stream,user_location',
'redirect_uri' => 'http://scubadivinglog.org/php/fblink.php'
)
);
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
Hope this helps. Let us know and if not post the code you are using.

Resources