Skip to main content

A profile page in your app

Show the account of the signed-in person in your own layout, with the name, the email address and the roles.

RecipeReadyView source
Works with
JavaScript / TypeScriptReact

What it shows​

  1. A signed-out visitor opens the profile page. RedirectToSignIn sends the browser to the hosted sign-in page at once.
  2. After sign-in, the page shows the avatar, the name, the email address and its state, and the roles. Each value comes from useUser(), in the layout of your app.
  3. Below that, the UserProfile card of the SDK shows the same account, with a Sign out button.
  4. Change the roles of the person in the portal, and reload the page: the new roles appear.

useUser() holds the UserInfo response of the person. The page reads it in the browser, with no backend and no API key.

Before you start​

  • An application for a browser app, with its client ID. A new organization has the Starter App, which accepts http://localhost:5173. For an application of your own, follow the steps of Sign in through the hosted page.
  • The application must send the roles claim. A new application sends no roles until its token settings are saved once:
    1. In the portal, open Applications, then your application.
    2. Open the Security tab. Under Token settings, keep JWT (JSON Web Token) as the access token format.
    3. Under JWT claims, check that User roles is ticked.
    4. Click Save token settings.
  • A test user to sign in with, who holds a role. In the portal, open Users, then the user, and give them a role under Roles.
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. The recipe is a small project with its own package.json. A real browser opened the page signed out, went to sign-in, came back to the profile, and showed a changed role after a reload.

accounts/profile-page/js/src/ProfilePage.jsx
// The profile page of the app. A signed-out visitor goes to sign-in at
// once. A signed-in person sees their account twice: in the layout of the
// app, from useUser(), and in the UserProfile card of the SDK.
import { RedirectToSignIn, SignedIn, SignedOut, UserAvatar, UserProfile, useSession, useUser } from '@lumoauth/react';

// The roles claim holds identifiers such as ROLE_TENANT_ROLE_SUPPORT-AGENT.
// The part after this prefix is the slug of the role, in capitals.
const rolePrefix = 'ROLE_TENANT_ROLE_';

export default function ProfilePage() {
const { isLoaded } = useSession();
if (!isLoaded) return <p>Loading…</p>;

return (
<main>
<SignedOut>
{/* Starts the hosted sign-in as soon as it renders. */}
<RedirectToSignIn />
</SignedOut>
<SignedIn>
<Profile />
</SignedIn>
</main>
);
}

function Profile() {
// useUser() holds the UserInfo response of the signed-in person. It is
// null for a moment after sign-in, while the SDK reads that response.
const user = useUser();
if (!user) return <p>Loading…</p>;

const roles = user.roles
.filter((role) => role.startsWith(rolePrefix))
.map((role) => role.slice(rolePrefix.length).toLowerCase());

return (
<>
<header style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
<UserAvatar size={48} />
<h1>{user.displayName}</h1>
</header>
<dl>
<dt>Email</dt>
<dd>
{user.email} ({user.emailVerified ? 'verified' : 'not verified'})
</dd>
<dt>Roles</dt>
{/* Empty until the application sends the roles claim: see the page. */}
<dd>{roles.join(', ') || 'none'}</dd>
</dl>

<h2>Account card</h2>
{/* Read-only. Its Sign out button ends the session at LumoAuth too. */}
<UserProfile afterSignOutUrl="/" />
</>
);
}
accounts/profile-page/js/src/main.jsx
// A profile page in your own layout, with React.
//
// cd accounts/profile-page/js && npm install && npm run dev
//
// Then open http://localhost:5173.
import { createRoot } from 'react-dom/client';
import { AuthCallback, LumoAuthProvider } from '@lumoauth/react';
import ProfilePage from './ProfilePage.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}
>
{window.location.pathname === callbackPath ? <AuthCallback afterSignInUrl="/" /> : <ProfilePage />}
</LumoAuthProvider>,
);
accounts/profile-page/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 accounts/profile-page/js && npm install && npm run dev

Open http://localhost:5173.

Check the result​

Open the app. The browser goes to the sign-in page. After sign-in the profile page shows this text:

Output
Maya Chen
Email
maya@example.com (verified)
Roles
support-agent

Account card
Maya Chen
maya@example.com
ACCOUNT
Email maya@example.com Verified
ROLES & GROUPS
Roles ROLE_USER ROLE_TENANT_ROLE_SUPPORT-AGENT
Sign out
  • The roles on the page match the roles of the user under Users in the portal. The claim holds identifiers such as ROLE_TENANT_ROLE_SUPPORT-AGENT: the app shows the slug of each role, and the card shows the identifiers as they are.
  • Give the user a second role in the portal, and reload the app. The new role appears without a new sign-in.
  • Click Sign out on the card. The LumoAuth page "You have been logged out" opens, and the next visit to the app goes to the sign-in page again.

Limits​

  • The UserInfo response holds no groups and no MFA state. user.groups stays empty, user.mfaEnabled stays false, and the card shows no Groups row and no MFA row.
  • Without User roles in the saved token settings, user.roles is empty and the card shows no roles.
  • The card and the page are read-only. To let the person change their name, password or second factor, link to the hosted account pages: see Link to the account pages.
  • After sign-out, the browser returns to your app only when the address of the app is a post-logout redirect URI of the application. That setting is in the portal, under Configuration of the application.
  • Do not decide access from user.roles in the browser. Check the permission in your API: see Check a permission.