Skip to content

Social Login

Overview

Social Login lets users authenticate with third-party identity providers (Google, GitHub, Apple, etc.). The SDK supports two modes:

  • Popup — Opens a popup window for the provider’s login page. The main window stays open.
  • Redirect — Redirects the entire page to the provider. Requires a callback page to complete the flow.

The simplest approach — opens a popup, completes authentication, and returns the result:

const { data, error } = await auth.social.loginWithPopup('google');
if (error) {
console.error('Social login failed:', error.message);
return;
}
console.log('User:', data.user);
console.log('Session:', data.session);

The popup is managed by the SDK. When the user completes authentication at the provider, the popup closes automatically and the promise resolves.

Supported Providers

type SocialProvider = 'google' | 'github' | 'apple' | string;

The built-in providers are google, github, and apple. Custom OIDC providers configured in the Authrim Admin panel can also be used by their string identifier.

// Check which providers are configured
const providers = auth.social.getSupportedProviders();
// ['google', 'github', 'apple']
interface SocialLoginOptions {
/** Additional OAuth scopes */
scopes?: string[];
/** Login hint (e.g., email address) */
loginHint?: string;
}
// Example: request additional scopes
await auth.social.loginWithPopup('google', {
scopes: ['profile', 'email'],
loginHint: '[email protected]',
});

Redirect Login

For environments where popups are blocked (some mobile browsers), use redirect mode:

// Step 1: Redirect to provider (this navigates away from your page)
await auth.social.loginWithRedirect('github');
// Step 2: Handle callback in your callback page (callback.html)
if (auth.social.hasCallbackParams()) {
const { data, error } = await auth.social.handleCallback();
if (!error) {
window.location.href = '/'; // Redirect to home
}
}

Callback Page Setup

Create a callback page (e.g., callback.html) that handles the provider’s redirect:

import { createAuthrim } from '@authrim/web';
const auth = await createAuthrim({
issuer: 'https://auth.example.com',
clientId: 'my-app',
});
// Check if this is a social login callback
if (auth.social.hasCallbackParams()) {
const { data, error } = await auth.social.handleCallback();
if (error) {
document.getElementById('error').textContent = error.message;
} else {
// Success — redirect to home or dashboard
window.location.href = '/';
}
} else {
// Not a social callback — might be an OAuth/SSO callback
// See Cross-Domain SSO docs for handling trySilentLogin callbacks
}

Register the callback URL (https://yourapp.com/callback.html) in the Authrim Admin panel’s client settings as an allowed redirect URI.

FeaturePopupRedirect
User experienceStays on pageNavigates away
Mobile supportMay be blockedAlways works
ImplementationSingle callTwo-step (redirect + callback)
State preservationApp state preservedApp state lost (use storage)

Error Handling

Common social login errors:

Error CodeMeaningUser Action
AR004001Provider not configuredCheck Admin panel settings
AR004002Popup blockedUse redirect mode or prompt user to allow popups
AR004003User denied consentAllow retry
AR004004Provider errorShow error message, offer retry
AR004005Callback validation failedCheck callback URL configuration
const { data, error } = await auth.social.loginWithPopup('google');
if (error) {
if (error.code === 'AR004002') {
// Popup blocked — fall back to redirect
await auth.social.loginWithRedirect('google');
} else {
showMessage(error.message);
}
}

Complete Example

import { createAuthrim } from '@authrim/web';
const auth = await createAuthrim({
issuer: 'https://auth.example.com',
clientId: 'my-app',
});
// Render available providers
const providers = auth.social.getSupportedProviders();
providers.forEach(provider => {
const button = document.createElement('button');
button.textContent = `Sign in with ${provider}`;
button.addEventListener('click', async () => {
button.disabled = true;
const { data, error } = await auth.social.loginWithPopup(provider);
if (error) {
if (error.code === 'AR004002') {
// Popup blocked — try redirect
await auth.social.loginWithRedirect(provider);
return;
}
alert(error.message);
} else {
window.location.href = '/dashboard';
}
button.disabled = false;
});
document.getElementById('social-buttons').appendChild(button);
});

Next Steps