Finish to add configuration tutorial
This commit is contained in:
parent
a3b25c3ee1
commit
4e8fd12fdc
9 changed files with 236 additions and 39 deletions
|
@ -2,7 +2,9 @@ import 'package:desktopapp/widgets/components/link.dart';
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
class Step1 extends StatelessWidget {
|
||||
const Step1({Key? key}) : super(key: key);
|
||||
const Step1({Key? key, required this.onNext}) : super(key: key);
|
||||
|
||||
final VoidCallback onNext;
|
||||
|
||||
Widget buildFieldDescription(String field, String description) {
|
||||
return Row(
|
||||
|
@ -60,7 +62,7 @@ class Step1 extends StatelessWidget {
|
|||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
TextButton(onPressed: () {}, child: const Text('Étape suivante'))
|
||||
TextButton(onPressed: onNext, child: const Text('Étape suivante'))
|
||||
],
|
||||
),
|
||||
)
|
||||
|
|
|
@ -4,7 +4,11 @@ import 'package:flutter/material.dart';
|
|||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class Step2 extends StatelessWidget {
|
||||
Step2({Key? key}) : super(key: key);
|
||||
Step2({Key? key, required this.onPrevious, required this.onNext})
|
||||
: super(key: key);
|
||||
|
||||
final VoidCallback onPrevious;
|
||||
final VoidCallback onNext;
|
||||
|
||||
final TextEditingController _clientIdController = TextEditingController();
|
||||
final TextEditingController _clientSecretController = TextEditingController();
|
||||
|
@ -86,8 +90,8 @@ class Step2 extends StatelessWidget {
|
|||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: () {}, child: const Text('Étape précédente')),
|
||||
TextButton(onPressed: () {}, child: const Text('Étape suivante'))
|
||||
onPressed: onPrevious, child: const Text('Étape précédente')),
|
||||
TextButton(onPressed: onNext, child: const Text('Étape suivante'))
|
||||
],
|
||||
),
|
||||
)
|
||||
|
|
|
@ -1,30 +1,45 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:desktopapp/classes/routes.dart';
|
||||
import 'package:desktopapp/utils/logger.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:desktopapp/widgets/components/link.dart' as link;
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
class Step3 extends StatelessWidget {
|
||||
const Step3({Key? key}) : super(key: key);
|
||||
const Step3({Key? key, required this.onPrevious}) : super(key: key);
|
||||
|
||||
final VoidCallback onPrevious;
|
||||
|
||||
Widget buildContent(BuildContext context, SharedPreferences prefs) {
|
||||
var clientId = prefs.getString('client_id');
|
||||
return Column(children: [
|
||||
const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 20),
|
||||
child: Text('Étape 3:Authorisation de l\'application',
|
||||
child: Text('Étape 3: Autorisation de l\'application',
|
||||
style: TextStyle(fontSize: 20)),
|
||||
),
|
||||
link.Link(
|
||||
uri:
|
||||
'https://streamlabs.com/api/v1.0/authorize?redirect_uri=http://localhost:1234/&client_id=$clientId&response_type=code&scope=alerts.create',
|
||||
label: 'Cliquez ici pour vous connecter à Streamlabs',
|
||||
)
|
||||
const Text(
|
||||
'Pour terminer la configuration, il reste plus qu\'à se connecter à Streamlabs pour autoriser l\'application à créer des alertes.'),
|
||||
const SizedBox(height: 10),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
waitAuthorization(context, prefs);
|
||||
var clientId = prefs.getString('client_id');
|
||||
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
launch(
|
||||
'https://streamlabs.com/api/v1.0/authorize?redirect_uri=http://localhost:1234/&client_id=$clientId&response_type=code&scope=alerts.create');
|
||||
},
|
||||
child: const Text('Cliquez ici pour vous connecter à Streamlabs')),
|
||||
]);
|
||||
}
|
||||
|
||||
Future waitAuthorizationCallback() async {
|
||||
var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 1214);
|
||||
Future<String?> waitAuthorizationCallback(SharedPreferences prefs) async {
|
||||
var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 1234);
|
||||
Logger.log(LoggerType.info, this,
|
||||
'Server started at ${server.address.address}:${server.port}');
|
||||
var request = await server.first;
|
||||
|
||||
var uri = Uri.dataFromString(request.requestedUri.toString());
|
||||
|
@ -32,30 +47,116 @@ class Step3 extends StatelessWidget {
|
|||
var code = query['code'];
|
||||
|
||||
if (code != null) {
|
||||
var prefs = await SharedPreferences.getInstance();
|
||||
prefs.setString('code', code);
|
||||
|
||||
request.response
|
||||
..headers.contentType = ContentType.text
|
||||
..write('You can close tab now')
|
||||
..close();
|
||||
} else {
|
||||
request.response
|
||||
..headers.contentType = ContentType.text
|
||||
..write('Internal error')
|
||||
..close();
|
||||
}
|
||||
|
||||
request.response
|
||||
..headers.contentType = ContentType.text
|
||||
..write('You can close tab now')
|
||||
..close();
|
||||
return code;
|
||||
}
|
||||
|
||||
// SEND TOKEN ASK TO API
|
||||
// POST https://streamlabs.com/api/v1.0/token
|
||||
// {
|
||||
// "grant_type": "authorization_code",
|
||||
// "code": "CODE",
|
||||
// "redirect_uri": "http://localhost:1234/",
|
||||
// "client_id": "CLIENT_ID",
|
||||
// "client_secret": "CLIENT_SECRET"
|
||||
// }
|
||||
// SAVE RESPONSE TOKEN AS CREDENTIALS IN SHARED PREFERENCES
|
||||
Future<void> waitAuthorization(
|
||||
BuildContext context, SharedPreferences prefs) async {
|
||||
var code = await waitAuthorizationCallback(prefs);
|
||||
|
||||
if (code != null) {
|
||||
if (prefs.containsKey('credentials')) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text('Info'),
|
||||
content: const Text(
|
||||
'Connexion réussi. Appuyez sur OK pour acceder à l\'application.'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
Navigator.of(context)
|
||||
.pushReplacementNamed(Routes.home.path);
|
||||
},
|
||||
child: const Text('OK'))
|
||||
],
|
||||
));
|
||||
} else {
|
||||
Dio dio = Dio(BaseOptions(
|
||||
contentType: Headers.jsonContentType,
|
||||
responseType: ResponseType.json,
|
||||
validateStatus: (_) => true));
|
||||
|
||||
var data = jsonEncode({
|
||||
'grant_type': 'authorization_code',
|
||||
'client_id': prefs.getString('client_id'),
|
||||
'client_secret': prefs.getString('client_secret'),
|
||||
'redirect_uri': 'http://localhost:1234/',
|
||||
'code': code,
|
||||
});
|
||||
|
||||
var response =
|
||||
await dio.post('https://streamlabs.com/api/v1.0/token', data: data);
|
||||
|
||||
if (response.statusCode == HttpStatus.ok) {
|
||||
prefs.setString('credentials', response.data.toString());
|
||||
Logger.log(LoggerType.info, this,
|
||||
'Authorization success : ${response.data}');
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text('Info'),
|
||||
content: const Text(
|
||||
'Connexion réussi. Appuyez sur OK pour acceder à l\'application.'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
Navigator.of(context)
|
||||
.pushReplacementNamed(Routes.home.path);
|
||||
},
|
||||
child: const Text('OK'))
|
||||
],
|
||||
));
|
||||
} else {
|
||||
Logger.log(LoggerType.error, this,
|
||||
'Authorization error : ${response.data} with $data and ${response.requestOptions.headers}');
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text('Erreur'),
|
||||
content: const Text('Impossible de se connecter.'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('OK'))
|
||||
],
|
||||
));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Logger.log(LoggerType.error, this,
|
||||
'Authorization error : impossible to get code');
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text('Erreur'),
|
||||
content: const Text('Impossible de se connecter.'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('OK'))
|
||||
],
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
waitAuthorizationCallback();
|
||||
return Column(
|
||||
children: [
|
||||
Expanded(
|
||||
|
@ -76,8 +177,7 @@ class Step3 extends StatelessWidget {
|
|||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: () {}, child: const Text('Étape précédente')),
|
||||
TextButton(onPressed: () {}, child: const Text('Étape suivante'))
|
||||
onPressed: onPrevious, child: const Text('Étape précédente'))
|
||||
],
|
||||
),
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue