JavaScript SDK
@authon/js — Universal browser SDK that works in any JavaScript environment. Renders a drop-in login modal via ShadowDOM with full appearance customization.
Installation
npm install @authon/jsOr load from a CDN for quick prototyping:
<script type="module">
import { Authon } from "https://cdn.jsdelivr.net/npm/@authon/js/+esm";
</script>Initialization
Create an Authon instance with your publishable key. Find your key in the Dashboard under API Keys.
import { Authon } from "@authon/js";
const authon = new Authon("pk_live_your_publishable_key", {
// Optional — override the API base URL (default: https://api.authon.dev)
apiUrl: "https:1
2
mode: "popup",
3
theme: "auto",
4
locale: "en",
});Open Login Modal
Call openSignIn() or openSignUp() to show the Authon modal. The modal is rendered into a ShadowDOM root so it never conflicts with your app styles.
// Open the sign-in modal
await authon.openSignIn();
// Open the sign-up modal (registration flow)
await authon.openSignUp();Both methods are async — they fetch your project branding and OAuth provider list from the API before rendering, so the modal always reflects your dashboard settings.
Email Authentication
For fully programmatic flows (e.g., your own custom UI), call the email methods directly:
import type { AuthonUser } from "@authon/js";
// Sign in an existing user
const user: AuthonUser = await authon.signInWithEmail(
"user@example.com",
"securepassword",
);
// Register a new user
const user: AuthonUser = await authon.signUpWithEmail(
"newuser@example.com",
"securepassword",
{ displayName: "Jane Doe" },
);OAuth Sign In
Trigger an OAuth popup for any provider configured in your dashboard. The popup will handle the PKCE flow and post a message back to your window on completion.
// Supported providers: google | apple | github | kakao | naver |
// facebook | discord | x | line | microsoft
await authon.signInWithOAuth("google");
await authon.signInWithOAuth("github");
await authon.signInWithOAuth("kakao");User & Session
// Get the current signed-in user (returns null if not signed in)
const user = authon.getUser();
// {
// id: "usr_abc123",
// email: "user@example.com",
// displayName: "Jane Doe",
// avatarUrl: "https://...",
// emailVerified: true,
// isBanned: false,
// publicMetadata: {},
// createdAt: "2026-01-01T00:00:00Z",
// }
// Get the raw JWT access token for API calls
const token = authon.getToken();
// Sign out the current user and clear local session
await authon.signOut();Events
Subscribe to authentication lifecycle events with authon.on(event, callback). The method returns an unsubscribe function.
// Fired when a user successfully signs in or the session is restored
const unsubSignedIn = authon.on("signedIn", (user) => {
console.log("Signed in:", user.email);
router.push("/dashboard");
});
// Fired when the user signs out or the session expires
const unsubSignedOut = authon.on("signedOut", () => {
console.log("Signed out");
router.push("/");
});
// Fired when a token is auto-refreshed
authon.on("tokenRefreshed", (token) => {
console.log("Token refreshed:", token.slice(0, 16) + "...");
});
// Fired on any API or auth error
authon.on("error", (error) => {
console.error("Auth error:", error.message);
});
// Unsubscribe when needed
unsubSignedIn();
unsubSignedOut();Appearance Customization
Override branding programmatically via the appearance config. These settings merge with the branding you set in the Dashboard.
const authon = new Authon("pk_live_your_key", {
appearance: {
brandName: "Acme Corp",
primaryColorStart: "#7c3aed",
primaryColorEnd: "#4f46e5",
darkBg: "#0f172a",
darkText: "#f1f5f9",
borderRadius: 12,
showEmailPassword: true,
showDivider: true,
termsUrl: "https:0
privacyUrl: "https://acme.com/privacy",
},
});Cleanup
Calldestroy() to remove event listeners, close the modal, and clear session data when your app unmounts.
// In a SPA — call on route change or component unmount
authon.destroy();Full Example
import { Authon } from "@authon/js";
const authon = new Authon("pk_live_your_key", {
mode: "popup",
theme: "dark",
appearance: {
brandName: "My App",
primaryColorStart: "#7c3aed",
primaryColorEnd: "#4f46e5",
},
});
// Event listeners
authon.on("signedIn", (user) => {
userNameEl.textContent = user.displayName ?? user.email ?? "";
authSection.style.display = "block";
loginBtn.style.display = "none";
});
authon.on("signedOut", () => {
authSection.style.display = "none";
loginBtn.style.display = "block";
});
authon.on("error", (err) => {
console.error("Authon error:", err.message);
});
// Wire up buttons
loginBtn.addEventListener("click", () => authon.openSignIn());
logoutBtn.addEventListener("click", () => authon.signOut());
// Check if user is already signed in on page load
const user = authon.getUser();
if (user) {
userNameEl.textContent = user.displayName ?? user.email ?? "";
authSection.style.display = "block";
loginBtn.style.display = "none";
}