Skip to main content

Social sign-in buttons

Add "Continue with Google" and other providers to your own sign-in page. Each button goes straight to its provider.

RecipeDraftView source
This page is a draft. The code exists, and not all of it has been run for this page yet. The page says what was checked.
Works with
JavaScript / TypeScriptReact
Standards
RFC 7636

What it shows​

  1. The setup script adds Google as a provider of the organization and turns it on.
  2. The sign-in card of the SDK lists the same provider in socialProviders. A button of your own calls useAuth().signInWithSocial('google').
  3. Each button sends the browser to LumoAuth with provider=google. LumoAuth skips its sign-in page and goes on to Google, with PKCE.
  4. Google sends the person back to LumoAuth, which signs them in and returns to the callback of your app.
  5. AuthCallback exchanges the code, and the app shows who is signed in.

The app gets tokens from LumoAuth, never from Google. A provider is set up once for the organization, and every application can offer it.

Before you start​

  • An OAuth client at Google, of the type Web application. Its authorized redirect URI is the callback address of LumoAuth: https://app.lumoauth.dev/orgs/<your organization>/auth/social/google/callback. The setup script prints it.
  • A public application with a redirect URI, and the origin of your app in its Allowed origins. The Starter App of a new organization accepts http://localhost:5173. Sign in through the hosted page has the steps for an application of your own.

The setup script adds the provider through the Admin API. In the portal, the same form is under Integrations, Social login, Add provider. With the lumo CLI, run lumo social create --provider google --client-id … --client-secret … --enable.

VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
LUMOAUTH_API_KEYFor the setup script only: an API key with the scopes admin:social:read and admin:social:write. The app has no API key.
GOOGLE_CLIENT_IDFor the setup script only: the client ID of the OAuth client that you created at Google.
GOOGLE_CLIENT_SECRETFor the setup script only: the client secret of that OAuth client.
LUMOAUTH_CLIENT_IDThe client ID of the application. A browser app has no client secret.

The setup script adds Google as a provider of the organization and turns it on. It prints the redirect URI that the OAuth client at Google must list. A second run changes nothing.

node sign-in/social/setup.mjs
setup.mjs
// Turns on Google as a social provider of the organization, through the Admin API.
//
// node sign-in/social/setup.mjs
//
// It reads GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET, the OAuth client that you
// created at Google. The API key needs admin:social:read and admin:social:write.
import { call, ensure } from '../../_lib/admin.mjs';

const { GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET } = process.env;
if (!GOOGLE_CLIENT_ID || !GOOGLE_CLIENT_SECRET) {
console.error('Set GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET first.');
process.exit(1);
}

// Each provider type exists once in an organization, so a second run finds it.
const provider = await ensure('/admin/social-providers', (item) => item.provider === 'google', {
provider: 'google',
displayName: 'Google',
clientId: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
enabled: true,
});

// A provider that was turned off in the portal is not offered on the sign-in page.
if (!provider.enabled) await call('POST', `/admin/social-providers/${provider.id}/enable`);

// The list answers with a short form of each provider. The full form has the callback URL.
const google = (await call('GET', `/admin/social-providers/${provider.id}`)).data;
console.log(`google: ${google.enabled ? 'on' : 'off'}, offered to applications: ${google.enableForOAuthApps ? 'yes' : 'no'}`);
console.log(`Add this redirect URI to the OAuth client at Google: ${google.callbackUrl}`);

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. The recipe is a small project with its own package.json. Both buttons sent the browser through LumoAuth to Google, with PKCE. The run stopped before the Google page, because the OAuth client at Google was a placeholder.

sign-in/social/js/src/App.jsx
// The sign-in page: the card of the SDK with one social button, and a button of your own.
import { SignedIn, SignedOut, SignIn, SignOutButton, useAuth, useUser } from '@lumoauth/react';

// List the providers that you turned on for the organization. The card
// cannot read that list, so it has no default.
const providers = ['google'];

export default function App() {
const user = useUser();
const { signInWithSocial } = useAuth();
return (
<main>
<h1>Social sign-in buttons</h1>
<SignedOut>
{/* Each button of the card goes straight to its provider. A provider
that is not turned on opens the hosted sign-in page instead. */}
<SignIn socialProviders={providers} />

{/* The same call from a button of your own. */}
<p>
<button type="button" onClick={() => signInWithSocial('google')}>Google, from my own button</button>
</p>
</SignedOut>
<SignedIn>
<p>{user ? `Signed in as ${user.email}` : 'Loading the profile…'}</p>
<SignOutButton />
</SignedIn>
</main>
);
}
sign-in/social/js/src/main.jsx
// Social sign-in buttons on a sign-in page of your own.
//
// cd sign-in/social/js && npm install && npm run dev
//
// Then open http://localhost:5173.
import { createRoot } from 'react-dom/client';
import { AuthCallback, LumoAuthProvider } from '@lumoauth/react';
import App from './App.jsx';

const callbackPath = '/auth/callback';

createRoot(document.getElementById('root')).render(
<LumoAuthProvider
domain={import.meta.env.LUMOAUTH_URL}
orgId={import.meta.env.LUMOAUTH_ORG_ID}
clientId={import.meta.env.LUMOAUTH_CLIENT_ID}
redirectUri={window.location.origin + callbackPath}
>
{/* The provider sends the person back through LumoAuth, to the same callback as every sign-in. */}
{window.location.pathname === callbackPath ? <AuthCallback afterSignInUrl="/" /> : <App />}
</LumoAuthProvider>,
);
sign-in/social/js/vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

// The browser gets these three values and nothing else from the environment.
// None of them is a secret: a browser app has no client secret and no API key.
export default defineConfig({
plugins: [react()],
define: {
'import.meta.env.LUMOAUTH_URL': JSON.stringify(process.env.LUMOAUTH_URL ?? 'https://app.lumoauth.dev'),
'import.meta.env.LUMOAUTH_ORG_ID': JSON.stringify(process.env.LUMOAUTH_ORG_ID ?? ''),
'import.meta.env.LUMOAUTH_CLIENT_ID': JSON.stringify(process.env.LUMOAUTH_CLIENT_ID ?? ''),
},
});
cd sign-in/social/js && npm install && npm run dev

Compiles. Not run against an organization. The app ran against an organization up to the page of Google, and the sign-in with email ran to the end. No Google account was used, so the return from Google was not seen.

Check the result​

The setup script prints the first two lines. Open http://localhost:5173 and click Continue with Google, or the button of your own. Sign in at Google. The app then shows the last line.

Output
google: on, offered to applications: yes
Add this redirect URI to the OAuth client at Google: https://app.lumoauth.dev/orgs/acme-corp/auth/social/google/callback

Signed in as maya.chen@example.com
  • Click Sign in with email. The hosted page of LumoAuth also has a Google button.
  • Turn the provider off under Integrations, Social login, and click a button again. The hosted sign-in page opens instead of Google.

Limits​

  • The card cannot read which providers are turned on. List them yourself in socialProviders. Icons exist for google, github, microsoft and apple.
  • A first sign-in creates an account only when Google says that it verified the address.
  • An account that already has a password is not linked by email. The person signs in as usual, then links Google on the Sign-in & security page of their account.
  • An application can offer fewer providers than the organization: open the application, then the Security tab, Choose providers. A provider that the application does not offer opens the hosted sign-in page.
  • LumoAuth uses the tokens of Google once, to read the profile, and does not keep them. To call a Google API for the person, use the token vault.