Open your web app signed in from a native app
When a signed-in user of your native application 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 WebView or browser opens it. The page then loads with the same user signed in.
Choose how to open the page
The Clerk Android SDK has three methods for this on Clerk.auth. Each is a suspend function that returns a ClerkResult.
loadInWebView(webView, destination)loads the page in yourWebView. Use it for web pages that are part of your app's screens, such as an account page.openInBrowser(destination)opens the page in the user's default browser. Use it to send users to your full web app.prepareSessionTransfer(destination, presentation)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 Android SDK
1.5.0or later. 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 WebView
Call loadInWebView() with your WebView and the page to open. The WebView first loads a Clerk page that signs the user in, and then redirects to destination. The method succeeds once that load starts, before the web page signs the user in.
The method doesn't change your WebView, so set it up first:
- Set
settings.javaScriptEnabledtotrue. Clerk's sign-in page runs a script. - Set a
WebViewClient. Without one, the WebView hands the redirect todestinationto the system browser, and the page opens outside your app.
To detect a failed sign-in, check each URL in WebViewClient.shouldOverrideUrlLoading() for the __clerk_status query parameter. onPageFinished() misses it when destination redirects again, such as to its own sign-in page.
import android.annotation.SuppressLint
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.viewinterop.AndroidView
import com.clerk.api.Clerk
import com.clerk.api.network.serialization.errorMessage
import com.clerk.api.network.serialization.onFailure
import kotlinx.coroutines.launch
@SuppressLint("SetJavaScriptEnabled")
@Composable
fun AccountWebView() {
val scope = rememberCoroutineScope()
var errorMessage by remember { mutableStateOf<String?>(null) }
val message = errorMessage
if (message != null) {
Text(message)
return
}
AndroidView(
factory = { context ->
WebView(context).apply {
settings.javaScriptEnabled = true
webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
val url = request.url
if (url.isHierarchical && url.getQueryParameter("__clerk_status") == "session_transfer_failed") {
errorMessage = "Sign-in failed: ${url.getQueryParameter("__clerk_error_code")}"
}
return false
}
}
scope.launch {
Clerk.auth.loadInWebView(this@apply, "https://app.example.com/account")
.onFailure { errorMessage = it.errorMessage }
}
}
},
)
}Clear the WebView's cookies when the user changes
The WebView keeps its cookies after the user signs out of your app. Clerk usually ends the web session along with the app's session, except in the cases listed under Sign-out. When the WebView 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 WebView, clear the cookies when the user signs out or a different user signs in. Android WebViews share one cookie store, so this signs out every WebView in your app. Wait for the callback before you load the WebView for the next user.
CookieManager.getInstance().removeAllCookies {
CookieManager.getInstance().flush()
}Open a web page in the browser
Call openInBrowser() with the page to open:
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import com.clerk.api.Clerk
import com.clerk.api.network.serialization.errorMessage
import com.clerk.api.network.serialization.onFailure
import kotlinx.coroutines.launch
@Composable
fun OpenOnWebButton() {
val scope = rememberCoroutineScope()
var errorMessage by remember { mutableStateOf<String?>(null) }
Column {
Button(
onClick = {
scope.launch {
Clerk.auth.openInBrowser("https://app.example.com/settings")
.onFailure { errorMessage = it.errorMessage }
}
},
) {
Text("Open on web")
}
errorMessage?.let { Text(it) }
}
}The SDK's intent matches only browsers, so the link opens in a browser even when your app has a verified App Link for your Frontend API host.
Handle a failed sign-in on the web
openInBrowser() succeeds once it asks the browser to open the link, 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 web sign-in error codes.
Open the link yourself
prepareSessionTransfer() returns a SessionTransfer with the link in url and its expiration in expiresAt, in milliseconds since the Unix epoch. Set presentation to SessionTransferPresentation.WEB_VIEW for a WebView that your app renders, or SessionTransferPresentation.BROWSER for a browser. Each call creates a new link.
To load the link in your WebView:
scope.launch {
Clerk.auth.prepareSessionTransfer("https://app.example.com/reports", SessionTransferPresentation.WEB_VIEW)
.onSuccess { transfer -> webView.loadUrl(transfer.url.toString()) }
.onFailure { errorMessage = it.errorMessage }
}To open the link in a browser, set Intent.EXTRA_REFERRER to your app's android-app:// URI. Otherwise the sign-in fails with the cross_site_request error code. Also set a selector that only browsers match, as openInBrowser() does, so that an App Link can't open the link in your app:
scope.launch {
Clerk.auth.prepareSessionTransfer("https://app.example.com/reports", SessionTransferPresentation.BROWSER)
.onSuccess { transfer ->
val intent = Intent(Intent.ACTION_VIEW, transfer.url).apply {
addCategory(Intent.CATEGORY_BROWSABLE)
putExtra(Intent.EXTRA_REFERRER, Uri.parse("android-app://${context.packageName}"))
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
selector = Intent(Intent.ACTION_VIEW, Uri.parse("https://")).addCategory(Intent.CATEGORY_BROWSABLE)
}
try {
context.startActivity(intent)
} catch (e: ActivityNotFoundException) {
errorMessage = "No browser could open the link."
}
}
.onFailure { errorMessage = it.errorMessage }
}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 yourWebView, so yourWebViewClientcallbacks receive it, such as theurlinonPageStarted(). Don't log or store the URLs they receive.- If you use
prepareSessionTransfer(), openurlonce and don't log or store it.SessionTransfer.toString()leaves the link out, so printing the transfer doesn't reveal it.
Handle errors
When a method fails, read the error code from the failure:
.onFailure { failure ->
when (failure.error?.errors?.firstOrNull()?.code) {
"signed_out" -> navigateToSignIn()
else -> showError(failure.errorMessage)
}
}The SDK sets the following codes before or while opening the link:
When Clerk refuses to create the link, the code is the first one that Clerk returned. For example, feature_not_enabled means Native to Web SSO is turned off on the Clerk Dashboard's Native applications page. Network failures, unexpected responses, and calls to openInBrowser() before Clerk is initialized have no code.
For more information, see Error handling.
Web sign-in error codes
When the sign-in on the web fails, __clerk_error_code is one of the following codes:
Feedback
Last updated on