diff --git a/lib/ui/setup_page.dart b/lib/ui/setup_page.dart index a9650739c..06bea2dcc 100644 --- a/lib/ui/setup_page.dart +++ b/lib/ui/setup_page.dart @@ -13,14 +13,13 @@ class SetupPage extends StatefulWidget { } class _SetupPageState extends State { - bool _hasFoundEndpoint = Configuration.instance.getEndpoint() != null; bool _errorFindingEndpoint = false; String _enteredEndpoint = ""; @override Widget build(BuildContext context) { - _hasFoundEndpoint = Configuration.instance.getEndpoint() != null; - if (!_hasFoundEndpoint && !_errorFindingEndpoint) { + if (Configuration.instance.getEndpoint() == null && + !_errorFindingEndpoint) { EndpointFinder.instance.findEndpoint().then((endpoint) { setState(() { Configuration.instance.setEndpoint(endpoint); @@ -41,9 +40,11 @@ class _SetupPageState extends State { } Widget _getBody() { - if (!_hasFoundEndpoint && !_errorFindingEndpoint) { + if (Configuration.instance.getEndpoint() == null && + !_errorFindingEndpoint) { return _getSearchScreen(); - } else if (!_hasFoundEndpoint && _errorFindingEndpoint) { + } else if (Configuration.instance.getEndpoint() == null && + _errorFindingEndpoint) { return _getManualEndpointEntryScreen(); } else { return SignInWidget(() { diff --git a/lib/ui/sign_in_widget.dart b/lib/ui/sign_in_widget.dart index a13563484..701e04ec9 100644 --- a/lib/ui/sign_in_widget.dart +++ b/lib/ui/sign_in_widget.dart @@ -17,21 +17,31 @@ class SignInWidget extends StatefulWidget { } class _SignInWidgetState extends State { - String _username, _password; + String _username, _password, _repeatedPassword; @override void initState() { - _username = Configuration.instance.getUsername(); - _password = Configuration.instance.getPassword(); super.initState(); } @override Widget build(BuildContext context) { + if (Configuration.instance.getToken() == null) { + // Has probably not signed up + return _getSignUpWidget(context); + } else { + return _getSignInWidget(context); + } + } + + Widget _getSignUpWidget(BuildContext context) { return Container( child: Column( children: [ + Padding( + padding: const EdgeInsets.fromLTRB(0, 16, 0, 0), + child: Text("Create an account to get started"), + ), TextFormField( - initialValue: _username, decoration: InputDecoration( hintText: 'username', contentPadding: EdgeInsets.all(20), @@ -45,7 +55,75 @@ class _SignInWidgetState extends State { }, ), TextFormField( - initialValue: _password, + decoration: InputDecoration( + hintText: 'password', + contentPadding: EdgeInsets.all(20), + ), + autocorrect: false, + obscureText: true, + onChanged: (value) { + setState(() { + _password = value; + }); + }, + ), + TextFormField( + decoration: InputDecoration( + hintText: 'repeat password', + contentPadding: EdgeInsets.all(20), + ), + autocorrect: false, + obscureText: true, + onChanged: (value) { + setState(() { + _repeatedPassword = value; + }); + }, + ), + CupertinoButton( + child: Text("Sign Up"), + onPressed: () async { + if (_password != _repeatedPassword) { + _showPasswordMismatchDialog(); + } else { + try { + final userCreated = await UserAuthenticator.instance + .create(_username, _password); + if (userCreated) { + Navigator.of(context).pop(); + } else { + _showGenericErrorDialog(); + } + } catch (e) { + _showGenericErrorDialog(error: e); + } + } + }, + ), + ], + )); + } + + Widget _getSignInWidget(BuildContext context) { + return Container( + child: Column( + children: [ + TextFormField( + initialValue: Configuration.instance.getUsername(), + decoration: InputDecoration( + hintText: 'username', + contentPadding: EdgeInsets.all(20), + ), + autofocus: true, + autocorrect: false, + onChanged: (value) { + setState(() { + _username = value; + }); + }, + ), + TextFormField( + initialValue: Configuration.instance.getPassword(), decoration: InputDecoration( hintText: 'password', contentPadding: EdgeInsets.all(20), @@ -66,7 +144,7 @@ class _SignInWidgetState extends State { if (loggedIn) { Navigator.of(context).pop(); } else { - _showErrorDialog(); + _showAuthenticationFailedErrorDialog(); } }, ), @@ -74,7 +152,57 @@ class _SignInWidgetState extends State { )); } - void _showErrorDialog() { + void _showPasswordMismatchDialog() { + showDialog( + context: context, + barrierDismissible: false, // user must tap button! + builder: (BuildContext context) { + return CupertinoAlertDialog( + title: Text("Passwords don't match"), + content: Padding( + padding: const EdgeInsets.fromLTRB(0, 16, 0, 0), + child: Text("Please make sure that the passwords you enter match."), + ), + actions: [ + CupertinoDialogAction( + child: Text('OK'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ], + ); + }, + ); + } + + void _showGenericErrorDialog({Exception error}) { + showDialog( + context: context, + barrierDismissible: false, // user must tap button! + builder: (BuildContext context) { + return CupertinoAlertDialog( + title: Text("Ooops."), + content: Padding( + padding: const EdgeInsets.fromLTRB(0, 16, 0, 0), + child: error == null + ? Text("Something went wrong.") + : Text(error.toString()), + ), + actions: [ + CupertinoDialogAction( + child: Text('OK'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ], + ); + }, + ); + } + + void _showAuthenticationFailedErrorDialog() { showDialog( context: context, barrierDismissible: false, // user must tap button! @@ -82,7 +210,7 @@ class _SignInWidgetState extends State { return CupertinoAlertDialog( title: Text('Login failed'), content: Padding( - padding: const EdgeInsets.fromLTRB(0, 8, 0, 0), + padding: const EdgeInsets.fromLTRB(0, 16, 0, 0), child: Text( 'Please make sure that the credentials entered are correct.'), ), diff --git a/lib/user_authenticator.dart b/lib/user_authenticator.dart index f8310c972..d2e0810f7 100644 --- a/lib/user_authenticator.dart +++ b/lib/user_authenticator.dart @@ -35,4 +35,30 @@ class UserAuthenticator { return false; }); } + + Future create(String username, String password) { + return _dio.post( + "http://" + Configuration.instance.getEndpoint() + ":8080/users/create", + queryParameters: { + "username": username, + "password": password + }).then((response) { + if (response.statusCode == 200 && response.data != null) { + Configuration.instance.setUsername(username); + Configuration.instance.setPassword(password); + Configuration.instance.setToken(response.data["token"]); + Bus.instance.fire(UserAuthenticatedEvent()); + return true; + } else { + if (response.data != null && response.data["message"] != null) { + throw Exception(response.data["message"]); + } else { + throw Exception("Something went wrong"); + } + } + }).catchError((e) { + _logger.e(e.toString()); + throw e; + }); + } }