createAuth
Create the shared auth object your app uses to sign people in, track session state, and sign them out.
createAuth(config)
import { createAuth } from "@mvd/auth";Use createAuth to set up the one auth object your app will use for login and session handling.
const auth = createAuth({
clientId: "my-client-id",
authorizationEndpoint: "https://auth.example.com/authorize",
tokenEndpoint: "https://auth.example.com/token",
redirectUri: window.location.origin,
});What you get back
The returned auth object gives you the actions and state you need for the full sign-in journey.
| Member | Use it when you want to… |
|---|---|
login(options?) | start sign-in |
logout() | clear the current session |
getSnapshot() | read the current auth state outside React |
subscribe(callback) | react to auth state changes in your own integration |
Common usage patterns
Start sign-in
auth.login();Override the login style for one action
auth.login({ method: "popup" });This is useful when most of your app uses redirects, but one button should keep people on the current page.
Working in TypeScript?
You can pass a token-claim type to createAuth so tokenData is easier to use throughout your app.
const auth = createAuth<MyTokenPayload>({
clientId: "my-client-id",
authorizationEndpoint: "https://auth.example.com/authorize",
tokenEndpoint: "https://auth.example.com/token",
redirectUri: window.location.origin,
});For the full list of configuration options, see Configuration.