Skip to main content

dioxus_clerk/
lib.rs

1//! Clerk authentication for [Dioxus] 0.7: components, hooks, server-function
2//! context readers, and SSR initial auth state for web (WASM) and fullstack
3//! (Axum) apps.
4//!
5//! # Quick start (web-only SPA)
6//!
7//! Mount [`ClerkProvider`] at the app root, then gate content with [`SignedIn`]
8//! / [`SignedOut`] and drop in Clerk's prebuilt widgets:
9//!
10//! ```rust,ignore
11//! use dioxus::prelude::*;
12//! use dioxus_clerk::*;
13//!
14//! fn App() -> Element {
15//!     rsx! {
16//!         ClerkProvider { publishable_key: "pk_test_...",
17//!             SignedOut { SignInButton { class: "btn" } }
18//!             SignedIn { UserButton {} }
19//!         }
20//!     }
21//! }
22//! ```
23//!
24//! # Fullstack (Axum)
25//!
26//! Enable the `server` feature on the native build to verify Clerk sessions in
27//! `#[server]` functions with `current_auth()`, installed by the
28//! `ClerkAuthLayer` tower middleware. SSR initial auth state lets the client
29//! hydrate without a flash of unauthenticated content.
30//!
31//! ```rust,ignore
32//! #[server]
33//! async fn whoami() -> Result<String, ServerFnError> {
34//!     use dioxus_clerk::server::current_auth;
35//!     Ok(current_auth()?.user_id)
36//! }
37//! ```
38//!
39//! # Reactive hooks
40//!
41//! [`use_auth`], [`use_user`], and [`use_session`] expose reactive auth state to
42//! any descendant of [`ClerkProvider`]; [`use_clerk`] returns a lifecycle-aware
43//! action facade for imperative flows.
44//!
45//! # Feature flags
46//!
47//! | Feature | Default | Enables |
48//! | --- | --- | --- |
49//! | *(none)* | ✅ | Client components, hooks, guards, Clerk widgets, and SSR consumption. |
50//! | `server` | | Axum middleware, extractors, `#[server]` context readers, and SSR initial-state helpers. Enable on the native server build only. |
51//! | `worker` | | `server` plus `Send`-wrapped middleware futures for single-threaded Cloudflare Workers. |
52//! | `testing` | | Test helpers (`TestClerk`) that mint Clerk-shaped session tokens locally. Enable under `[dev-dependencies]` only. |
53//!
54//! [Dioxus]: https://dioxuslabs.com
55
56#![forbid(unsafe_code)]
57#![warn(missing_docs)]
58// docs.rs passes --cfg docsrs (see [package.metadata.docs.rs]) and builds on
59// nightly, where doc_cfg renders feature-requirement badges.
60#![cfg_attr(docsrs, feature(doc_cfg))]
61
62pub mod components;
63pub mod core;
64pub mod hooks;
65pub mod options;
66pub mod prelude;
67pub mod ssr;
68
69#[cfg(feature = "server")]
70#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
71pub mod server;
72
73#[cfg(feature = "testing")]
74#[cfg_attr(docsrs, doc(cfg(feature = "testing")))]
75pub mod testing;
76
77// Curated crate-root re-exports. These lists are explicit (rather than glob
78// re-exporting the modules) so the stable surface is reviewable and a new
79// `pub` item in a module does not silently land at the crate root. Advanced
80// types (`ClerkAuth`, `VerificationOutcome`, `InvalidTokenReason`) stay under
81// `crate::core`; `crate::prelude` re-exports the everyday subset.
82pub use components::{
83    AuthButtonMode, ClerkFailed, ClerkLoaded, ClerkLoading, ClerkProvider, CreateOrganization,
84    OrganizationList, OrganizationProfile, OrganizationSwitcher, Protect, RedirectToSignIn,
85    RedirectToSignUp, SignIn, SignInButton, SignOutButton, SignUp, SignUpButton, SignedIn,
86    SignedInWhenLoaded, SignedOut, SignedOutWhenLoaded, TaskSetupMFA, UserAvatar, UserButton,
87    UserProfile, Waitlist,
88};
89pub use core::{
90    AuthRequirement, AuthState, AuthStatus, ClerkError, OtherReverificationLevel, OtherStatus,
91    OtherTaskKey, ReverificationLevel, Session, SessionStatus, SessionTask, SessionTaskKey, User,
92};
93pub use hooks::{
94    ClerkActions, SessionState, UseAuth, UseAuthOptions, UseSession, UseUser, UserState, use_auth,
95    use_auth_with_options, use_clear_clerk_error, use_clerk, use_clerk_error, use_session,
96    use_user,
97};
98pub use options::{
99    ClerkOptions, CreateOrganizationOptions, GetTokenOptions, JsonOptions, OrganizationListOptions,
100    OrganizationProfileOptions, OrganizationSwitcherOptions, RedirectOptions, Routing,
101    SignInOptions, SignOutOptions, SignUpOptions, TaskSetupMFAOptions, UserButtonOptions,
102    UserProfileMode, UserProfileOptions, WaitlistOptions,
103};
104pub use reverification::{UseReverification, use_reverification};
105
106/// Re-export of the exact [`serde_json`] this crate builds against.
107///
108/// [`serde_json::Value`] appears in the public prop surface of the Clerk widget
109/// components (the `options`/`appearance`/`localization`/… escape hatches) and
110/// in the `impl Into<serde_json::Value>` option arguments on the hooks, so
111/// `serde_json` is part of this crate's semver contract. Build your option
112/// values through this re-export to stay in lockstep with the version the
113/// components deserialize with.
114pub use serde_json;
115
116// Client-side browser modules. `clerk_client` (emitted by build.rs) means
117// wasm32 without the `worker` feature. The gate excludes `worker` rather than
118// `server` so features stay additive: a browser-wasm build keeps the client
119// path even when the `server` feature unifies in; only the explicit `worker`
120// opt-in (server-on-wasm) drops it.
121/// Reset page-scoped `Clerk.load()` state between wasm integration tests.
122///
123/// Hidden test-support hook: a mock whose load promise never settles (e.g. a
124/// pending-load fixture) leaves the in-flight flag set, which would make the
125/// next test's provider block in the load-in-flight wait loop. Real clerk-js
126/// always settles, so this never matters outside tests.
127///
128/// Not part of the public API. The `__` prefix and `#[doc(hidden)]` mark it as
129/// an internal hook the crate's own (external) wasm integration tests reach;
130/// it is excluded from this crate's semver guarantees and may change or be
131/// removed at any time.
132#[doc(hidden)]
133#[cfg(clerk_client)]
134pub fn __reset_load_state() {
135    crate::bridge::reset_load_state();
136}
137
138mod actions;
139#[cfg(clerk_client)]
140mod bindings;
141#[cfg(clerk_client)]
142mod bridge;
143mod context;
144#[cfg(clerk_client)]
145mod handle;
146#[cfg(clerk_client)]
147mod lifecycle;
148#[cfg(clerk_client)]
149mod loader;
150mod reverification;
151// Pure publishable-key decoding for the client loader. Compiled under `test`
152// too so its logic is covered by host `cargo test`, not only the CI-only wasm
153// suite.
154#[cfg(any(clerk_client, test))]
155mod publishable_key;
156#[cfg(clerk_client)]
157mod ssr_document;
158mod startup;
159
160/// Long-form guides, rendered from the Markdown sources in the repository's
161/// `docs/` directory.
162///
163/// These are `#[cfg(doc)]`-only: they exist to put the guides on docs.rs and in
164/// local `cargo doc` output, versioned with the release they describe, so they
165/// are reachable without leaving the API documentation.
166#[cfg(doc)]
167pub mod guides {
168    /// Setting up an application's test suite against Clerk — offline token
169    /// minting, SSR tests, and Playwright configuration.
170    #[doc = include_str!("../docs/testing.md")]
171    pub mod testing {}
172}