Skip to content

Commit 8388594

Browse files
authored
fix: update user agent to support desktop/mobile modes [#166] (#168)
1 parent 64ccec2 commit 8388594

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

packages/clerk_auth/lib/src/clerk_constants.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ sealed class ClerkConstants {
1616

1717
/// The url used to catch oauth redirects
1818
static const oauthRedirect = 'com.clerk.flutter://callback';
19+
20+
/// The user agent to use for oauth
21+
static const userAgent = 'ClerkFlutterSDK/$flutterSdkVersion';
1922
}

packages/clerk_flutter/lib/src/clerk_auth_state.dart

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class ClerkAuthState extends clerk.Auth with ChangeNotifier {
9797
routeSettings: const RouteSettings(name: _kSsoRouteName),
9898
builder: (BuildContext context) {
9999
return _SsoWebViewOverlay(
100+
strategy: strategy,
100101
url: url,
101102
oauthRedirect: clerk.ClerkConstants.oauthRedirect,
102103
onError: (error) => _onError(error, onError),
@@ -138,6 +139,7 @@ class ClerkAuthState extends clerk.Auth with ChangeNotifier {
138139
useRootNavigator: true,
139140
routeSettings: const RouteSettings(name: _kSsoRouteName),
140141
builder: (context) => _SsoWebViewOverlay(
142+
strategy: strategy,
141143
url: url,
142144
oauthRedirect: clerk.ClerkConstants.oauthRedirect,
143145
onError: (error) => _onError(error, onError),
@@ -265,11 +267,13 @@ class ClerkAuthState extends clerk.Auth with ChangeNotifier {
265267

266268
class _SsoWebViewOverlay extends StatefulWidget {
267269
const _SsoWebViewOverlay({
270+
required this.strategy,
268271
required this.url,
269272
required this.oauthRedirect,
270273
required this.onError,
271274
});
272275

276+
final clerk.Strategy strategy;
273277
final String url;
274278
final String oauthRedirect;
275279
final ClerkErrorCallback onError;
@@ -287,9 +291,6 @@ class _SsoWebViewOverlayState extends State<_SsoWebViewOverlay> {
287291
super.initState();
288292

289293
controller = WebViewController()
290-
..setUserAgent(
291-
'Clerk Flutter SDK v${clerk.ClerkConstants.flutterSdkVersion}',
292-
)
293294
..setJavaScriptMode(JavaScriptMode.unrestricted)
294295
..setBackgroundColor(Colors.white)
295296
..setNavigationDelegate(
@@ -319,7 +320,23 @@ class _SsoWebViewOverlayState extends State<_SsoWebViewOverlay> {
319320
},
320321
),
321322
);
322-
controller.loadRequest(Uri.parse(widget.url));
323+
324+
// For google authentication we use a custom user-agent
325+
if (widget.strategy.provider == clerk.Strategy.oauthGoogle.provider) {
326+
controller.setUserAgent(clerk.ClerkConstants.userAgent);
327+
controller.loadRequest(Uri.parse(widget.url));
328+
} else {
329+
controller.getUserAgent().then((String? userAgent) {
330+
if (mounted) {
331+
if (userAgent != null) {
332+
controller.setUserAgent(
333+
'$userAgent ${clerk.ClerkConstants.userAgent}',
334+
);
335+
}
336+
controller.loadRequest(Uri.parse(widget.url));
337+
}
338+
});
339+
}
323340
}
324341

325342
@override

0 commit comments

Comments
 (0)