Open your web app signed in from a native app
When a signed-in user of your native app opens a page of your web app, the page normally loads signed out, and the user has to sign in again. Native to Web SSO signs them in for you. Clerk creates a one-time sign-in link for the page, and the web view or browser opens it. The page then loads with the same user signed in.
Choose how to open the page
The Clerk iOS SDK has three methods for this on clerk.auth:
loadInWebView()loads the page in yourWKWebView. Use it for web pages that are part of your app's screens, such as an account page.openInBrowser()opens the page in the user's default browser. Use it to send users to your full web app. On macOS, it requires Clerk iOS SDK1.7.4or later.prepareSessionTransfer()creates the link and leaves opening it to you. Use it only when neither of the other two fits.
Before you start
- In the Clerk Dashboard, navigate to the Native applications page.
- Enable the Native API, if it isn't already.
- Turn on Native to Web SSO. Until you do, Clerk can't create sign-in links and returns the
feature_not_enablederror code.
Then check the following requirements:
- Your app uses Clerk iOS SDK
1.7.3or later. The methods are inClerkKitand aren't available on tvOS or watchOS. For more information, see Install the SDK. - Your web app uses Clerk with the same instance as your native app.
- The
destinationyou open is an absolute URL. Outside development instances, it must use HTTPS. In production, it must share a root domain with one of your Clerk instance's domains. - The user is signed in to your native app. Sessions with pending session tasks can't be transferred.
Show a web page in a web view
Call loadInWebView() with your WKWebView and the page to open. The web view first loads a Clerk page that signs the user in, and then redirects to destination. The method returns once that load starts, before the web page signs the user in.
The method doesn't change your web view's delegates. To detect a failed sign-in, check each navigation in your WKNavigationDelegate for the __clerk_status query parameter.
import ClerkKit
import UIKit
import WebKit
final class AccountViewController: UIViewController, WKNavigationDelegate {
private let webView = WKWebView()
override func loadView() {
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
Task {
do {
try await Clerk.shared.auth.loadInWebView(
webView,
destination: URL(string: "https://app.example.com/account")!
)
} catch {
showError(error)
}
}
}
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction
) async -> WKNavigationActionPolicy {
let queryItems = navigationAction.request.url
.flatMap { URLComponents(url: $0, resolvingAgainstBaseURL: false) }?
.queryItems ?? []
if queryItems.contains(where: { $0.name == "__clerk_status" && $0.value == "session_transfer_failed" }) {
showSignInFailed(code: queryItems.first { $0.name == "__clerk_error_code" }?.value)
}
return .allow
}
private func showError(_ error: Error) {
// Show your own error view in place of the web view.
}
private func showSignInFailed(code: String?) {
// The page isn't signed in as the app's user. Show your own error view.
}
}Clear the web view's data when the user changes
The web view keeps its web session in its WKWebsiteDataStore after the user signs out of your app. Clerk usually ends the web session along with the app's session, but not in every case. When the web view still holds the previous user's web session, the sign-in fails with the other_user_signed_in error code.
To start each user with a clean web view, clear its data store when the user signs out or a different user signs in:
await WKWebsiteDataStore.default().removeData(
ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(),
modifiedSince: .distantPast
)Every web view that doesn't set its own data store shares the default one, so clearing it can sign users out of your app's other web views.
Or give the web view a WKWebsiteDataStore.nonPersistent() data store, which keeps its data in memory only and discards it with the web view.
Open a web page in the browser
Call openInBrowser() with the page to open:
import ClerkKit
import SwiftUI
struct OpenOnWebButton: View {
@Environment(Clerk.self) private var clerk
@State private var errorMessage: String?
var body: some View {
VStack {
Button("Open on web") {
Task { await openOnWeb() }
}
if let errorMessage {
Text(errorMessage)
}
}
}
private func openOnWeb() async {
do {
try await clerk.auth.openInBrowser(
destination: URL(string: "https://app.example.com/settings")!
)
} catch {
errorMessage = error.localizedDescription
}
}
}Handle a failed sign-in on the web
openInBrowser() returns once the system hands the link to the browser, before the web page signs the user in. If the sign-in fails, your app isn't notified.
The browser lands on destination, or on the Home URL set in the Clerk Dashboard if the link isn't valid. The page usually loads signed out. For other_user_signed_in and impersonation_in_progress, Clerk leaves the browser's sessions as they were, so the page stays signed in as the other user or in the impersonation session. Either page gets the following query parameters:
__clerk_statusissession_transfer_failed.__clerk_error_codeis the reason, such asexpiredorother_user_signed_in.
Check for these parameters on your web page to show the user a message or a sign-in prompt. For the possible codes, see the error codes.
Open the link yourself
prepareSessionTransfer() returns a SessionTransfer with the link in url and its expiration in expiresAt. Set presentation to .webView for a web view that your app renders, or .browser for a browser. Each call creates a new link.
let transfer = try await clerk.auth.prepareSessionTransfer(
destination: URL(string: "https://app.example.com/reports")!,
presentation: .webView
)
webView.load(URLRequest(url: transfer.url))Use openInBrowser() rather than SFSafariViewController to open the link in a browser. SFSafariViewController keeps its cookies separate from the user's browser, so the user is signed in only inside it.
Sign-out
When the app's session ends, Clerk also ends the web sessions that its links created. This happens when the user signs out of your app or when you revoke the app's session.
Some web sessions stay signed in:
- If the browser was already signed in as the same user, the link may keep that existing session. Ending the app's session doesn't end it.
- When a new sign-in replaces the app's session, such as in single-session mode or with hosted authentication, Clerk doesn't end the web sessions. They still expire when the app's session would have.
Keep the link secret
The sign-in link signs in anyone who opens it. It works once, within 60 seconds.
openInBrowser()never exposes the link.loadInWebView()loads the link in your web view, so yourWKNavigationDelegatesees it. Don't log or store the URLs that it receives.- If you use
prepareSessionTransfer(), openurlonce and don't log or store it.SessionTransferleaves the link out of its descriptions, so printing the transfer doesn't reveal it.
Handle errors
The methods throw the following errors:
- A
ClerkClientErrorbefore any network request, when the user isn't signed in ordestinationisn't an absolute HTTP or HTTPS URL. - A
ClerkAPIErrorwhen Clerk refuses to create the link. Itscodeis the first error code that Clerk returned. For example,feature_not_enabledmeans Native to Web SSO is turned off on the Clerk Dashboard's Native applications page. - A
ClerkClientErrorwhen the active session changes while Clerk creates the link, when Clerk's response isn't valid, or when no browser can open the link.
For more information, see Error handling.
Error codes
When the sign-in on the web fails, __clerk_error_code is one of the following codes:
Feedback
Last updated on