Skip to main content

Sign in through the hosted page

Send the browser to your organization's sign-in page and come back with tokens. The app never sees a password.

RecipeReadyView source
Works with
JavaScript / TypeScriptReactVueAngular

What it shows​

  1. The app creates a PKCE verifier and sends the browser to the authorization endpoint.
  2. The person signs in on the hosted page, with the methods that the organization turned on.
  3. The callback address of the app exchanges the code for tokens and reads the profile.
  4. The app asks the SDK for the access token and calls an API with it. The SDK renews the token before it expires.
  5. Sign-out removes the tokens and ends the session at LumoAuth.

The React version uses the components of @lumoauth/react. The Vue and Angular versions use AuthModule and LumoAuthSession from @lumoauth/client, which do the same work without components.

Before you start​

You need an application for a browser app: a public client with a redirect URI and an allowed origin.

A new organization has one, the Starter App. Open Applications in the portal and copy its client ID. It accepts http://localhost:5173 and http://localhost:3000, the two addresses that this recipe uses.

For an application of your own:

  1. Open Applications and click New application.
  2. Enter an Application name and choose Single-page or mobile app.
  3. Enter the Redirect URI, for example http://localhost:5173/auth/callback, and click Create application.
  4. Open the Configuration tab of the application. Under Browser CORS origins, enter the origin of your app in Allowed origins, for example http://localhost:5173.
  5. Click Save configuration.

Without step 4 the browser blocks the token request right after sign-in.

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_CLIENT_IDThe client ID of the application. A browser app has no client secret.

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization. Each version is a small project with its own package.json. A real browser signed in through each one, called UserInfo, renewed the token and signed out.

sign-in/hosted-page/js/react/src/main.jsx
// Sign in through the hosted page of your organization, with React.
//
// cd sign-in/hosted-page/js/react && 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';

// The hosted page sends the browser back to this address with a code.
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}
>
{/* AuthCallback exchanges the code for tokens, then opens the start page. */}
{window.location.pathname === callbackPath ? <AuthCallback afterSignInUrl="/" /> : <App />}
</LumoAuthProvider>,
);
sign-in/hosted-page/js/react/src/App.jsx
// The start page: a sign-in button, or the profile of the signed-in person.
import { useState } from 'react';
import { SignedIn, SignedOut, SignInButton, SignOutButton, useLumoAuth, useSession, useUser } from '@lumoauth/react';

export default function App() {
return (
<main>
<h1>Sign in through the hosted page</h1>
<SignedOut>
<p>Nobody is signed in.</p>
{/* Creates the PKCE verifier and sends the browser to the hosted page. */}
<SignInButton />
</SignedOut>
<SignedIn>
<Profile />
<SignOutButton />
</SignedIn>
</main>
);
}

function Profile() {
const user = useUser();
const { getToken } = useSession();
const client = useLumoAuth();
const [answer, setAnswer] = useState('');

// getToken() returns the access token and renews it first when it is about
// to expire. Send it to your own API in the same way.
async function callApi() {
const token = await getToken();
const info = await client.auth.getUserInfo(token);
setAnswer(`UserInfo with the access token: sub ${info.sub}`);
}

if (!user) return <p>Loading the profile…</p>;
return (
<>
<p>Signed in as {user.email}</p>
<p>Name: {user.displayName}</p>
<button type="button" onClick={callApi}>Call the API</button>
<p>{answer}</p>
</>
);
}
sign-in/hosted-page/js/react/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/hosted-page/js/react && npm install && npm run dev

Open http://localhost:5173.

Check the result​

Click Sign in. The address bar shows the sign-in page of your organization, not a form in your app. After sign-in the app shows these lines. The third line appears when you click Call the API.

Output
Signed in as dana.reyes@example.com
Name: Dana Reyes
UserInfo with the access token: sub 01a21fb9-10a3-7e59-b71d-8cc6d9bb53ce
  • The recipe calls the UserInfo endpoint. Send the access token to your own API in the same way.
  • The audit log of the organization has a user.login event for the sign-in. Open Audit logs in the portal.
  • Click Sign out. The Starter App then stops on the page "You have been logged out" of your organization.
  • To return to your app after sign-out, add its address to the application. Open the Configuration tab, and under Logout add http://localhost:5173/ to Post-logout redirect URIs.

Limits​

  • The tokens are in sessionStorage, so each browser tab signs in for itself. Choose where tokens are stored compares the other stores.
  • The app gets a refresh token only when the grant types of the application include refresh_token. A new application has it. The scope offline_access does not change this.
  • The redirect URI is matched exactly. An address with another port or another path is refused.