Browse Source

Ensure that the userID is saved after both login and signup events

Vishnu Mohandas 4 years ago
parent
commit
b1b67913bd
1 changed files with 9 additions and 7 deletions
  1. 9 7
      lib/user_authenticator.dart

+ 9 - 7
lib/user_authenticator.dart

@@ -22,10 +22,7 @@ class UserAuthenticator {
           "password": password,
         }).then((response) {
       if (response.statusCode == 200 && response.data != null) {
-        Configuration.instance.setUsername(username);
-        Configuration.instance.setPassword(password);
-        Configuration.instance.setUserID(response.data["id"]);
-        Configuration.instance.setToken(response.data["token"]);
+        _saveConfiguration(username, password, response);
         Bus.instance.fire(UserAuthenticatedEvent());
         return true;
       } else {
@@ -44,9 +41,7 @@ class UserAuthenticator {
       "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"]);
+        _saveConfiguration(username, password, response);
         Bus.instance.fire(UserAuthenticatedEvent());
         return true;
       } else {
@@ -61,4 +56,11 @@ class UserAuthenticator {
       throw e;
     });
   }
+
+  void _saveConfiguration(String username, String password, Response response) {
+    Configuration.instance.setUsername(username);
+    Configuration.instance.setPassword(password);
+    Configuration.instance.setUserID(response.data["id"]);
+    Configuration.instance.setToken(response.data["token"]);
+  }
 }