register app in ui
This commit is contained in:
parent
bc8d31ccbc
commit
4baacc751d
3 changed files with 101 additions and 10 deletions
|
@ -1,6 +1,9 @@
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:url_launcher/url_launcher.dart';
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
import '../../global.dart' as global;
|
import '../../global.dart' as global;
|
||||||
|
import '../../business_logic/settings.dart' as settings;
|
||||||
|
|
||||||
class App {
|
class App {
|
||||||
late String clientSecret;
|
late String clientSecret;
|
||||||
|
@ -27,15 +30,26 @@ class App {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<http.Response> askKnownServerForAppRegistration() async {
|
||||||
|
String url = await settings.loadInstanceUrl();
|
||||||
|
try {
|
||||||
|
return await registerApp(url);
|
||||||
|
} catch (e) {
|
||||||
|
return http.Response(jsonEncode({}), 404);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<http.Response> registerApp(String baseurl) async {
|
Future<http.Response> registerApp(String baseurl) async {
|
||||||
//String url = baseurl Uri."api/v1/apps";
|
//String url = baseurl Uri."api/v1/apps";
|
||||||
Uri url = Uri.https(baseurl, "/api/v1/apps");
|
Uri url = Uri.https(baseurl, "/api/v1/apps");
|
||||||
var response = await http.post(url, headers: global.defaultHeaders, body: {
|
final response = await http.post(url,
|
||||||
|
headers: global.defaultHeaders,
|
||||||
|
body: jsonEncode({
|
||||||
'client_name': global.name,
|
'client_name': global.name,
|
||||||
'redirect_uris': "ietf:wg:oauth:2.0:oob",
|
'redirect_uris': "urn:ietf:wg:oauth:2.0:oob",
|
||||||
'scopes': "read write",
|
'scopes': "read write",
|
||||||
'website': global.website
|
'website': global.website
|
||||||
});
|
}));
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
30
lib/business_logic/settings.dart
Normal file
30
lib/business_logic/settings.dart
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
enum Settings {
|
||||||
|
instanceUrl,
|
||||||
|
username,
|
||||||
|
}
|
||||||
|
|
||||||
|
void saveSetting(Settings setting, dynamic value) async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> saveInstanceUrl(String url) async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
return prefs.setString("instance-url", url);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String> loadInstanceUrl() async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
String? possibleReturn = prefs.getString("instance-url");
|
||||||
|
if (possibleReturn == null) {
|
||||||
|
return "example.com";
|
||||||
|
} else {
|
||||||
|
return possibleReturn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> saveUsername(String username) async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
return prefs.setString("username", username);
|
||||||
|
}
|
|
@ -1,7 +1,11 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:http/http.dart';
|
||||||
import 'package:slothmu/business_logic/user.dart';
|
import 'package:slothmu/business_logic/user.dart';
|
||||||
import 'package:localization/localization.dart';
|
import 'package:localization/localization.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
import '../business_logic/auth/oauth.dart' as oauth;
|
import '../business_logic/auth/oauth.dart' as oauth;
|
||||||
|
import '../business_logic/settings.dart' as settings;
|
||||||
|
import '../business_logic/user.dart' as user;
|
||||||
|
|
||||||
class Login extends StatefulWidget {
|
class Login extends StatefulWidget {
|
||||||
const Login({Key? key}) : super(key: key);
|
const Login({Key? key}) : super(key: key);
|
||||||
|
@ -44,6 +48,11 @@ class _LoginFormState extends State<LoginForm> {
|
||||||
children: [
|
children: [
|
||||||
Text("greeting".i18n(), style: const TextStyle(fontSize: 64)),
|
Text("greeting".i18n(), style: const TextStyle(fontSize: 64)),
|
||||||
TextFormField(
|
TextFormField(
|
||||||
|
onSaved: (value) async {
|
||||||
|
await settings
|
||||||
|
.saveInstanceUrl(user.urlFromUsername(name: value!));
|
||||||
|
await settings.saveUsername(user.userFromUsername(name: value));
|
||||||
|
},
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
labelText: "user-id".i18n(),
|
labelText: "user-id".i18n(),
|
||||||
hintText: "user-id-example".i18n(),
|
hintText: "user-id-example".i18n(),
|
||||||
|
@ -54,8 +63,6 @@ class _LoginFormState extends State<LoginForm> {
|
||||||
validator: (value) {
|
validator: (value) {
|
||||||
if (value!.isEmpty || !isValidUsername(name: value)) {
|
if (value!.isEmpty || !isValidUsername(name: value)) {
|
||||||
return "user-id-not-valid".i18n();
|
return "user-id-not-valid".i18n();
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
@ -66,6 +73,7 @@ class _LoginFormState extends State<LoginForm> {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||||
content: Text("login-failed-snackbar-text".i18n())));
|
content: Text("login-failed-snackbar-text".i18n())));
|
||||||
} else {
|
} else {
|
||||||
|
formKey.currentState?.save();
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
|
@ -82,6 +90,9 @@ class _LoginFormState extends State<LoginForm> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
page that handles authenticating user
|
||||||
|
*/
|
||||||
class AuthPage extends StatefulWidget {
|
class AuthPage extends StatefulWidget {
|
||||||
const AuthPage({Key? key, required String baseurl}) : super(key: key);
|
const AuthPage({Key? key, required String baseurl}) : super(key: key);
|
||||||
|
|
||||||
|
@ -94,7 +105,7 @@ class _AuthPageState extends State<AuthPage> {
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
body: Padding(
|
body: Padding(
|
||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(24.0),
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||||
children: [
|
children: [
|
||||||
|
@ -103,7 +114,43 @@ class _AuthPageState extends State<AuthPage> {
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
hintText: "123-ABC",
|
hintText: "123-ABC",
|
||||||
icon: const Icon(Icons.password),
|
icon: const Icon(Icons.password),
|
||||||
labelText: "code-hint".i18n()))
|
labelText: "code-hint".i18n())),
|
||||||
|
Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||||
|
children: [
|
||||||
|
TextButton.icon(
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.pop(context);
|
||||||
|
},
|
||||||
|
icon: const Icon(Icons.arrow_back),
|
||||||
|
label: Text("back-button".i18n())),
|
||||||
|
FutureBuilder<http.Response>(
|
||||||
|
future: oauth.askKnownServerForAppRegistration(),
|
||||||
|
builder: (context, snapshot) {
|
||||||
|
if (snapshot.hasError) {
|
||||||
|
return Text("login-failed-snackbar-text".i18n());
|
||||||
|
} else if (snapshot.hasData) {
|
||||||
|
if (snapshot.data?.statusCode != null) {
|
||||||
|
print(snapshot.data?.body);
|
||||||
|
if (snapshot.data?.statusCode != 200) {
|
||||||
|
return Row(
|
||||||
|
children: [
|
||||||
|
Text("error ${snapshot.data!.statusCode}")
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return TextButton.icon(
|
||||||
|
onPressed: null,
|
||||||
|
icon: const Icon(Icons.check),
|
||||||
|
label: Text("confirm-button".i18n()));
|
||||||
|
} else {
|
||||||
|
return const CircularProgressIndicator();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue