# Implement Social Login for Storefront Next

Storefront Next offers a native social login feature for your shoppers. Include social login in your Storefront Next site to improve the shopper experience. With social login, shoppers can quickly log into your site and create profiles by using their social network accounts.

Our [demo site](https://sfcc-odyssey-production.mobify-storefront.com/) shows the default implementation of social login. Test the social login flow on the demo site by clicking **Sign In**, and then **Continue with Apple** or **Continue with Google**.

To enable and configure social login for your Storefront Next site, follow the steps in this guide. Throughout this guide we use an example storefront with the URL: `https://www.example.com`. Replace `example.com` with your domain name.

## Supported IdPs

Storefront Next supports the identity providers (IdPs) listed under [Supported Identity Providers](https://developer.salesforce.com/docs/commerce/commerce-api/guide/slas-identity-providers.md#supported-identity-providers).

If you meet the [prerequisites](#prerequisites), the Google and Apple social logins are available in the Retail React App by default.

## Prerequisites

- To use password reset, build your site by using Storefront Next and the [Storefront Next template](https://github.com/SalesforceCommerceCloud/storefront-next-template) version 1.0.0 or later.
- Add your chosen IdP by completing the steps in [Registering an Identity Provider](https://developer.salesforce.com/docs/commerce/commerce-api/guide/slas-identity-providers.md#registering-an-identity-provider).
- [Configure a Shopper Login and API Access (SLAS) Client](https://developer.salesforce.com/docs/commerce/commerce-api/guide/authorization-for-shopper-apis.md#create-a-slas-client). In your client’s Redirect URI field, enter the redirect URI that you want to use in the social login flow. By default, Storefront Next uses `/social-callback` as the path. For example, if your domain is `example.com`, enter: `https://www.example.com/social-callback`. Replace `example.com` with your actual domain.

## Configure Social Login in Storefront Next

### Update `config.server.ts`

To configure social login in your Storefront Next project, update `config.server.ts` as shown in this example.

Ensure that the redirect URI path configured in your Storefront Next project matches the path configured in your SLAS client. The SLAS client can use a wildcard for the full URI, but the paths must match exactly.

```jsx
module.exports = {
    app: {
        login: {
            social: {

               // Enable social login and add your chosen IdPs and Redirect URI.
                enabled: true,
                idps: ['Google', 'Apple'],
                redirectURI: '/social-callback' // relative path
            }
            // More code here...

         sites,
         commerceAPI: {
            proxyPath: `/mobify/proxy/api`,
            parameters: {

            // Add your clientId, organizationId, short code, and siteId.
                clientId: 'c9c45bfd-0ed3-4aa2-9971-40f88962b836',
                organizationId: 'f_ecom_zzrf_001',
                shortCode: '8o7m175y',
                siteId: 'RefArchGlobal'
            }

           // More code here...
```

### Optional: Use an Environment Variable to Set the Redirect URI

:::note
The value of the environment variable for social login overrides any values that you added in `config.server.ts`.
:::

Optionally, you can set the redirect URI using an environment variable instead of `config.server.ts`. Use `config.server.ts` to enable social login and set the IdPs. Configure this environment variable to set the redirect URI.

`PUBLIC__app__features__SOCIAL_LOGIN_REDIRECT_URI`

## Add Icons for Supported IdPs

By default, Storefront Next imports and configures the icons for the Google and Apple social logins in the `SocialLogin` component in the `template-retail-react-app/app/components/social-login/index.jsx` file. On your site, you can include icons for other [supported IdPs](https://developer.salesforce.com/docs/commerce/commerce-api/guide/slas-identity-providers.md#supported-identity-providers). To do so, complete these steps.

### Import Icons into the `SocialLogin` Component

In this example, we import the Facebook icon into the `SocialLogin` component.

```jsx
// Icons
import {
  AppleIcon,
  GoogleIcon,
  FacebookIcon,
} from "@salesforce/retail-react-app/app/components/icons";
```

### Extend the `IDP_CONFIG` Object

Use the `IDP_CONFIG` object in the `SocialLogin` component to display buttons for your additional social logins, each with their respective icons and localized messages.

In this example, we add the configuration for Facebook. Ensure that each IdP key matches the lowercase name of your chosen IdP.

```jsx
const IDP_CONFIG = {
  apple: {
    icon: AppleIcon,
    message: defineMessage({
      id: "login_form.button.apple",
      defaultMessage: "Apple",
    }),
  },
  google: {
    icon: GoogleIcon,
    message: defineMessage({
      id: "login_form.button.google",
      defaultMessage: "Google",
    }),
  },
  facebook: {
    icon: FacebookIcon,
    message: defineMessage({
      id: "login_form.button.facebook",
      defaultMessage: "Facebook",
    }),
  },
};
```

## Best Practices for Social Login

- Follow security best practices to protect shopper accounts. For example:
  - If you’re using embedded CDN (eCDN), you can apply [eCDN Rate Limiting Rules](https://developer.salesforce.com/docs/commerce/commerce-api/guide/cdn-rate-limiting-rules.md).
  - If you’re using a third-party content delivery network (CDN) such as Cloudfare, you can [Block requests by Threat Score](https://developers.cloudflare.com/waf/custom-rules/use-cases/block-ip-reputation/).
- Track shopper usage of authentication options and diagnose issues efficiently. For example, use [Log Center](https://help.salesforce.com/s/articleView?id=cc.b2c_log_center.htm\&type=5) to access logs from Managed Runtime (MRT) and your B2C Commerce instance.
- [Merge Shopper Profiles](https://developer.salesforce.com/docs/commerce/commerce-api/guide/merge-shopper-profiles.md) in Shopper Login and API Access Service (SLAS) to ensure that shoppers with both a B2C Commerce identity and a third-party IdP can log in seamlessly without creating duplicate accounts.

:::note
- If both profiles exist before you configured `loginMergeClaims` and before a shopper logged into your site, no merge is performed. Accounts can't be merged retroactively.
- The steps in the Merge Shopper Profiles guide apply to shoppers who have the same login ID for their B2C Commerce profile and a third-party IdP.
:::

For more details about merging profiles and the supported use cases, see [Merge Shopper Profiles](https://developer.salesforce.com/docs/commerce/commerce-api/guide/merge-shopper-profiles.md).

## Troubleshoot Social Login

This section provides suggested solutions for a few common errors that you can encounter related to social login.

### Shoppers Can’t Log in

**Potential Cause:** The social login configuration is incorrect.

**Suggested Solution:** Ensure that the redirect URI path configured in your Storefront Next project matches the path configured in your SLAS client. See [Configure Social Login](#configure-social-login-in-storefront-next).

### Shopper Profile Conflicts Occur

**Potential Cause:** Shoppers have separate profiles for B2C Commerce and third-party IdPs.

**Suggested Solution:** Follow the steps in [Merge Shopper Profiles](https://developer.salesforce.com/docs/commerce/commerce-api/guide/merge-shopper-profiles.md) to merge records for shoppers registered with B2C Commerce and a third-party IdP.

## See Also

- [SLAS Identity Providers](https://developer.salesforce.com/docs/commerce/commerce-api/guide/slas-identity-providers.md)
