I am trying to implement an authorization feature using the Facebook JavaScript SDK. When I run it, it and check the console I see the error.
uncaught TypeError: Cannot read property 'userID' of undefined
Code snippet
<div id="fb-root"></div>
<!-- Load the Facebook JavaScript SDK -->
<script src="//connect.facebook.net/en_US/all.js"></script>
<script>
var appId = 'APP_ID';
var uid;
// Initialize the JS SDK
FB.init({
appId: '413026618765431',
cookie: true,
});
// Get the user's UID
FB.getLoginStatus(function(response) {
uid = response.authResponse.userID ? response.authResponse.userID : null;
});
function authUser() {
FB.login(function(response) {
uid = response.authResponse.userID ? response.authResponse.userID : null;
}, {scope:'email,publish_actions'});
}
</script>
If the app is not authorized, then the response object will not contain an
authResponseproperty – hence the error you’re seeing.What you want is
or simply
or simply