# `Assent.Strategy.Facebook`
[🔗](https://github.com/pow-auth/assent/blob/v0.4.0/lib/assent/strategies/facebook.ex#L1)

Facebook OAuth 2.0 strategy.

The Facebook user endpoint does not provide data on email verification, email
is considered unverified. See the [Facebook login
documentation](https://developers.facebook.com/docs/facebook-login/multiple-providers#postfb1)
for more.

## Configuration

  * `:user_url_request_fields` - the fields for the resource, defaults to
    `email,name,first_name,last_name,middle_name,link,picture`. A larger
    image can be requested by using `picture.type(large){url}`.

See `Assent.Strategy.OAuth2` for more.

## Usage

    config = [
      client_id: "REPLACE_WITH_CLIENT_ID",
      client_secret: "REPLACE_WITH_CLIENT_SECRET",
      redirect_uri: "http://localhost:4000/auth/callback"
    ]

## With JS SDK

You can use the JS SDK instead of handling it through `authorize_url/1`. All
you have to do is to set up your HTML page in the following way:

    <fb:login-button scope="[SCOPES]" onlogin="checkLoginState();">
    </fb:login-button>
    <div id="status">
    </div>

    <script>
      function checkLoginState() {
        FB.getLoginStatus(function(response) {
          let signedRequest = response.authResponse.signedRequest
          let encodedPayload = atob(signedRequest.split('.')[1])
          let payload = JSON.parse(encodedPayload)

          window.location.href = '<%= @redirect_uri %>?&code=' + encodeURI(payload.code)
        });
      }

      window.fbAsyncInit = function() {
        FB.init({
          appId   : '[CLIENT_ID]',
          cookie  : true, // required for server to pick up session
          version : 'v26.0',
          xfbml   : true
        });
      };
    </script>
    <script async defer src="https://connect.facebook.net/en_US/sdk.js"></script>

In the above, the signed request is decoded and the authorization code is
fetched from it. The `@redirect_uri` is the callback URL.

You have to use an empty `redirect_uri` for the callback:

    {:ok, %{user: user, token: token}} =
      config
      |> Keyword.put(:redirect_uri, "")
      |> Assent.Strategy.Facebook.callback(params)

# `authorize_url`

```elixir
@spec authorize_url(Keyword.t()) :: Assent.Strategy.OAuth2.on_authorize_url()
```

Generates an OAuth 2.0 authorization URL.

# `callback`

```elixir
@spec callback(Keyword.t(), map()) :: Assent.Strategy.OAuth2.on_callback()
```

Handles an OAuth 2.0 callback.

# `fetch_user`

```elixir
@spec fetch_user(Keyword.t(), map()) :: {:ok, map()} | {:error, term()}
```

Fetches user data using the access token returned by the provider.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
