-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathserver_ipc.dart
38 lines (34 loc) · 1.05 KB
/
server_ipc.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import 'dart:io';
import 'package:url_launcher/url_launcher.dart';
Future<String?> connectIpc(String authUri, String redirectUri) async {
try {
if (await canLaunch(authUri)) {
print("[Launching]: $authUri");
await launch(authUri);
}
HttpServer server =
await HttpServer.bind(InternetAddress.loopbackIPv4, 4304);
print("Server started");
await for (HttpRequest request in server) {
if (request.uri.path == "/auth/spotify/callback" &&
request.method == "GET") {
String? code = request.uri.queryParameters["code"];
if (code != null) {
request.response
..statusCode = HttpStatus.ok
..write("Authentication successful")
..close();
return "$redirectUri?code=$code";
} else {
request.response
..statusCode = HttpStatus.forbidden
..write("Authorization failed start over!")
..close();
throw Exception("No code provided");
}
}
}
} catch (error) {
throw error;
}
}