Managing Login Accounts

Let users manage their login accounts, list all login providers and let them connect or disconnect their account from the front-end.

Retrieve login providers

Get list of enabled login providers.

{% set loginProviders = craft.social.getLoginProviders() %}

To retrieve disabled login providers as well, you will need to pass false as a parameter.

{% set loginProviders = craft.social.getLoginProviders(false) %}

Retrieve login accounts

Retrieve login account elements based on criterias.

{% set loginAccounts = craft.social.loginAccounts({
    providerHandle: 'google',
}).all() %}

Query Parameters

ParamAcceptsDescription
userIdint\|int[]\|nullThe user ID(s) that the resulting login accounts must belong to
providerHandlestring\|nullThe handle of the provider that the resulting login accounts must belong to
socialUidstring\|nullThe socialUid of the login account
usernamestring\|nullThe username of the user that the resulting login accounts must belong to
emailstring\|nullThe email of the user that the resulting login accounts must belong to
firstNamestring\|nullThe firstName of the user that the resulting login accounts must belong to
lastNamestring\|nullThe handle of the provider that the resulting login accounts must belong to
lastLoginDatemixedThe last time the user has logged in

Retrieve a login account

Get a login account from its login provider’s handle.

{% set loginAccount = craft.social.getLoginAccountByLoginProvider('google') %}

Connect to a login provider

Get login account connect URL by login provider handle.

<a href="{{ craft.social.getLoginAccountConnectUrl('google') }}>Enable social login with Google</a>

Disconnect from a login provider

Get login account disconnect URL by login provider handle.

<a href="{{ craft.social.getLoginAccountDisconnectUrl('google') }}">Disable social login with Google</a>

Example

This example lists enabled login providers and shows a “Enable/Disable Social Login” button for each one of them.

<h2>Login Accounts</h2>

{% for provider in craft.social.getLoginProviders() %}
    {% set account = craft.social.getLoginAccountByLoginProvider(provider.handle) %}

    <h4>{{ provider.name }}</h4>

    <p>
        {% if account %}
            You can login using {{ provider.name }}.
        {% else %}
            {{ provider.name }} login is disabled for your account.
        {% endif %}
    </p>

    <p>
        {% if account %}
            <a href="{{ craft.social.getLoginAccountDisconnectUrl(provider.handle) }}">Disable Social Login with {{ provider.name }}</a>
        {% else %}
            <a href="{{ craft.social.getLoginAccountConnectUrl(provider.handle) }}">Enable Social Login with {{ provider.name }}</a>
        {% endif %}
    </p>
{% endfor %}