> ## Documentation Index
> Fetch the complete documentation index at: https://docs.median.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Server-side redirects

> Handle Facebook, Google, and Apple social login on your backend by redirecting authentication tokens to your server as URL parameters.

This guide walks through implementing social login using server-side redirects with the Median JavaScript Bridge. Use this approach when you want to handle authentication through your backend API, keeping token validation and session management secure on your server.

**Before you start**

You'll need:

* A backend API endpoint that can receive GET requests with tokens and session details as URL parameters
* The ability to parse JWT tokens on your server to extract user details
* Your server configured to create login sessions for authenticated users
* Your social login providers already configured ([Facebook](https://docs.median.co/docs/facebook-login-configuration.md), [Google](https://docs.median.co/docs/google-sign-in-configuration.md), and/or [Apple](https://docs.median.co/docs/sign-in-with-apple-configuration.md))

## Understanding the parameters

Each social login method accepts these parameters:

* **redirectUri** (required): Your backend API endpoint URL. After login completes, the webview redirects here with the authentication token added as a URL parameter.
* **scope** (optional): Defines what user data your app can access. Each provider has different scope options:
  * [Facebook scope documentation ](https://developers.facebook.com/documentation/development/permissions)
  * [Google scope documentation ](https://developers.google.com/identity/protocols/oauth2/scopes)
  * [Apple scope documentation ](https://developer.apple.com/documentation/signinwithapplejs/clientconfigi/scope)
* **state** (optional): An optional string you supply to verify the request came from your app. We'll pass this back to you unchanged. Use it to prevent CSRF attacks.

Here's the basic syntax for each provider:

Median JavaScript Bridge

```javascript JavaScript theme={null}
median.socialLogin.facebook.login({ 
  'redirectUri' : '<endpoint>', 
  'scope' : '<text>', 
  'state' : '<text>' 
});
median.socialLogin.google.login({ 
  'redirectUri' : '<endpoint>', 
  'state' : '<text>' 
});
median.socialLogin.apple.login({ 
  'redirectUri' : '<endpoint>', 
  'scope' : '<text>', 
  'state' : '<text>' 
});
```

## What you'll receive

When login succeeds, your `redirectUri` receives the authentication tokens as URL parameters. If the user cancels the login process, the `error` parameter will contain an error message.

**Redirect URL API endpoint parameters**

| Identity provider | Status | Parameters | Example |
| - | - | - | - |
| Facebook Login | Success | `accessToken`, `userId`, `type=facebook`, `state` | `https://yoursite.com/auth/facebook/redirect?accessToken=example&type=facebook&userId=1234567890&state=example` |
| Facebook Login | Error | `error`, `type=facebook` | `https://yoursite.com/auth/facebook/redirect?error=error&type=facebook` |
| Google Sign-In | Success | `idToken`, `type=google`, `state` | `https://yoursite.com/auth/google/redirect?idToken=example&type=google&state=example` |
| Google Sign-In | Error | `error`, `type=google` | `https://yoursite.com/auth/google/redirect?error=error&type=google` |
| Sign in with Apple | Success | `idToken`, `code`, `firstName`, `lastName`, `type=apple`, `state` | `https://yoursite.com/auth/apple/redirect?idToken=example&code=example&firstName=John&lastName=Doe&type=apple&state=example` |
| Sign in with Apple | Error | `error`, `type=apple` | `https://yoursite.com/auth/apple/redirect?error=error&type=apple` |

Apple only includes `firstName` and `lastName` on the user's first login. See [Platform-specific requirements](#platform-specific-requirements).

## Platform-specific requirements

> [tab] Sign in with Apple
>
> Apple only sends complete user profile data (`firstName`, `lastName`, `email`) on the *first* login. After that, you only get a JWT token and a user identifier.
>
> Here's what to do:
>
> * Save the full user profile to your database during the first authentication
> * On subsequent logins, retrieve the stored profile using the user identifier from the JWT
> * Parse the JWT for additional fields like email and email\_verified
>
> [Read Apple's documentation on retrieving user information ](https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/authenticating_users_with_sign_in_with_apple#:~:text=Retrieve%20the%20user%E2%80%99s%20information%20from%20Apple%20ID%20servers)

> [tab] Facebook on iOS
>
> When users decline **App Tracking Transparency (ATT)**, or tracking **isn't enabled**, Facebook uses **Limited Login** mode.
>
> What changes:
>
> * The `accessToken` becomes a JWT instead of a standard access token
> * You can't use this JWT for downstream **Graph API** calls

## Implementation

Use the Median JavaScript Bridge methods to trigger native login from your website. When a user clicks the button, the native login flow starts. After they authenticate, the webview redirects to your endpoint with the authentication token as a URL parameter.

Here are examples for each platform:

### Add the login button

Median JavaScript Bridge

```html Google theme={null}
<button class="google-login native-only" 
  onclick="median.socialLogin.google.login({ 
    'redirectUri' : 'https://your-domain.com/auth/google/callback', /* Your API endpoint */
    'state' : 'xyz123' /* Optional security token */
  });">
	Log in with Google
</button>
```

```javascript Facebook theme={null}
median.socialLogin.facebook.login({ 
  'redirectUri' : 'https://your-domain.com/auth/facebook/callback',
  'scope' : 'public_profile,email', // Request profile and email access
  'state' : 'xyz123'
});
```

```javascript Apple theme={null}
median.socialLogin.apple.login({ 
  'redirectUri' : 'https://your-domain.com/auth/apple/callback',
  'scope' : 'name email', // Request name and email
  'state' : 'xyz123'
});
```

### Configure your backend

Your backend server must be configured to accept the authentication token, parse it as a JWT to access the user details, and create a login session for the user.

Always verify the token on your server before trusting it. Each provider documents how:

* **Google:** [Verify the Google ID token on your server side ](https://developers.google.com/identity/gsi/web/guides/verify-google-id-token)
* **Apple:** [Authenticating users with Sign in with Apple ](https://developer.apple.com/documentation/signinwithapple/authenticating-users-with-sign-in-with-apple)
* **Facebook:** [Inspect an access token with debug\_token ](https://developers.facebook.com/docs/graph-api/reference/debug_token), or [validate a Limited Login token ](https://developers.facebook.com/documentation/facebook-login/ios/limited-login/token/validating)


## Related topics

- [Social login overview](https://docs.median.co/docs/social-login.md)
- [Google Sign-In configuration](https://docs.median.co/docs/google-sign-in-configuration.md)
- [Website configuration](https://docs.median.co/docs/social-login-website-configuration.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.