angular controller doesn't work after page changed - facebook

I have an angular app generated by yeoman,and it has two pages.
And I set a routeProvider for these two pages:
angular.module('fbPostApp', [
'ngCookies',
'ngResource',
'ngSanitize',
'ngRoute'
])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.otherwise({
redirectTo: '/'
});
$routeProvider
.when('/friend-filter', {
templateUrl: 'views/friend-filter.html',
controller: 'FriendFilterCtrl'
})
.otherwise({
redirectTo: '/'
});
});
MainCtrl:
function MainCtrl($scope,$http) {
window.fbAsyncInit = function() {
FB.init({
appId : 'Myid',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
$scope.login = function(){
FB.login(function(response) {
$scope.uid = response.authResponse.userID;
$scope.accessToken = response.authResponse.accessToken;
login_success($scope.uid,$scope.accessToken);
});
}
................
};
};
};
FriendFilterCtrl:
function FriendFilterCtrl($scope,$http) {
window.fbAsyncInit = function() {
FB.init({
appId : 'Myid',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
$scope.login = function(){
FB.login(function(response) {
$scope.uid = response.authResponse.userID;
$scope.accessToken = response.authResponse.accessToken;
login_success($scope.uid,$scope.accessToken);
});
}
................
};
};
}
My link:
Main
Friend
When I get into both pages their controllers work very well(Login with your Facebook).However, if I "switch" to another page by link,I can get the page's template but it won't do anything.
So I want to ask that is there anything I missed or did wrong cause this situation?

Put this code outside of any angular controller:
window.fbAsyncInit = function() {
FB.init({
appId : 'Myid',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
Your controller(s) should now look like:
angular.module('fbPostApp').controller('MainCtrl', ['$scope','$http', function ($scope,$http) {
$scope.login = function(){
FB.login(function(response) {
$scope.uid = response.authResponse.userID;
$scope.accessToken = response.authResponse.accessToken;
login_success($scope.uid,$scope.accessToken);
});
}
................
}])
Note: if you have a global Facebook login button, you shouldn't copy your $scope.login function to every single controller in the app. Learn about angular directives.

Related

Meteor Facebook login with a signed request

My app uses Account-Facebook to handle Facebook login.
I'm trying to implement this way of login. https://developers.facebook.com/docs/facebook-login/using-login-with-games#login
Assuming I have for a particular user:
{
"oauth_token": "{user-access-token}",
"algorithm": "HMAC-SHA256",
"expires": 1291840400,
"issued_at": 1291836800,
"user_id": "218471"
}
How can I hack Account-Facebook to log the user like Meteor.loginWithFacebook does?
Here is how I implemented :
client/landing.html
<template name="landing">
<a class="btn btn-lg btn-primary" id="login" role="button">Login with Facebook</a>
</template>
client/landing.js
Template.landing.events({
'click #login': function (e, tmpl) {
console.log("Logging you in ");
Meteor.loginWithFacebook({
requestPermisssions: ['user']
}, function(err){
if(err){
//handle errors
}
else{
//show an alert
}
});
}
});
server/accounts.js
Accounts.onCreateUser(function(option, user){
var accessToken = user.services.facebook.accessToken;
var result, profile;
result = Meteor.http.get("https://graph.facebook.com/v2.2/me?fields=id,name,email,age_range",{
params: {
access_token: accessToken
}
});
if(result.error)
throw result.error;
fbData = _.pick(result.data,
"name",
"email",
"age_range",
)
console.log(profile);
return profile;
});
server/config.js
ServiceConfiguration.configurations.remove({
service: "facebook"
});
ServiceConfiguration.configurations.insert({
service: "facebook",
appId: "<your AppId>",
secret: "<your App Secret>",
loginStyle: "popup"
});
Have a look at this tutorial.
Also you'll need to add this package : service-configuration

facebook js sdk post to wall

i am struggling to ad the post to wall functionality to an existing piece of code ?
It works fine with logging in and obtaing email for form submit, i would like it to post to wall on log in as well ! in the simplist way possible .?
URL to see it in action is http://lproctor.co.uk/.
// The facebook JavaScript SDK to log in to FB
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
cookie: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
FB.ui(
{
method: 'feed',
name: 'The Facebook SDK for Javascript',
caption: 'Bringing Facebook to the desktop and mobile web',
description: (
'A small JavaScript library that allows you to harness ' +
'the power of Facebook, bringing the user\'s identity, ' +
'social graph and distribution power to your site.'
),
link: 'https://developers.facebook.com/docs/reference/javascript/',
picture: 'http://www.fbrell.com/public/f8.jpg'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
NOW USING
<script type="text/javascript">
// The facebook JavaScript SDK to log in to FB
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
cookie: true,
xfbml: true,
oauth: true
});
FB.ui(
{
method: 'feed',
name: 'Facebook name',
link: 'http://lproctor.co.uk/',
picture: 'http://lproctor.co.uk/logo.jpg',
caption: 'facebook caption',
description: ' facebook description facebook description facebook description facebook description',
message: 'Facebook message!'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
Try something like that
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// connected
} else if (response.status === 'not_authorized') {
// not_authorized
login();
} else {
// not_logged_in
login();
}
});
};

How to properly handle user registration and redirect?

I have a registration form that uses an old version of the FB API.
I am trying to update it to the current version and am having problems dealing with the response from FB.
After clicking the FB login button and granting permissions (email,publish_stream) I cannot find a way to know that the user has granted access (and not just logged in as an already registered user) and needs to proceed to the next page of the registration form.
The response after a successful permission request is the same as a successful login:
{
authResponse:
{
accessToken: AAAFEIew...URYo,
userID: 100003908595992,
expiresIn: 6014,
signedRequest: uhTqZodDn0QUoWAUBuYEKmlaM8zViO0r_fnKONaC4v8.eyJhbGdvcml...k5MiJ9,
},
status: connected
}
How can I tell if the login was the result of an already registered user or a new registration?
I am using the javascript SDK.
Code:
<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script>
<div id="fb-root" style="padding: 0px;"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'API_KEY',
status : true,
channelUrl : '//'+window.location.hostname+'/channel.php',
cookie : true,
xfbml : true,
oauth : true
});
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', function(response) {
if (response.authResponse) {
// user has auth'd your app and is logged into Facebook
alert('logged in!');
}
else
{
// user has not auth'd your app, or is not logged into Facebook
alert('not logged in!');
}
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
This is the resource I used when attempting to do what you are doing. It works extremely well.
window.fbAsyncInit = function() {
FB.init({ appId: 'Your_APP_ID',
status: true,
cookie: true,
xfbml: true,
oauth: true});
showLoader(true);
function updateButton(response) {
button = document.getElementById('fb-auth');
userInfo = document.getElementById('user-info');
if (response.authResponse) {
//user is already logged in and connected
FB.api('/me', function(info) {
login(response, info);
});
button.onclick = function() {
FB.logout(function(response) {
logout(response);
});
};
} else {
//user is not connected to your app or logged out
button.innerHTML = 'Login';
button.onclick = function() {
showLoader(true);
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(info) {
login(response, info);
});
} else {
//user cancelled login or did not grant authorization
showLoader(false);
}
}, {scope:'email,user_birthday,status_update,publish_stream,user_about_me'});
}
}
}
// run once with current status and whenever the status changes
FB.getLoginStatus(updateButton);
FB.Event.subscribe('auth.statusChange', updateButton);
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol
+ '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
Reference: http://thinkdiff.net/facebook/new-javascript-sdk-oauth-2-0-based-fbconnect-tutorial/
Faith Sloan
I realized that I needed to use the getLoginStatus function in the FB SDK.
My functioning code is:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo HSWI_FB_KEY; ?>',
status : true,
logging : true,
channelUrl : '//'+window.location.hostname+'/channel.php',
cookie : true,
xfbml : true,
oauth : true
});
FB.Event.subscribe('auth.login', function(response)
{
window.location.href = '/login_script.php';
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
checkLoggedInFB();
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
function fbLogin()
{
FB.login(
function(response)
{
if(response.authResponse)
{
window.location.href = '/login_script.php';
}
}
);
}
function checkLoggedInFB()
{
try
{
FB.getLoginStatus(
function(response)
{
if(response.status == 'connected')
{
window.location.href = '/login_script.php';
}
}
);
}
catch(e)
{
alert('Error checking if logged into FB: '+ e.message);
return false;
}
return true;
}
</script>
<style type="text/css">
.fb-login-button
{
margin-top: -15px;
}
.fb-login-button.fb_iframe_widget.fb_hide_iframes
{
display: none;
}
</style>
<div class="fb-login-button" scope="email,publish_stream,user_birthday,user_location,user_about_me">Login</div>

FaceBook FB.ui logout dont fire

FB.ui(
{
method: 'feed',
name: 'some text',
link: 'some text',
picture: 'aa.jpg',
caption: 'some text',
description: 'some text',
message: 'some text'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
});
}
That code WORK fine, now i like after :
alert('Post was published.');
to be logged out from facebook, silently
HOW ?
Adding that code After the alert('post publish') did not do anything !!
FB.ui(
{ method:'auth.logout', display:'hidden' },
function() { alert("you're logged out!"); }
);
i have found : FB auth.logout is being raised after being logged in using the "server-side-workflow" (OAuth 2.0) but not sure i understand the code enough to know it do what i ask !
https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
https://developers.facebook.com/docs/reference/javascript/FB.logout/
Best Practices
FB.logout will log the user out of both your site and Facebook. You
will need to have a valid access token for the user in order to call
the function.
Calling FB.logout will also invalidate the access token that you have
for the user, unless you have the offline_access permission.
I wrote a sample using the comments box to fire the auto logout
http://shawnsspace.com/fb.logout.test.php
THE CODE:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '112104298812138',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
//channelUrl : 'http://WWW.MYDOMAIN.COM/channel.html', // channel.html file
oauth : true // enable OAuth 2.0
});
FB.Canvas.EarlyFlush.addResource("http://shawnsspace.com/index.php");
FB.Canvas.setAutoResize();
FB.getLoginStatus(function(response) {
if (response.authResponse) {
var accessToken = response.authResponse.accessToken;
} else {
}
});
FB.Event.subscribe('comment.create', function(response) {
//alert(JSON.stringify(response));
FB.logout(function(response) {
window.location.reload();
});
});
FB.Event.subscribe('auth.login', function(response) {
//top.location.href = 'http://apps.facebook.com/shawnsspace/fbcomments.php?ref=loggedin';
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
//top.location.href = "http://apps.facebook.com/shawnsspace/fbcomments.php?ref=loggedout";
alert('logged out');
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>

Delete facebook comment from my site or facebook Javascript SDK

I am using facebook javascript SDK to post comment on facebook and using this code:
FB.init({
//appId: 'YOUR_APP_ID', cookie: true,
appId: '2697XXXXXXXXXX098', cookie: true,
status: true, xfbml: true
});
FB.ui({
method: 'feed',
name: 'MyComment',
link: 'http://www.facebook.com./',
picture: 'http://xyz.com/App_Shared/Images/logo.jpg',
caption: 'Comment caption',
description: 'comment description',
message: 'Join facebook.'
}, function (response) {
if (response && response.post_id) {
alert('Post was published.' + response.post_id);
} else {
alert('Post was not published.');
}
});
and now i want to delete this post by using the same type of js code.
I saved post_id in my database.
Can you provide me the code or any URL to do this.....
First of all you must give to access to you application and allow by user according to this URL
http://developers.facebook.com/docs/reference/api/permissions/
These permission you can use like this method
FB.login(function (response) {
if (response.authResponse) {
FB.api('/me', function (response) {
jQuery('#hdfUID1').val(response.id);
jQuery('#btnFBLoginTemp').click();
// FB.logout(function (response) {
// });
});
} else {
jQuery('#hdfUID1').val('');
}
}, { scope: 'email,user_photos,read_stream,publish_stream,offline_access' });
if user allow these permissions then you can delete comment from user's wall which was posted by your application by using "Anatoly Lubarsky" answer method....
FB.api(postId, 'delete', function(response) {
// callback here
});
should be very simple like:
FB.api(postId, 'delete', function(response) {
// callback here
});
hope this helps