Skip to main content

Clerk

Clerk is the main entry point for the SDK. After you configure the SDK, you can access it in two ways:

Access user state

Use Clerk.userFlow to reactively observe the current user:

import com.clerk.api.Clerk

// In a ViewModel or Composable
val user by Clerk.userFlow.collectAsState(initial = null)

if (user != null) {
    Text("Welcome, ${user.firstName}")
} else {
    Text("Please sign in")
}

Access the Clerk instance

Access authentication methods through the global Clerk object:

import com.clerk.api.Clerk

// Sign in
Clerk.auth.signInWithPassword {
    identifier = "user@email.com"
    password = "secretpassword"
}

// Sign out
Clerk.auth.signOut()

Access the Active Organization state

When Organizations are enabled, the current session can have an . Use these properties to access the current Organization state and its instance settings:

  • Name
    Clerk.organization
    Type
    Organization?
    Description

    The for the current active session. Returns null when there is no active session, no selected Organization, or the active user's memberships don't include the selected Organization.

  • Name
    Clerk.organizationMembership
    Type
    OrganizationMembership?
    Description

    The active user's membership in Clerk.organization.

  • Name
    Clerk.session?.lastActiveOrganizationId
    Type
    String?
    Description

    The Organization ID selected on the current session.

  • Name
    Clerk.organizationIsEnabled
    Type
    Boolean
    Description

    Whether Organizations are enabled for the instance.

  • Name
    Clerk.organizationSelectionIsForced
    Type
    Boolean
    Description

    Whether users must choose an Organization before the session is considered complete.

  • Name
    Clerk.organizationDomainsIsEnabled
    Type
    Boolean
    Description

    Whether Verified Domains are enabled for Organizations.

  • Name
    Clerk.organizationDomainEnrollmentModes
    Type
    List<String>
    Description

    The enabled Organization domain enrollment modes.

  • Name
    Clerk.organizationCreationDefaultsIsEnabled
    Type
    Boolean
    Description

    Whether Organization creation defaults are enabled for the current user.

  • Name
    Clerk.organizationAdminDeleteIsEnabled
    Type
    Boolean
    Description

    Whether Organization admins can delete Organizations from the client.

  • Name
    Clerk.organizationSlugIsEnabled
    Type
    Boolean
    Description

    Whether Organization slugs are enabled for the instance.

val activeOrganization = Clerk.organization
val activeMembership = Clerk.organizationMembership
val activeOrganizationId = Clerk.session?.lastActiveOrganizationId

if (activeMembership?.canManageMemberships == true) {
    // Show organization membership management.
}

Switch the Active Organization

Use Clerk.auth.setActive() to switch the for a session. Pass sessionId and organizationId to select an Organization, or omit organizationId (or pass null) to select the user's . The latter isn't allowed when Clerk.organizationSelectionIsForced is true.

Select an Organization

val sessionId = Clerk.session?.id ?: return

when (
    val result = Clerk.auth.setActive(
        sessionId = sessionId,
        organizationId = "org_123",
    )
) {
    is ClerkResult.Success -> {
        val session = result.value
    }
    is ClerkResult.Failure -> {
        // Handle the failure.
    }
}
val sessionId = Clerk.session?.id ?: return

when (val result = Clerk.auth.setActive(sessionId = sessionId)) {
    is ClerkResult.Success -> {
        val personalAccountSession = result.value
    }
    is ClerkResult.Failure -> {
        // Handle the failure.
    }
}

Feedback

What did you think of this content?

Last updated on