✨ Connect with spotify
This commit is contained in:
@ -1,8 +1,11 @@
|
||||
import 'package:desktop_webview_window/desktop_webview_window.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:media_kit/media_kit.dart';
|
||||
import 'package:rhythm_box/platform.dart';
|
||||
import 'package:rhythm_box/providers/audio_player.dart';
|
||||
import 'package:rhythm_box/providers/audio_player_stream.dart';
|
||||
import 'package:rhythm_box/providers/auth.dart';
|
||||
import 'package:rhythm_box/providers/database.dart';
|
||||
import 'package:rhythm_box/providers/history.dart';
|
||||
import 'package:rhythm_box/providers/palette.dart';
|
||||
@ -11,6 +14,8 @@ import 'package:rhythm_box/providers/skip_segments.dart';
|
||||
import 'package:rhythm_box/providers/spotify.dart';
|
||||
import 'package:rhythm_box/providers/user_preferences.dart';
|
||||
import 'package:rhythm_box/router.dart';
|
||||
import 'package:rhythm_box/services/kv_store/encrypted_kv_store.dart';
|
||||
import 'package:rhythm_box/services/kv_store/kv_store.dart';
|
||||
import 'package:rhythm_box/services/lyrics/provider.dart';
|
||||
import 'package:rhythm_box/services/server/active_sourced_track.dart';
|
||||
import 'package:rhythm_box/services/server/routes/playback.dart';
|
||||
@ -18,9 +23,29 @@ import 'package:rhythm_box/services/server/server.dart';
|
||||
import 'package:rhythm_box/services/server/sourced_track.dart';
|
||||
import 'package:rhythm_box/translations.dart';
|
||||
import 'package:rhythm_box/widgets/tracks/querying_track_info.dart';
|
||||
import 'package:smtc_windows/smtc_windows.dart';
|
||||
import 'package:window_manager/window_manager.dart';
|
||||
|
||||
Future<void> main(List<String> rawArgs) async {
|
||||
if (rawArgs.contains('web_view_title_bar')) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
if (runWebViewTitleBarWidget(rawArgs)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
MediaKit.ensureInitialized();
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
if (PlatformInfo.isDesktop) {
|
||||
await windowManager.setPreventClose(true);
|
||||
}
|
||||
if (PlatformInfo.isWindows) {
|
||||
await SMTCWindows.initialize();
|
||||
}
|
||||
|
||||
await KVStoreService.initialize();
|
||||
await EncryptedKvStoreService.initialize();
|
||||
|
||||
runApp(const MyApp());
|
||||
}
|
||||
@ -62,6 +87,7 @@ class MyApp extends StatelessWidget {
|
||||
Get.lazyPut(() => SyncedLyricsProvider());
|
||||
|
||||
Get.put(DatabaseProvider());
|
||||
Get.put(AuthenticationProvider());
|
||||
|
||||
Get.put(AudioPlayerProvider());
|
||||
Get.put(ActiveSourcedTrackProvider());
|
||||
|
140
lib/providers/auth.dart
Normal file
140
lib/providers/auth.dart
Normal file
@ -0,0 +1,140 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/io.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:get/get.dart' hide Value;
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:rhythm_box/providers/database.dart';
|
||||
import 'package:rhythm_box/services/database/database.dart';
|
||||
|
||||
extension ExpirationAuthenticationTableData on AuthenticationTableData {
|
||||
bool get isExpired => DateTime.now().isAfter(expiration);
|
||||
|
||||
String? getCookie(String key) => cookie.value
|
||||
.split('; ')
|
||||
.firstWhereOrNull((c) => c.trim().startsWith('$key='))
|
||||
?.trim()
|
||||
.split('=')
|
||||
.last
|
||||
.replaceAll(';', '');
|
||||
}
|
||||
|
||||
class AuthenticationProvider extends GetxController {
|
||||
static final Dio dio = () {
|
||||
final dio = Dio();
|
||||
|
||||
(dio.httpClientAdapter as IOHttpClientAdapter)
|
||||
.createHttpClient = () => HttpClient()
|
||||
..badCertificateCallback = (X509Certificate cert, String host, int port) {
|
||||
return host.endsWith('spotify.com') && port == 443;
|
||||
};
|
||||
|
||||
return dio;
|
||||
}();
|
||||
|
||||
var auth = Rxn<AuthenticationTableData?>();
|
||||
Timer? refreshTimer;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
loadAuthenticationData();
|
||||
}
|
||||
|
||||
Future<void> loadAuthenticationData() async {
|
||||
final database = Get.find<DatabaseProvider>().database;
|
||||
final data = await (database.select(database.authenticationTable)
|
||||
..where((s) => s.id.equals(0)))
|
||||
.getSingleOrNull();
|
||||
|
||||
auth.value = data;
|
||||
_setRefreshTimer();
|
||||
}
|
||||
|
||||
void _setRefreshTimer() {
|
||||
refreshTimer?.cancel();
|
||||
if (auth.value != null && auth.value!.isExpired) {
|
||||
refreshCredentials();
|
||||
}
|
||||
refreshTimer = Timer(
|
||||
auth.value!.expiration.difference(DateTime.now()),
|
||||
() => refreshCredentials(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> refreshCredentials() async {
|
||||
final database = Get.find<DatabaseProvider>().database;
|
||||
final refreshedCredentials =
|
||||
await credentialsFromCookie(auth.value!.cookie.value);
|
||||
|
||||
await database
|
||||
.update(database.authenticationTable)
|
||||
.replace(refreshedCredentials);
|
||||
loadAuthenticationData(); // Reload data after refreshing
|
||||
}
|
||||
|
||||
Future<void> login(String cookie) async {
|
||||
final database = Get.find<DatabaseProvider>().database;
|
||||
final refreshedCredentials = await credentialsFromCookie(cookie);
|
||||
|
||||
await database
|
||||
.into(database.authenticationTable)
|
||||
.insert(refreshedCredentials, mode: InsertMode.replace);
|
||||
loadAuthenticationData(); // Reload data after login
|
||||
}
|
||||
|
||||
Future<AuthenticationTableCompanion> credentialsFromCookie(
|
||||
String cookie) async {
|
||||
try {
|
||||
final spDc = cookie
|
||||
.split('; ')
|
||||
.firstWhereOrNull((c) => c.trim().startsWith('sp_dc='))
|
||||
?.trim();
|
||||
final res = await dio.getUri(
|
||||
Uri.parse(
|
||||
'https://open.spotify.com/get_access_token?reason=transport&productType=web_player'),
|
||||
options: Options(
|
||||
headers: {
|
||||
'Cookie': spDc ?? '',
|
||||
'User-Agent':
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
|
||||
},
|
||||
validateStatus: (status) => true,
|
||||
),
|
||||
);
|
||||
final body = res.data;
|
||||
|
||||
if ((res.statusCode ?? 500) >= 400) {
|
||||
throw Exception(
|
||||
"Failed to get access token: ${body['error'] ?? res.statusMessage}");
|
||||
}
|
||||
|
||||
return AuthenticationTableCompanion.insert(
|
||||
id: const Value(0),
|
||||
cookie: DecryptedText("${res.headers["set-cookie"]?.join(";")}; $spDc"),
|
||||
accessToken: DecryptedText(body['accessToken']),
|
||||
expiration: DateTime.fromMillisecondsSinceEpoch(
|
||||
body['accessTokenExpirationTimestampMs']),
|
||||
);
|
||||
} catch (e) {
|
||||
// Handle error
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
auth.value = null;
|
||||
final database = Get.find<DatabaseProvider>().database;
|
||||
await (database.delete(database.authenticationTable)
|
||||
..where((s) => s.id.equals(0)))
|
||||
.go();
|
||||
// Additional cleanup if necessary
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
refreshTimer?.cancel();
|
||||
super.onClose();
|
||||
}
|
||||
}
|
@ -7,8 +7,6 @@ class PaletteProvider extends GetxController {
|
||||
|
||||
void updatePalette(PaletteGenerator? newPalette) {
|
||||
palette.value = newPalette;
|
||||
print('call update!');
|
||||
print(newPalette);
|
||||
if (newPalette != null) {
|
||||
Get.changeTheme(
|
||||
ThemeData.from(
|
||||
|
@ -1,17 +1,59 @@
|
||||
import 'dart:async';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:rhythm_box/providers/auth.dart';
|
||||
import 'package:spotify/spotify.dart';
|
||||
|
||||
class SpotifyProvider extends GetxController {
|
||||
late final SpotifyApi api;
|
||||
late SpotifyApi api;
|
||||
|
||||
List<StreamSubscription>? _subscriptions;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
api = SpotifyApi(
|
||||
final AuthenticationProvider authenticate = Get.find();
|
||||
if (authenticate.auth.value == null) {
|
||||
api = _initApiWithClientCredentials();
|
||||
} else {
|
||||
api = _initApiWithUserCredentials();
|
||||
}
|
||||
_subscriptions = [
|
||||
authenticate.auth.listen((value) {
|
||||
if (value == null) {
|
||||
api = _initApiWithClientCredentials();
|
||||
} else {
|
||||
api = _initApiWithUserCredentials();
|
||||
}
|
||||
}),
|
||||
];
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
SpotifyApi _initApiWithClientCredentials() {
|
||||
log('[SpotifyApi] Using client credentials...');
|
||||
return SpotifyApi(
|
||||
SpotifyApiCredentials(
|
||||
'f73d4bff91d64d89be9930036f553534',
|
||||
'5cbec0b928d247cd891d06195f07b8c9',
|
||||
),
|
||||
);
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
SpotifyApi _initApiWithUserCredentials() {
|
||||
log('[SpotifyApi] Using user credentials...');
|
||||
final AuthenticationProvider authenticate = Get.find();
|
||||
return SpotifyApi.withAccessToken(
|
||||
authenticate.auth.value!.accessToken.value);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
if (_subscriptions != null) {
|
||||
for (final subscription in _subscriptions!) {
|
||||
subscription.cancel();
|
||||
}
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
import 'package:animations/animations.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:rhythm_box/screens/auth/mobile_login.dart';
|
||||
import 'package:rhythm_box/screens/explore.dart';
|
||||
import 'package:rhythm_box/screens/player/lyrics.dart';
|
||||
import 'package:rhythm_box/screens/player/view.dart';
|
||||
@ -62,4 +63,14 @@ final router = GoRouter(routes: [
|
||||
),
|
||||
],
|
||||
),
|
||||
ShellRoute(
|
||||
builder: (context, state, child) => child,
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: '/auth/mobile-login',
|
||||
name: 'authMobileLogin',
|
||||
builder: (context, state) => const MobileLogin(),
|
||||
),
|
||||
],
|
||||
),
|
||||
]);
|
||||
|
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();
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -1,4 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:rhythm_box/providers/auth.dart';
|
||||
import 'package:rhythm_box/providers/spotify.dart';
|
||||
import 'package:rhythm_box/screens/auth/login.dart';
|
||||
import 'package:rhythm_box/widgets/auto_cache_image.dart';
|
||||
|
||||
class SettingsScreen extends StatefulWidget {
|
||||
const SettingsScreen({super.key});
|
||||
@ -8,6 +13,11 @@ class SettingsScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _SettingsScreenState extends State<SettingsScreen> {
|
||||
late final SpotifyProvider _spotify = Get.find();
|
||||
late final AuthenticationProvider _authenticate = Get.find();
|
||||
|
||||
bool _isLoggingIn = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Material(
|
||||
@ -15,14 +25,73 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
child: SafeArea(
|
||||
child: Column(
|
||||
children: [
|
||||
ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
leading: const Icon(Icons.login),
|
||||
title: const Text('Connect with Spotify'),
|
||||
subtitle: const Text('To explore your own library and more'),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: () {},
|
||||
),
|
||||
Obx(() {
|
||||
if (_authenticate.auth.value == null) {
|
||||
return ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
leading: const Icon(Icons.login),
|
||||
title: const Text('Connect with Spotify'),
|
||||
subtitle: const Text('To explore your own library and more'),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
enabled: !_isLoggingIn,
|
||||
onTap: () async {
|
||||
setState(() => _isLoggingIn = true);
|
||||
await universalLogin(context);
|
||||
setState(() => _isLoggingIn = false);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return FutureBuilder(
|
||||
future: _spotify.api.me.get(),
|
||||
builder: (context, snapshot) {
|
||||
print(snapshot.data);
|
||||
print(snapshot.error);
|
||||
if (!snapshot.hasData) {
|
||||
return const ListTile(
|
||||
contentPadding:
|
||||
const EdgeInsets.symmetric(horizontal: 24),
|
||||
leading: SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 3,
|
||||
),
|
||||
),
|
||||
title: Text('Loading...'),
|
||||
);
|
||||
}
|
||||
|
||||
return ListTile(
|
||||
leading: (snapshot.data!.images?.isNotEmpty ?? false)
|
||||
? CircleAvatar(
|
||||
backgroundImage: AutoCacheImage.provider(
|
||||
snapshot.data!.images!.firstOrNull!.url!,
|
||||
),
|
||||
)
|
||||
: const Icon(Icons.account_circle),
|
||||
title: Text(snapshot.data!.displayName!),
|
||||
subtitle: const Text('Connected with your Spotify'),
|
||||
);
|
||||
},
|
||||
);
|
||||
}),
|
||||
Obx(() {
|
||||
if (_authenticate.auth.value == null) {
|
||||
return const SizedBox();
|
||||
}
|
||||
|
||||
return ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
leading: const Icon(Icons.logout),
|
||||
title: const Text('Log out'),
|
||||
subtitle: const Text('Disconnect with this Spotify account'),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: () async {
|
||||
_authenticate.logout();
|
||||
},
|
||||
);
|
||||
}),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
@ -15,7 +15,7 @@ class WindowsAudioService {
|
||||
final subscriptions = <StreamSubscription>[];
|
||||
|
||||
WindowsAudioService() : smtc = SMTCWindows(enabled: false) {
|
||||
smtc.setPlaybackStatus(PlaybackStatus.Stopped);
|
||||
smtc.setPlaybackStatus(PlaybackStatus.stopped);
|
||||
final buttonStream = smtc.buttonPressStream.listen((event) {
|
||||
switch (event) {
|
||||
case PressedButton.play:
|
||||
@ -42,16 +42,16 @@ class WindowsAudioService {
|
||||
audioPlayer.playerStateStream.listen((state) async {
|
||||
switch (state) {
|
||||
case AudioPlaybackState.playing:
|
||||
await smtc.setPlaybackStatus(PlaybackStatus.Playing);
|
||||
await smtc.setPlaybackStatus(PlaybackStatus.playing);
|
||||
break;
|
||||
case AudioPlaybackState.paused:
|
||||
await smtc.setPlaybackStatus(PlaybackStatus.Paused);
|
||||
await smtc.setPlaybackStatus(PlaybackStatus.paused);
|
||||
break;
|
||||
case AudioPlaybackState.stopped:
|
||||
await smtc.setPlaybackStatus(PlaybackStatus.Stopped);
|
||||
await smtc.setPlaybackStatus(PlaybackStatus.stopped);
|
||||
break;
|
||||
case AudioPlaybackState.completed:
|
||||
await smtc.setPlaybackStatus(PlaybackStatus.Changing);
|
||||
await smtc.setPlaybackStatus(PlaybackStatus.changing);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user