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.setFBConnectListener() or this function itself.

Syntax

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.

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.v4" )

-- 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 genericFacebookListener( event )
    print( event )
end

-- This listener will handle the request of read-only permissions, then request publishable permissions
local function intermediateListener( 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( genericFacebookListener, { "publish_actions" } )
            else
                print( "The user did not grant the read-only permissions" )
            end
        end
    end
end

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