@@ -83,8 +83,7 @@ class AuthProvider with ChangeNotifier {
83
83
/// Server application version
84
84
Future <void > setServerVersion () async {
85
85
final response = await client.get (makeUri (serverUrl! , SERVER_VERSION_URL ));
86
- final responseData = json.decode (response.body);
87
- serverVersion = responseData;
86
+ serverVersion = json.decode (response.body);
88
87
}
89
88
90
89
Future <void > initData (String serverUrl) async {
@@ -115,41 +114,39 @@ class AuthProvider with ChangeNotifier {
115
114
}
116
115
117
116
/// Registers a new user
118
- Future <Map <String , LoginActions >> register (
119
- {required String username,
120
- required String password,
121
- required String email,
122
- required String serverUrl}) async {
117
+ Future <Map <String , LoginActions >> register ({
118
+ required String username,
119
+ required String password,
120
+ required String email,
121
+ required String serverUrl,
122
+ String locale = 'en' ,
123
+ }) async {
123
124
// Register
124
- try {
125
- final Map <String , String > data = {'username' : username, 'password' : password};
126
- if (email != '' ) {
127
- data['email' ] = email;
128
- }
129
- final response = await client.post (
130
- makeUri (serverUrl, REGISTRATION_URL ),
131
- headers: {
132
- HttpHeaders .contentTypeHeader: 'application/json; charset=UTF-8' ,
133
- HttpHeaders .authorizationHeader: 'Token ${metadata [MANIFEST_KEY_API ]}' ,
134
- HttpHeaders .userAgentHeader: getAppNameHeader (),
135
- },
136
- body: json.encode (data),
137
- );
138
- final responseData = json.decode (response.body);
139
-
140
- if (response.statusCode >= 400 ) {
141
- throw WgerHttpException (responseData);
142
- }
143
-
144
- // If update is required don't log in user
145
- if (await applicationUpdateRequired ()) {
146
- return {'action' : LoginActions .update};
147
- }
148
-
149
- return login (username, password, serverUrl);
150
- } catch (error) {
151
- rethrow ;
125
+ final Map <String , String > data = {'username' : username, 'password' : password};
126
+ if (email != '' ) {
127
+ data['email' ] = email;
128
+ }
129
+ final response = await client.post (
130
+ makeUri (serverUrl, REGISTRATION_URL ),
131
+ headers: {
132
+ HttpHeaders .contentTypeHeader: 'application/json; charset=UTF-8' ,
133
+ HttpHeaders .authorizationHeader: 'Token ${metadata [MANIFEST_KEY_API ]}' ,
134
+ HttpHeaders .userAgentHeader: getAppNameHeader (),
135
+ HttpHeaders .acceptLanguageHeader: locale
136
+ },
137
+ body: json.encode (data),
138
+ );
139
+
140
+ if (response.statusCode >= 400 ) {
141
+ throw WgerHttpException (response.body);
142
+ }
143
+
144
+ // If update is required don't log in user
145
+ if (await applicationUpdateRequired ()) {
146
+ return {'action' : LoginActions .update};
152
147
}
148
+
149
+ return login (username, password, serverUrl);
153
150
}
154
151
155
152
/// Authenticates a user
@@ -160,48 +157,44 @@ class AuthProvider with ChangeNotifier {
160
157
) async {
161
158
await logout (shouldNotify: false );
162
159
163
- try {
164
- final response = await client.post (
165
- makeUri (serverUrl, LOGIN_URL ),
166
- headers: < String , String > {
167
- HttpHeaders .contentTypeHeader: 'application/json; charset=UTF-8' ,
168
- HttpHeaders .userAgentHeader: getAppNameHeader (),
169
- },
170
- body: json.encode ({'username' : username, 'password' : password}),
171
- );
172
- final responseData = json.decode (response.body);
173
-
174
- if (response.statusCode >= 400 ) {
175
- throw WgerHttpException (responseData);
176
- }
177
-
178
- await initData (serverUrl);
179
-
180
- // If update is required don't log in user
181
- if (await applicationUpdateRequired ()) {
182
- return {'action' : LoginActions .update};
183
- }
184
-
185
- // Log user in
186
- token = responseData['token' ];
187
- notifyListeners ();
160
+ final response = await client.post (
161
+ makeUri (serverUrl, LOGIN_URL ),
162
+ headers: < String , String > {
163
+ HttpHeaders .contentTypeHeader: 'application/json; charset=UTF-8' ,
164
+ HttpHeaders .userAgentHeader: getAppNameHeader (),
165
+ },
166
+ body: json.encode ({'username' : username, 'password' : password}),
167
+ );
168
+ final responseData = json.decode (response.body);
188
169
189
- // store login data in shared preferences
190
- final prefs = await SharedPreferences .getInstance ();
191
- final userData = json.encode ({
192
- 'token' : token,
193
- 'serverUrl' : this .serverUrl,
194
- });
195
- final serverData = json.encode ({
196
- 'serverUrl' : this .serverUrl,
197
- });
198
-
199
- prefs.setString ('userData' , userData);
200
- prefs.setString ('lastServer' , serverData);
201
- return {'action' : LoginActions .proceed};
202
- } catch (error) {
203
- rethrow ;
170
+ if (response.statusCode >= 400 ) {
171
+ throw WgerHttpException (response.body);
204
172
}
173
+
174
+ await initData (serverUrl);
175
+
176
+ // If update is required don't log in user
177
+ if (await applicationUpdateRequired ()) {
178
+ return {'action' : LoginActions .update};
179
+ }
180
+
181
+ // Log user in
182
+ token = responseData['token' ];
183
+ notifyListeners ();
184
+
185
+ // store login data in shared preferences
186
+ final prefs = await SharedPreferences .getInstance ();
187
+ final userData = json.encode ({
188
+ 'token' : token,
189
+ 'serverUrl' : this .serverUrl,
190
+ });
191
+ final serverData = json.encode ({
192
+ 'serverUrl' : this .serverUrl,
193
+ });
194
+
195
+ prefs.setString ('userData' , userData);
196
+ prefs.setString ('lastServer' , serverData);
197
+ return {'action' : LoginActions .proceed};
205
198
}
206
199
207
200
/// Loads the last server URL from which the user successfully logged in
0 commit comments