re factor rog
This commit is contained in:
44
lib/features/services/auth_service.dart
Normal file
44
lib/features/services/auth_service.dart
Normal file
@ -0,0 +1,44 @@
|
||||
import 'package:rogapp/features/data/user.dart';
|
||||
|
||||
import '../utils/const.dart';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
class AuthService {
|
||||
Dio dio = Dio();
|
||||
|
||||
Future<User?> userLogin(String email, String password) async {
|
||||
final String serverUrl = ConstValues.currentServer();
|
||||
final String url = '$serverUrl/api/login/';
|
||||
|
||||
try {
|
||||
final response = await dio.post(
|
||||
url,
|
||||
data: {
|
||||
'email': email,
|
||||
'password': password,
|
||||
},
|
||||
options: Options(
|
||||
headers: {
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200 && response.data != null) {
|
||||
final data = response.data;
|
||||
User user = User.fromMap(data["user"]);
|
||||
final String token = data["token"];
|
||||
user.auth_token = token;
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
} on DioException catch (e) {
|
||||
print("Dio error: ${e.response?.statusCode} - ${e.message}");
|
||||
return null;
|
||||
} catch (e) {
|
||||
print("Unexpected error: $e");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user