Skip to content

Commit

Permalink
feat(macos): enable same window webview support
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Oct 9, 2024
1 parent 2a31739 commit 70bbb4a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lib/pages/mobile_login/hooks/login_callback.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Future<void> Function() useLoginCallback(WidgetRef ref) {
final authNotifier = ref.read(authenticationProvider.notifier);

return useCallback(() async {
if (kIsMobile) {
if (kIsMobile || kIsMacOS) {
context.pushNamed(WebViewLogin.name);
return;
}
Expand Down
72 changes: 38 additions & 34 deletions lib/pages/mobile_login/mobile_login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotube/components/titlebar/titlebar.dart';

import 'package:spotube/provider/authentication/authentication.dart';
import 'package:spotube/utils/platform.dart';
Expand All @@ -23,44 +24,47 @@ class WebViewLogin extends HookConsumerWidget {
}

return Scaffold(
body: SafeArea(
child: InAppWebView(
initialSettings: InAppWebViewSettings(
userAgent:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 safari/537.36",
),
initialUrlRequest: URLRequest(
url: WebUri("https://accounts.spotify.com/"),
),
onPermissionRequest: (controller, permissionRequest) async {
return PermissionResponse(
resources: permissionRequest.resources,
action: PermissionResponseAction.GRANT,
);
},
onLoadStop: (controller, action) async {
if (action == null) return;
String url = action.toString();
if (url.endsWith("/")) {
url = url.substring(0, url.length - 1);
}
appBar: const PageWindowTitleBar(
leading: BackButton(color: Colors.white),
backgroundColor: Colors.transparent,
),
extendBodyBehindAppBar: true,
body: InAppWebView(
initialSettings: InAppWebViewSettings(
userAgent:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 safari/537.36",
),
initialUrlRequest: URLRequest(
url: WebUri("https://accounts.spotify.com/"),
),
onPermissionRequest: (controller, permissionRequest) async {
return PermissionResponse(
resources: permissionRequest.resources,
action: PermissionResponseAction.GRANT,
);
},
onLoadStop: (controller, action) async {
if (action == null) return;
String url = action.toString();
if (url.endsWith("/")) {
url = url.substring(0, url.length - 1);
}

final exp = RegExp(r"https:\/\/accounts.spotify.com\/.+\/status");
final exp = RegExp(r"https:\/\/accounts.spotify.com\/.+\/status");

if (exp.hasMatch(url)) {
final cookies =
await CookieManager.instance().getCookies(url: action);
final cookieHeader =
"sp_dc=${cookies.firstWhere((element) => element.name == "sp_dc").value}";
if (exp.hasMatch(url)) {
final cookies =
await CookieManager.instance().getCookies(url: action);
final cookieHeader =
"sp_dc=${cookies.firstWhere((element) => element.name == "sp_dc").value}";

await authenticationNotifier.login(cookieHeader);
if (context.mounted) {
// ignore: use_build_context_synchronously
GoRouter.of(context).go("/");
}
await authenticationNotifier.login(cookieHeader);
if (context.mounted) {
// ignore: use_build_context_synchronously
GoRouter.of(context).go("/");
}
},
),
}
},
),
);
}
Expand Down

0 comments on commit 70bbb4a

Please sign in to comment.