#[non_exhaustive]pub struct AuthState { /* private fields */ }Expand description
App-visible authentication state for components and hooks.
status is the single source of truth for the
signed-in answer; signed-in checks derive from it so contradictory states
cannot be represented. The fields are private so status and user_id
cannot be mutated out of step with each other; read them through the
accessors (status, user_id,
…) and build values through the constructors and with_* setters. SSR
initial state carries crate::ssr::InitialAuthSnapshot instead. Browser
loadedness is client lifecycle state, so server-rendered auth snapshots do
not set is_loaded.
Implementations§
Source§impl AuthState
impl AuthState
Sourcepub fn signed_out() -> Self
pub fn signed_out() -> Self
Auth state representing a resolved signed-out session.
is_loaded stays false: browser loadedness is client lifecycle
state, not part of the auth answer. Set it explicitly when modeling a
client where clerk-js has finished loading.
Sourcepub fn signed_in(user_id: impl Into<String>) -> Self
pub fn signed_in(user_id: impl Into<String>) -> Self
Auth state representing a signed-in session for the given user id.
Chain the with_* setters to add optional session/org fields (and
is_loaded).
An empty user id cannot represent a signed-in session and produces
AuthState::signed_out, matching From<&ClerkAuth>.
Sourcepub fn with_loaded(self, is_loaded: bool) -> Self
pub fn with_loaded(self, is_loaded: bool) -> Self
Set whether clerk-js has finished loading on the client.
Sourcepub fn with_session_id(self, session_id: impl Into<String>) -> Self
pub fn with_session_id(self, session_id: impl Into<String>) -> Self
Set the active session id.
Sourcepub fn with_org_id(self, org_id: impl Into<String>) -> Self
pub fn with_org_id(self, org_id: impl Into<String>) -> Self
Set the active organization id.
Sourcepub fn with_org_slug(self, org_slug: impl Into<String>) -> Self
pub fn with_org_slug(self, org_slug: impl Into<String>) -> Self
Set the active organization slug.
Sourcepub fn with_org_role(self, org_role: impl Into<String>) -> Self
pub fn with_org_role(self, org_role: impl Into<String>) -> Self
Set the organization role from verified server auth.
Sourcepub fn with_org_permissions(
self,
org_permissions: impl IntoIterator<Item = impl Into<String>>,
) -> Self
pub fn with_org_permissions( self, org_permissions: impl IntoIterator<Item = impl Into<String>>, ) -> Self
Set the organization permissions from verified server auth.
Sourcepub fn status(&self) -> AuthStatus
pub fn status(&self) -> AuthStatus
Explicit auth resolution status: the single source of truth for the signed-in answer.
Sourcepub fn session_id(&self) -> Option<&str>
pub fn session_id(&self) -> Option<&str>
Active session id, if any.
Sourcepub fn org_permissions(&self) -> &[String]
pub fn org_permissions(&self) -> &[String]
Org permissions from verified server auth.
Sourcepub fn is_signed_in(&self) -> bool
pub fn is_signed_in(&self) -> bool
True when a signed-in session is known.
Sourcepub fn is_loading(&self) -> bool
pub fn is_loading(&self) -> bool
True while auth has not resolved to signed-in or signed-out yet.
Sourcepub fn is_signed_out(&self) -> bool
pub fn is_signed_out(&self) -> bool
True only when auth has resolved and no active session is known;
false while auth is still loading.
Sourcepub fn require_signed_in(&self) -> Result<&str, ClerkError>
pub fn require_signed_in(&self) -> Result<&str, ClerkError>
Return the user id for signed-in auth state.
Preserves the loading/signed-out distinction the rest of the API keeps:
ClerkError::NotLoaded while auth has not resolved yet (retry or
wait), ClerkError::Unauthenticated only once auth has resolved
without a signed-in user (redirect to sign-in).
Sourcepub fn has_role(&self, role: &str) -> bool
pub fn has_role(&self, role: &str) -> bool
True if the auth state includes the given server-verified org role.
Sourcepub fn has_permission(&self, permission: &str) -> bool
pub fn has_permission(&self, permission: &str) -> bool
True if the auth state includes the given server-verified org permission.
Sourcepub fn has(&self, requirement: &AuthRequirement) -> bool
pub fn has(&self, requirement: &AuthRequirement) -> bool
True if the auth state satisfies a rendering auth requirement.
Role and permission requirements imply a signed-in session; they are never satisfied by a loading or signed-out state.
Trait Implementations§
impl Eq for AuthState
Source§impl From<&ClerkAuth> for AuthState
Converts server-verified claims into app-visible auth state, cloning the
claim fields. See From<ClerkAuth>.
impl From<&ClerkAuth> for AuthState
Converts server-verified claims into app-visible auth state, cloning the
claim fields. See From<ClerkAuth>.
Source§impl From<ClerkAuth> for AuthState
Converts server-verified claims into app-visible auth state.
impl From<ClerkAuth> for AuthState
Converts server-verified claims into app-visible auth state.
is_loaded stays false: these claims come from the server, where
clerk-js does not exist, so browser loadedness cannot be inferred.