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


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.


facebook.login( [listener] [, permissions] )
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.


-- 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
    return false

local function genericFacebookLogin( event )
    print( event )

-- 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" } )
                print( "The user did not grant the read-only permissions" )

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" } )

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