38 lines
1011 B
Dart
38 lines
1011 B
Dart
import 'dart:convert';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:pcasttv/globals.dart';
|
|
import 'package:pcasttv/models/login_model.dart';
|
|
|
|
class AuthService {
|
|
Future<LoginModel> login(String email, String password) async {
|
|
http.Response response;
|
|
|
|
final data = {
|
|
"email": email,
|
|
"password": password,
|
|
};
|
|
|
|
try {
|
|
response = await http.post(
|
|
Uri.parse(Endpoints.login),
|
|
headers: {
|
|
'Content-Type': 'application/json; charset=utf-8',
|
|
'Accept': 'application/json',
|
|
},
|
|
body: jsonEncode(data),
|
|
);
|
|
|
|
switch (response.statusCode) {
|
|
case 200:
|
|
var authModel = LoginModel.fromJson(utf8.decode(response.bodyBytes).toString());
|
|
return authModel;
|
|
default:
|
|
throw Exception('Erro inesperado no login: ${response.statusCode} - ${response.body.toString()}');
|
|
}
|
|
} on Exception catch (e) {
|
|
throw Exception('Exception inesperada no login: $e');
|
|
}
|
|
}
|
|
}
|