Skip to content

RequireAuthController

RequireAuthController is a Lit ReactiveController that guards content behind authentication. It observes an AuthController and determines whether the user is authorized to view protected content. Use the authorized property in your render() method to conditionally display content.

Terminal window
npm install oidc-js-lit
import { LitElement, html } from "lit";
import { AuthController, RequireAuthController } from "oidc-js-lit";
class ProtectedPage extends LitElement {
private auth = new AuthController(this, {
config: {
issuer: "https://auth.example.com",
clientId: "my-app",
redirectUri: "http://localhost:3000/callback",
scopes: ["openid", "profile"],
},
});
private guard = new RequireAuthController(this, { auth: this.auth });
render() {
if (!this.guard.authorized) return html`<p>Loading...</p>`;
return html`<p>Protected content</p>`;
}
}
customElements.define("protected-page", ProtectedPage);
OptionTypeDefaultDescription
authAuthControllerrequiredThe AuthController instance to observe
autoRefreshbooleantrueAuto-refresh expired tokens before redirecting to login
loginOptionsLoginOptions-Options passed to auth.login() when redirecting
PropertyTypeDescription
authorizedbooleantrue when authenticated with a valid (non-expired) token

After each host update, RequireAuthController checks the auth state:

  1. If authenticated with a valid token, authorized is true
  2. If the token is expired and autoRefresh is true, attempts a silent refresh
  3. If refresh succeeds, authorized becomes true on the next update
  4. If refresh fails (or no refresh token), redirects to the IdP login

Unlike component-based frameworks, Lit uses the controller pattern. You can share a single AuthController across multiple elements and attach RequireAuthController only where you need guard behavior. Both controllers are registered on the same host element.