Skip to content

Commit

Permalink
Fix login request to using FormData() instead of raw JSON #115
Browse files Browse the repository at this point in the history
  • Loading branch information
up2code committed Mar 4, 2021
1 parent 805fff8 commit d8542f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 4 additions & 2 deletions lib/src/services/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ class AuthService extends GetxService {
}

Future<UserModel> getCurrent() async {
Map<String, String> data = {'fields': 'MainPicture'};
try {
UserModel us = await httpService.get('/api/users/current',
{'fields': 'MainPicture'}).then((item) => UserModel.fromJson(item));
UserModel us = await httpService
.get('/api/users/current', data)
.then((item) => UserModel.fromJson(item));
print('current user : $us');
return us;
} catch (e) {
Expand Down
12 changes: 7 additions & 5 deletions lib/src/services/http_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:cookie_jar/cookie_jar.dart';
import 'package:dio/dio.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:dio_http_cache/dio_http_cache.dart';
import 'package:get/get.dart';
import 'package:get/get.dart' hide FormData;
import 'package:vocadb_app/constants.dart';
import 'package:vocadb_app/exceptions.dart';
import 'package:vocadb_app/models.dart';
Expand All @@ -26,8 +26,7 @@ class HttpService extends GetxService {
}

Future<dynamic> get(String endpoint, Map<String, String> params) async {
params?.removeWhere((key, value) => value == null || value.isEmpty);

params?.removeWhere((key, value) => value == null || value == '');
String url = Uri.https(authority, endpoint, params).toString();
print('GET $url | $params');
final response =
Expand Down Expand Up @@ -62,13 +61,16 @@ class HttpService extends GetxService {

Future<UserCookie> login(String username, String password) async {
String url = Uri.https(authority, '/User/Login').toString();
Map<String, dynamic> data = {
'UserName': username,
'Password': password,
};
try {
await _dio.post(url, data: {'UserName': username, 'Password': password});
await _dio.post(url, data: FormData.fromMap(data));
throw LoginFailedException();
} catch (e) {
if (e is DioError && e.response.statusCode == 302) {
List<String> cookies = e.response.headers.map['set-cookie'];

if (cookies != null) {
return UserCookie(cookies: cookies);
}
Expand Down

0 comments on commit d8542f5

Please sign in to comment.