facebook.login()

Type Function
Return value Event
Revision Release 2024.3703
Keywords Facebook, login
See also facebook.logout()
facebook.*
fbconnect

Overview

Prompts the user to log in to Facebook. This function can be called even if the user is already logged in. If the user is already logged in but new permissions are requested, the user will be prompted to grant the new permissions. The recommended flow is to request no permissions the first time and then ask for additional permissions as they are needed.

The login comes back in the form of a fbconnect event that is sent to the listener specified within facebook.init(), facebook.setFBConnectListener(), or this function itself.

We strongly recommended that you call facebook.init() and wait for initialization to complete before making this API call. See code below for an example implementation.

Syntax

facebook.login( [listener] [, permissions], [, limitedLogin] )
listener (optional)

Listener. A listener that responds to fbconnect events. If the listener is a table, it should have a property "fbconnect" that is a function.

permissions (optional)

Array. An optional array of strings that correspond to Facebook's publishing permissions. This enables your application to ask the user to grant certain extended permissions to your app. If not supplied, no extended permissions are requested. Use "publish_actions" to allow posting to the user's wall. For developer convenience, required permissions such as "public_profile" (reference) and "user_friends" (reference) will be automatically requested if needed.

limitedLogin (optional)

Boolean. default is false, using Limited Login with the app will not be used to personalize or measure advertising effectiveness. Android does not support FB Limited Login.

Gotchas

-- This call will only request "user_events" as it's a read-only permission
facebook.login( { "user_events", "public_actions" } )
local facebook = require( "plugin.facebook.v4a" )

-- Check for a value inside the provided table
local function valueInTable( t, valueToFind )
    for k,v in pairs( t ) do
        if v == valueToFind then
            return true
        end
    end
    return false
end

local function genericFacebookLogin( event )
    print( event )
end

-- This listener will handle the request of read-only permissions, then request publishable permissions
local function intermediateFacebookLogin( event )

    if ( "session" == event.type ) then
        if ( "login" == event.phase ) then
            local accessToken = facebook.getCurrentAccessToken()

            -- Continue only if the user granted the read-only permissions
            if ( valueInTable( accessToken.grantedPermissions, "user_events" ) ) then
                facebook.login( genericFacebookLogin, { "publish_actions" } )
            else
                print( "The user did not grant the read-only permissions" )
            end
        end
    end
end

local function facebookListener( event )

    if ( "fbinit" == event.name ) then

        print( "Facebook initialized" )

        -- Request read-only permissions, followed by publishable permissions
        facebook.login( intermediateFacebookLogin, { "user_events" } )
    end
end

-- Set the "fbinit" listener to be triggered when initialization is complete
facebook.init( facebookListener )