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 Clerk through SwiftUI

After you inject Clerk.shared into the environment, you can access it with @Environment(Clerk.self).

import ClerkKit
import SwiftUI

struct ContentView: View {
  @Environment(Clerk.self) private var clerk

  var body: some View {
    if clerk.user != nil {
      Text("Signed in")
    } else {
      Text("Signed out")
    }
  }
}

Access Clerk through the shared instance

If you're not using SwiftUI, you can always access Clerk through the shared instance.

let clerk = Clerk.shared

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 nil 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.environment?.organizationSettings.enabled
    Type
    Bool?
    Description

    Whether Organizations are enabled for the instance.

  • Name
    clerk.environment?.organizationSettings.forceOrganizationSelection
    Type
    Bool?
    Description

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

  • Name
    clerk.environment?.organizationSettings.domains.enabled
    Type
    Bool?
    Description

    Whether Verified Domains are enabled for Organizations.

  • Name
    clerk.environment?.organizationSettings.domains.enrollmentModes
    Type
    [String]?
    Description

    The enabled Organization domain enrollment modes.

  • Name
    clerk.environment?.organizationSettings.organizationCreationDefaults.enabled
    Type
    Bool?
    Description

    Whether Organization creation defaults are enabled for the current user.

  • Name
    clerk.environment?.organizationSettings.actions.adminDelete
    Type
    Bool?
    Description

    Whether Organization admins can delete Organizations from the client.

  • Name
    clerk.environment?.organizationSettings.slug.disabled
    Type
    Bool?
    Description

    Whether Organization slugs are disabled for the instance.

let activeOrganization = clerk.organization
let activeMembership = clerk.organizationMembership
let 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 nil) to select the user's . The latter isn't allowed when clerk.environment?.organizationSettings.forceOrganizationSelection is true.

Select an Organization

guard let sessionId = clerk.session?.id else { return }

try await clerk.auth.setActive(
  sessionId: sessionId,
  organizationId: "org_123"
)
guard let sessionId = clerk.session?.id else { return }

try await clerk.auth.setActive(sessionId: sessionId)

Feedback

What did you think of this content?

Last updated on