Add user sign up
This commit is contained in:
parent
2cdb268b80
commit
f5e7cecc03
3 changed files with 168 additions and 13 deletions
|
@ -13,14 +13,13 @@ class SetupPage extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _SetupPageState extends State<SetupPage> {
|
||||
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<SetupPage> {
|
|||
}
|
||||
|
||||
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(() {
|
||||
|
|
|
@ -17,21 +17,31 @@ class SignInWidget extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _SignInWidgetState extends State<SignInWidget> {
|
||||
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: <Widget>[
|
||||
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<SignInWidget> {
|
|||
},
|
||||
),
|
||||
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: <Widget>[
|
||||
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<SignInWidget> {
|
|||
if (loggedIn) {
|
||||
Navigator.of(context).pop();
|
||||
} else {
|
||||
_showErrorDialog();
|
||||
_showAuthenticationFailedErrorDialog();
|
||||
}
|
||||
},
|
||||
),
|
||||
|
@ -74,7 +152,57 @@ class _SignInWidgetState extends State<SignInWidget> {
|
|||
));
|
||||
}
|
||||
|
||||
void _showErrorDialog() {
|
||||
void _showPasswordMismatchDialog() {
|
||||
showDialog<void>(
|
||||
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: <Widget>[
|
||||
CupertinoDialogAction(
|
||||
child: Text('OK'),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _showGenericErrorDialog({Exception error}) {
|
||||
showDialog<void>(
|
||||
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: <Widget>[
|
||||
CupertinoDialogAction(
|
||||
child: Text('OK'),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _showAuthenticationFailedErrorDialog() {
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false, // user must tap button!
|
||||
|
@ -82,7 +210,7 @@ class _SignInWidgetState extends State<SignInWidget> {
|
|||
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.'),
|
||||
),
|
||||
|
|
|
@ -35,4 +35,30 @@ class UserAuthenticator {
|
|||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
Future<bool> 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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue