In my app I can log in to Facebook but I can't logout. When I click button which call logout function I get:
Object { authResponse: null, status: "unknown" }
Here is my code:
function login_to_fb() {
FB.login(function(response) {
if (response.status === 'connected') {
window.location.reload();
}
}, {
scope: 'email'
});
}
function logout_from_fb() {
FB.getLoginStatus(function(response) {
console.log(response);
if (response && response.status === 'connected') {
FB.logout(function(response) {
window.location.reload();
});
}
});
}
I figured out it's because I refresh the page. Without it, scripts works fine but when I log-in and refresh the page then log-out is impossible. How can I save it?
It is possible that the result is cached. Try passing true as the second argument to clear the cache:
FB.getLoginStatus(function(response) {
// this will be called when the roundtrip to Facebook has completed
}, true);
Related
I am working with the FaceBook JavaScript SDK to log users in via FaceBook SSO. documentation
Clicking the SSO button opens a new window prompting the user to log in via FaceBook. If the user logs in there is no issue. However, if the user exits or cancels the SSO, I am not getting a response from FB.
Here is the function given in the FB documentation:
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
I have tried using this function exactly and again if I cancel the SSO the else block never runs as I would expect it to.
I am trying to modify the function slightly but I still am not able to get a response when the page is cancelled.
static login(callbacks, { mode = "login" }) {
const FB = window.FB;
FB.login(
(res) => {
console.log(res);
const { authResponse } = res;
if (mode === "login") {
Facebook.socialSignOn(authResponse, callbacks);
} else if (mode === "verify") {
Facebook.socialVerify(authResponse, callbacks);
}
},
{ return_scopes: "true", scope: "email" }
);
}
UPDATE
After playing around more it seems like if I try to use the FB SSO, cancel, and then try again all future FB.login() attempts immediately receive a response with authResponse : null
I don't know how to sync in api FB.login and here is my code:
function getAccesToken(){
var access_token = "";
FB.login(function(response) {
if (response.authResponse) {
access_token = FB.getAuthResponse()['accessToken'];
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, { scope: '' });
return access_token;
}
when I call function
var access_token = getAccessToken();
but it return empty string. How can I fix it to be synchronous?
You can't make this synchronous - and it would be extremely bad practice if you could. Instead, call a function in the FB.login callback handler that contains the access token. Try this:
function getAccesToken() {
FB.login(function(response) {
if (response.authResponse) {
loginComplete(FB.getAuthResponse()['accessToken']);
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, { scope: '' });
}
function loginComplete(token) {
console.log(token);
// do something here, now the user is logged in...
}
FB.login needs to be called on user interaction (mouse click) and you have to use the callback function of FB.login to get the Access Token asynchronously - the Access Token will be in the response value. For example:
document.getElementById('loginBtn').addEventListener('click', function() {
FB.login(function(response) {
if (response.authResponse) {
doSomethingWithTheToken(response.authResponse.accessToken);
}
}, {scope: 'email,public_profile', return_scopes: true});
}, false);
function doSomethingWithTheToken(token) {
console.log(token);
}
Source (and a lot more information about the JavaScript SDK): http://www.devils-heaven.com/facebook-javascript-sdk-login/
So I have been going through the facebook developer documentation and I have managed to get to a point where the user clicks the facebook login button and gives me publish permissions. Then my function will post something to their wall.
My question: Is their a way for my facebook app to post to the walls of all the users who have given my app publish permissions, using either the javascript sdk or the php sdk.
My current code:
FB.Event.subscribe('auth.authResponseChange', function(response) {
if (response.status === 'connected') {
testAPI();
} else if (response.status === 'not_authorized') {
FB.login(function(response) {
console.log(response);
},{scope: 'publish_actions'});
} else {
FB.login(function(response) {
console.log(response);
},{scope: 'publish_actions'});
};
});
function postAPI() {
var body = 'We are testing the ability of posting on users behalf.';
FB.api('/me/feed','post',{message:body},function(response){
if (!response || response.error) {
alert('Error occurred');
} else {
console.log('Post ID: ' + response.id);
};
});
};
My MVC application is detect first if user is logged into Facebook:\
function isLoggedIn(callback, param1) {
window.FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
updateUser(response.authResponse);
callback(param1);
} else {
login(callback, param1);
}
});
}
if user not connectd i use this function to login:
function login(callback, success) {
window.FB.login(function (response) {
if (response.authResponse) {
updateUser(response.authResponse, callback, success);
callback(success);
} else {
callback(null);
}
});
}
And in addition i subscribed to change status:
FB.Event.subscribe('auth.authResponseChange', function (response) {
if (response.status === 'connected') {
top.location.href = top.location.href;
//window.location.reload();
}
if (response.status != 'connected' && response.status != 'not_authorized') {
updateUser(null);
}
});
I must refresh page after user login to get FacebookSignedRequest on server.
top.location.href = top.location.href and window.location.reload() is not working...
any suggestions :)
location.reload() does work.
If it doesn't the code never executes or you overwrote the global location variable somewhere.
I have some 3 methods.
login();
isLogin();
reply();
function isLogin(){
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
} else {
// no user session available, someone you dont know
}
});
}
function login (){
FB.login(function(response) {
if (response.session) {
// user successfully logged in
} else {
// user cancelled login
}
});
}
function reply(){
var body = 'Reading Connect JS documentation';
FB.api('/me/feed', 'post', { body: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response);
}
});
}
I need, when a user will want to reply a comment then reply method will execute. before executing reply method it should test if user is login, if user is not login then execute login method and call reply method again. How can I achieve this task
Thanks in advance
you need to construct conditions that will handle the following:
When a user will want to reply a comment
Check session if user isLogin()
if TRUE then reply method is enabled by default
if FALSE then call login() method via ajax for awesomeness
if login.success then call reply method
if login.failed add statement here