relatica/lib/screens/sign_in.dart

304 lines
11 KiB
Dart
Raw Normal View History

2022-11-09 02:28:48 +00:00
import 'package:flutter/material.dart';
2023-02-27 20:39:02 +00:00
import 'package:go_router/go_router.dart';
2023-02-28 04:36:18 +00:00
import 'package:logging/logging.dart';
import 'package:relatica/models/auth/basic_credentials.dart';
2023-02-27 20:39:02 +00:00
import 'package:relatica/routes.dart';
import 'package:string_validator/string_validator.dart';
2022-11-09 02:28:48 +00:00
import '../controls/padding.dart';
import '../globals.dart';
2023-02-28 04:36:18 +00:00
import '../models/auth/credentials_intf.dart';
import '../models/auth/oauth_credentials.dart';
2022-11-09 02:28:48 +00:00
import '../services/auth_service.dart';
import '../utils/snackbar_builder.dart';
class SignInScreen extends StatefulWidget {
@override
State<SignInScreen> createState() => _SignInScreenState();
}
class _SignInScreenState extends State<SignInScreen> {
2023-02-28 04:36:18 +00:00
static final _logger = Logger('$SignInScreen');
static const usernamePasswordType = 'Username/Password';
static const oauthType = 'OAuth';
static final authTypes = [usernamePasswordType, oauthType];
2022-11-09 02:28:48 +00:00
final formKey = GlobalKey<FormState>();
final usernameController = TextEditingController();
final serverNameController = TextEditingController();
2022-11-09 02:28:48 +00:00
final passwordController = TextEditingController();
2023-02-28 04:36:18 +00:00
var authType = oauthType;
2023-01-28 23:18:18 +00:00
var hidePassword = true;
2023-02-28 04:36:18 +00:00
var showUsernameAndPasswordFields = true;
2022-11-09 02:28:48 +00:00
@override
void initState() {
super.initState();
2023-02-27 20:39:02 +00:00
final service = getIt<AccountsService>();
if (service.loggedIn) {
2023-02-28 04:36:18 +00:00
setCredentials(null, service.currentProfile.credentials);
}
}
void setBasicCredentials(BasicCredentials credentials) {
usernameController.text = credentials.username;
passwordController.text = credentials.password;
serverNameController.text = credentials.serverName;
showUsernameAndPasswordFields = true;
authType = usernamePasswordType;
}
void setOauthCredentials(OAuthCredentials credentials) {
serverNameController.text = credentials.serverName;
showUsernameAndPasswordFields = false;
authType = oauthType;
}
void setCredentials(BuildContext? context, ICredentials credentials) {
if (credentials is BasicCredentials) {
setBasicCredentials(credentials);
return;
}
if (credentials is OAuthCredentials) {
setOauthCredentials(credentials);
return;
}
final msg = 'Unknown credentials type: ${credentials.runtimeType}';
_logger.severe(msg);
if (context?.mounted ?? false) {
buildSnackbar(context!, msg);
}
}
2022-11-09 02:28:48 +00:00
@override
Widget build(BuildContext context) {
2023-02-27 20:39:02 +00:00
final service = getIt<AccountsService>();
final loggedInProfiles = service.loggedInProfiles;
final loggedOutProfiles = service.loggedOutProfiles;
2022-11-09 02:28:48 +00:00
return Scaffold(
appBar: AppBar(
2023-01-28 23:18:18 +00:00
title: const Text('Sign In'),
2022-11-09 02:28:48 +00:00
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Form(
key: formKey,
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
2023-02-28 04:36:18 +00:00
DropdownButton<String>(
value: authType,
items: authTypes
.map((a) => DropdownMenuItem(value: a, child: Text(a)))
.toList(),
onChanged: (value) {
setState(() {
authType = value ?? '';
switch (value) {
case usernamePasswordType:
showUsernameAndPasswordFields = true;
break;
case oauthType:
showUsernameAndPasswordFields = false;
break;
default:
print("Don't know this");
}
});
}),
const VerticalPadding(),
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
controller: serverNameController,
validator: (value) =>
isFQDN(value ?? '') ? null : 'Not a valid server name',
decoration: InputDecoration(
prefixIcon: const Icon(Icons.alternate_email),
hintText: 'Server Name (friendica.example.com)',
border: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).backgroundColor,
),
borderRadius: BorderRadius.circular(5.0),
),
labelText: 'Server Name',
),
),
const VerticalPadding(),
2023-02-28 04:36:18 +00:00
if (showUsernameAndPasswordFields) ...[
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
controller: usernameController,
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value == null) {
return null;
}
2023-02-28 04:36:18 +00:00
if (value.contains('@')) {
return isEmail(value)
? null
: 'Not a valid Friendica Account Address';
}
return isAlphanumeric(value.replaceAll('-', ''))
? null
: 'Username should be alpha-numeric';
},
decoration: InputDecoration(
prefixIcon: const Icon(Icons.alternate_email),
hintText: 'Username (user@example.com)',
border: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).backgroundColor,
),
borderRadius: BorderRadius.circular(5.0),
2022-11-09 02:28:48 +00:00
),
2023-02-28 04:36:18 +00:00
labelText: 'Username',
2022-11-09 02:28:48 +00:00
),
),
2023-02-28 04:36:18 +00:00
const VerticalPadding(),
TextFormField(
obscureText: hidePassword,
controller: passwordController,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.password),
suffixIcon: IconButton(
onPressed: () {
setState(() {
hidePassword = !hidePassword;
});
},
icon: hidePassword
? const Icon(Icons.remove_red_eye_outlined)
: const Icon(Icons.remove_red_eye),
2022-11-09 02:28:48 +00:00
),
2023-02-28 04:36:18 +00:00
hintText: 'Password',
border: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).backgroundColor,
),
borderRadius: BorderRadius.circular(5.0),
),
labelText: 'Password',
2022-11-09 02:28:48 +00:00
),
),
2023-02-28 04:36:18 +00:00
const VerticalPadding(),
],
2022-11-09 02:28:48 +00:00
ElevatedButton(
onPressed: () => _signIn(context),
2023-01-28 23:18:18 +00:00
child: const Text('Signin'),
2022-11-09 02:28:48 +00:00
),
2023-02-27 20:39:02 +00:00
const VerticalPadding(),
const Text('Logged out:'),
Expanded(
flex: 1,
child: ListView.separated(
itemBuilder: (context, index) {
final p = loggedOutProfiles[index];
return ListTile(
onTap: () {
2023-02-28 04:36:18 +00:00
setCredentials(context, p.credentials);
setState(() {});
2023-02-27 20:39:02 +00:00
},
title: Text(p.handle),
subtitle: Text(p.credentials is BasicCredentials
? 'Username/Password'
: 'OAuth Login'),
2023-02-27 20:39:02 +00:00
);
},
separatorBuilder: (_, __) => const Divider(),
itemCount: loggedOutProfiles.length,
),
),
const VerticalPadding(),
const Text('Logged in:'),
Expanded(
flex: 1,
child: ListView.separated(
itemBuilder: (context, index) {
final p = loggedInProfiles[index];
final active = service.loggedIn
? p.id == service.currentProfile.id
: false;
return ListTile(
onTap: () async {
2023-02-28 04:36:18 +00:00
setCredentials(context, p.credentials);
setState(() {});
2023-02-27 20:39:02 +00:00
await service.setActiveProfile(p);
2023-02-28 04:36:18 +00:00
2023-02-27 20:39:02 +00:00
if (mounted) {
clearCaches();
context.goNamed(ScreenPaths.timelines);
}
},
title: Text(
p.handle,
style: active
? const TextStyle(
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic)
: null,
),
subtitle: Text(
p.credentials is BasicCredentials
? 'Username/Password'
: 'OAuth Login',
style: active
? const TextStyle(
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic)
2023-02-27 20:39:02 +00:00
: null,
),
);
},
separatorBuilder: (_, __) => const Divider(),
itemCount: loggedInProfiles.length,
),
),
2022-11-09 02:28:48 +00:00
],
),
),
),
),
);
}
void _signIn(BuildContext context) async {
if (formKey.currentState?.validate() ?? false) {
2023-02-28 04:36:18 +00:00
ICredentials? creds;
switch (authType) {
case usernamePasswordType:
creds = BasicCredentials(
username: usernameController.text,
password: passwordController.text,
serverName: serverNameController.text);
break;
case oauthType:
creds = OAuthCredentials.bootstrap(serverNameController.text);
break;
default:
buildSnackbar(context, 'Unknown authorization type: $authType');
break;
}
if (creds == null) {
return;
}
2023-02-25 23:06:24 +00:00
final result = await getIt<AccountsService>().signIn(creds);
2023-02-27 20:39:02 +00:00
if (!mounted) {
return;
}
if (result.isFailure) {
buildSnackbar(context, 'Error signing in: ${result.error}');
}
2023-02-27 20:39:02 +00:00
context.goNamed(ScreenPaths.timelines);
2022-11-09 02:28:48 +00:00
}
}
}