✨ Connect with spotify
This commit is contained in:
52
lib/screens/auth/desktop_login.dart
Normal file
52
lib/screens/auth/desktop_login.dart
Normal file
@@ -0,0 +1,52 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:desktop_webview_window/desktop_webview_window.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:rhythm_box/platform.dart';
|
||||
import 'package:rhythm_box/providers/auth.dart';
|
||||
|
||||
Future<void> desktopLogin(BuildContext context) async {
|
||||
final exp = RegExp(r'https:\/\/accounts.spotify.com\/.+\/status');
|
||||
final applicationSupportDir = await getApplicationSupportDirectory();
|
||||
final userDataFolder =
|
||||
Directory(join(applicationSupportDir.path, 'webview_window_Webview2'));
|
||||
|
||||
if (!await userDataFolder.exists()) {
|
||||
await userDataFolder.create();
|
||||
}
|
||||
|
||||
final webview = await WebviewWindow.create(
|
||||
configuration: CreateConfiguration(
|
||||
title: 'Spotify Login',
|
||||
titleBarTopPadding: PlatformInfo.isMacOS ? 20 : 0,
|
||||
windowHeight: 720,
|
||||
windowWidth: 1280,
|
||||
userDataFolderWindows: userDataFolder.path,
|
||||
),
|
||||
);
|
||||
webview
|
||||
..setBrightness(Theme.of(context).colorScheme.brightness)
|
||||
..launch('https://accounts.spotify.com/')
|
||||
..setOnUrlRequestCallback((url) {
|
||||
if (exp.hasMatch(url)) {
|
||||
webview.getAllCookies().then((cookies) async {
|
||||
final cookieHeader =
|
||||
"sp_dc=${cookies.firstWhere((element) => element.name.contains("sp_dc")).value.replaceAll("\u0000", "")}";
|
||||
|
||||
final AuthenticationProvider authenticate = Get.find();
|
||||
await authenticate.login(cookieHeader);
|
||||
|
||||
webview.close();
|
||||
if (context.mounted) {
|
||||
GoRouter.of(context).go('/');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
13
lib/screens/auth/login.dart
Normal file
13
lib/screens/auth/login.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:rhythm_box/platform.dart';
|
||||
import 'package:rhythm_box/screens/auth/desktop_login.dart';
|
||||
|
||||
Future<void> universalLogin(BuildContext context) async {
|
||||
if (PlatformInfo.isMobile) {
|
||||
GoRouter.of(context).pushNamed('authMobileLogin');
|
||||
return;
|
||||
}
|
||||
|
||||
return await desktopLogin(context);
|
||||
}
|
67
lib/screens/auth/mobile_login.dart
Normal file
67
lib/screens/auth/mobile_login.dart
Normal file
@@ -0,0 +1,67 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:rhythm_box/platform.dart';
|
||||
import 'package:rhythm_box/providers/auth.dart';
|
||||
|
||||
class MobileLogin extends StatelessWidget {
|
||||
const MobileLogin({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final AuthenticationProvider authenticate = Get.find();
|
||||
|
||||
if (PlatformInfo.isDesktop) {
|
||||
const Scaffold(
|
||||
body: Center(
|
||||
child: Text('This feature is not available on desktop'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Connect with Spotify'),
|
||||
),
|
||||
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);
|
||||
}
|
||||
|
||||
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}";
|
||||
|
||||
await authenticate.login(cookieHeader);
|
||||
if (context.mounted) {
|
||||
GoRouter.of(context).pop();
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user