Social Login

Log in with your social media accounts!

Back to Social Login Support

sami
sami

how to add linkedin "Extensible! Don't see one you want? Add it!"

Last updated

vdomah
vdomah
use Backend\Widgets\Form;
use Flynsarmy\SocialLogin\SocialLoginProviders\SocialLoginProviderBase;
use URL;

class LinkedIn extends SocialLoginProviderBase
{
    use \October\Rain\Support\Traits\Singleton;

    protected $driver = 'linkedin';

    protected $callback;
    protected $adapter;

    /**
     * Initialize the singleton free from constructor parameters.
     */
    protected function init()
    {
        parent::init();

        $this->callback = URL::route('flynsarmy_sociallogin_provider_callback', ['LinkedIn'], true);
    }

    public function getAdapter()
    {
        if (!$this->adapter) {
            // Instantiate adapter using the configuration from our settings page
            $providers = $this->settings->get('providers', []);

            $this->adapter = new \Hybridauth\Provider\LinkedIn([
                'callback' => $this->callback,

                'keys' => [
                    'id'     => @$providers['LinkedIn']['client_id'],
                    'secret' => @$providers['LinkedIn']['client_secret'],
                ],

                'scope' =>
                    'r_liteprofile r_emailaddress',

                'debug_mode' => config('app.debug', false),
                'debug_file' => storage_path('logs/flynsarmy.sociallogin.' . basename(__FILE__) . '.log'),
            ]);
        }

        return $this->adapter;
    }

    public function isEnabled()
    {
        $providers = $this->settings->get('providers', []);

        return !empty($providers['LinkedIn']['enabled']);
    }

    public function isEnabledForBackend()
    {
        $providers = $this->settings->get('providers', []);

        return !empty($providers['LinkedIn']['enabledForBackend']);
    }

    public function extendSettingsForm(Form $form)
    {
        $form->addFields([
            'providers[LinkedIn][enabled]' => [
                'label' => 'Enabled on frontend?',
                'type' => 'checkbox',
                'comment' => 'Can frontend users log in with LinkedIn?',
                'default' => 'true',
                'span' => 'left',
                'tab' => 'LinkedIn',
            ],

            'providers[LinkedIn][app_name]' => [
                'label' => 'Application Name',
                'type' => 'text',
                'default' => 'Social Login',
                'comment' => 'This appears on the LinkedIn login screen. Usually your site name.',
                'tab' => 'LinkedIn',
            ],

            'providers[LinkedIn][client_id]' => [
                'label' => 'Client ID',
                'type' => 'text',
                'tab' => 'LinkedIn',
            ],

            'providers[LinkedIn][client_secret]' => [
                'label' => 'Client Secret',
                'type' => 'text',
                'tab' => 'LinkedIn',
            ],
        ], 'primary');
    }

    public function redirectToProvider()
    {
        if ($this->getAdapter()->isConnected()) {
            return \Redirect::to($this->callback);
        }

        $this->getAdapter()->authenticate();
    }

    /**
     * Handles redirecting off to the login provider
     *
     * @return array ['token' => array $token, 'profile' => \Hybridauth\User\Profile]
     */
    public function handleProviderCallback()
    {
        $this->getAdapter()->authenticate();

        $token = $this->getAdapter()->getAccessToken();
        $profile = $this->getAdapter()->getUserProfile();

        // Don't cache anything or successive logins to different accounts
        // will keep logging in to the first account
        $this->getAdapter()->disconnect();

        return [
            'token' => $token,
            'profile' => $profile
        ];
    }
}

1-2 of 2