Get Started

Get Started with Passage for iOS

If you haven't already, you'll need to add Passage to your iOS app.

Check current auth state

Declare an instance of Passage.

let passage = PassageAuth()

Check to see if the user is currently signed in.

override func viewDidLoad() {
    super.viewDidLoad()
    Task {
        guard let currentUser = await passage.getCurrentUser() else {
            // Ask user to log in
            return
        }
        // Do authenticated stuff
    }
}

Register new user

The Passage class comes with a convenient, simplified register method that will attempt to:

  • Prompt the user to create a passkey

  • On successful passkey creation, method will create a new user account and return an AuthResult that contains the user’s tokens

  • On a user cancellation or failure of passkey creation, method will use your Passage app’s fallback method (one-time passcode or magic link) and return an AuthFallbackResult that contains an id used for verification.

func registerNewUser(identifier: String) async {
    do {
        let result = try await passage.register(identifier: identifier)
        if let token = result.authResult?.authToken {
            // Account and passkey creation successful! Do authenticated stuff.
        } else if let authFallbackResult = result.authFallbackResult {
            handleAuthFallbackResult(result: authFallbackResult)
        }
    } catch {
        // Handle errors
    }
}

func handleAuthFallbackResult(authFallbackResult: AuthFallbackResult) {
    if let magicLink = authFallbackResult as? MagicLink {
        // User was sent a magic link. Navigate to a waiting screen.
        pushMagicLinkViewController(magicLinkId: magicLink.id)
    } else if let oneTimePasscode = authFallbackResult as? OneTimePasscode {
        // User was sent a one time passcode. Navigate to passcode input screen.
        pushOTPViewController(otpId: oneTimePasscode.id)
    }
}

Log in existing user

The Passage class comes with a simplified login method that will attempt to:

  • Prompt the user to login with a passkey

  • On successful passkey login, method will return an AuthResult that contains the user’s tokens

  • On a user cancellation or failure of passkey login, method will use your Passage app’s fallback method (one-time passcode or magic link) and return an AuthFallbackResult that contains an id used for verification.

func loginExistingUser(identifier: String) async {
    do {
        let result = try await passage.register(identifier: identifier)
        if let token = result.authResult?.authToken {
            // Account and passkey creation successful! Do authenticated stuff.
        } else if let authFallbackResult = result.authFallbackResult {
            handleAuthFallbackResult(authFallbackResult: authFallbackResult)
        }
    } catch {
        // Handle errors
    }
}

Want more? 🤩

These simplified register and login methods do a lot of heavy lifting for you, but you might want more customized behavior or visibility into the process. If so, jump into our technical docs.

Last updated

Change request #337: react native