Files
rog_app/lib/services/auth_service.dart

186 lines
5.7 KiB
Dart
Raw Normal View History

2022-05-12 02:17:08 +05:30
import 'dart:convert';
import 'package:http/http.dart' as http;
2023-10-06 16:25:21 +05:30
import 'package:rogapp/model/auth_user.dart';
2022-05-12 02:17:08 +05:30
2022-06-14 14:37:59 +05:30
import '../utils/const.dart';
2023-10-06 16:25:21 +05:30
class AuthService {
Future<AuthUser?> userLogin(String email, String password) async {
final serverUrl = ConstValues.currentServer();
final url = '$serverUrl/api/login/';
try {
final http.Response response = await http.post(
Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body:
jsonEncode(<String, String>{'email': email, 'password': password}),
);
switch (response.statusCode) {
case 200:
final data = json.decode(utf8.decode(response.bodyBytes));
AuthUser user = AuthUser.fromMap(data["user"]);
final String token = data["token"];
user.auth_token = token;
return user;
default:
return null;
//throw Exception(response.reasonPhrase);
}
} on Exception catch (_) {
rethrow;
}
}
2022-05-12 02:17:08 +05:30
2023-10-06 16:25:21 +05:30
Future<AuthUser?> userFromToken(String token) async {
final serverUrl = ConstValues.currentServer();
final url = '$serverUrl/api/user/';
try {
final http.Response response =
await http.get(Uri.parse(url), headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Token $token'
});
switch (response.statusCode) {
case 200:
final data = json.decode(utf8.decode(response.bodyBytes));
AuthUser user = AuthUser.fromMap(data);
user.auth_token = token;
return user;
default:
return null;
//throw Exception(response.reasonPhrase);
}
} on Exception catch (_) {
rethrow;
}
}
2022-05-12 02:17:08 +05:30
2023-10-06 16:25:21 +05:30
static Future<Map<String, dynamic>> changePassword(
String oldpassword, String newpassword, String token) async {
2022-10-12 21:46:17 +05:30
Map<String, dynamic> changePassword = {};
2023-08-16 14:53:32 +05:30
String serverUrl = ConstValues.currentServer();
String url = '$serverUrl/api/change-password/';
2023-10-06 16:25:21 +05:30
//print('++++++++$url');
2022-10-12 21:46:17 +05:30
final http.Response response = await http.put(
Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
2023-08-16 14:53:32 +05:30
'Authorization': 'Token $token'
2022-10-12 21:46:17 +05:30
},
body: jsonEncode(<String, String>{
'old_password': oldpassword,
'new_password': newpassword
}),
);
if (response.statusCode == 200) {
2023-10-06 16:25:21 +05:30
changePassword = json.decode(utf8.decode(response.bodyBytes));
2022-10-12 21:46:17 +05:30
}
return changePassword;
}
2023-10-06 16:25:21 +05:30
static Future<Map<String, dynamic>> login(
String email, String password) async {
//print("------- in logged email $email pwd $password ###### --------");
2022-05-12 02:17:08 +05:30
Map<String, dynamic> cats = {};
2023-08-16 14:53:32 +05:30
String serverUrl = ConstValues.currentServer();
String url = '$serverUrl/api/login/';
2023-10-06 16:25:21 +05:30
//print('++++++++$url');
2022-06-14 14:37:59 +05:30
//String url = 'http://localhost:8100/api/login/';
2022-05-12 02:17:08 +05:30
final http.Response response = await http.post(
Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
2023-10-06 16:25:21 +05:30
body: jsonEncode(<String, String>{'email': email, 'password': password}),
2022-05-12 02:17:08 +05:30
);
if (response.statusCode == 200) {
cats = json.decode(utf8.decode(response.bodyBytes));
}
return cats;
}
2023-10-06 16:25:21 +05:30
static Future<Map<String, dynamic>> register(
String email, String password) async {
2022-05-12 02:17:08 +05:30
Map<String, dynamic> cats = {};
2023-08-16 14:53:32 +05:30
String serverUrl = ConstValues.currentServer();
String url = '$serverUrl/api/register/';
2023-10-06 16:25:21 +05:30
//print('++++++++$url');
2022-05-12 02:17:08 +05:30
final http.Response response = await http.post(
Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
2023-10-06 16:25:21 +05:30
body: jsonEncode(<String, String>{'email': email, 'password': password}),
2022-05-12 02:17:08 +05:30
);
2023-10-06 16:25:21 +05:30
//print(response.body);
2022-05-12 02:17:08 +05:30
if (response.statusCode == 200) {
cats = json.decode(utf8.decode(response.bodyBytes));
}
return cats;
}
2023-10-06 16:25:21 +05:30
static Future<Map<String, dynamic>> deleteUser(String token) async {
2023-01-06 16:48:47 +05:30
Map<String, dynamic> cats = {};
2023-08-16 14:53:32 +05:30
String serverUrl = ConstValues.currentServer();
String url = '$serverUrl/api/delete-account/';
2023-10-06 16:25:21 +05:30
//print('++++++++$url');
final http.Response response =
await http.get(Uri.parse(url), headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Token $token'
});
2023-01-06 16:48:47 +05:30
if (response.statusCode == 200) {
cats = json.decode(utf8.decode(response.bodyBytes));
}
return cats;
}
2023-10-06 16:25:21 +05:30
static Future<List<dynamic>?> userDetails(int userid) async {
2022-06-14 14:37:59 +05:30
List<dynamic> cats = [];
2023-08-16 14:53:32 +05:30
String serverUrl = ConstValues.currentServer();
String url = '$serverUrl/api/userdetials?user_id=$userid';
2023-10-06 16:25:21 +05:30
//print('++++++++$url');
//print("---- UserDetails url is $url");
final response = await http.get(
Uri.parse(url),
2022-06-14 14:37:59 +05:30
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
if (response.statusCode == 200) {
cats = json.decode(utf8.decode(response.bodyBytes));
}
return cats;
}
2023-09-04 22:46:53 +05:30
static Future<List<dynamic>?> userForToken(String token) async {
Map<String, dynamic> cats = {};
String serverUrl = ConstValues.currentServer();
String url = '$serverUrl/api/user/';
2023-10-06 16:25:21 +05:30
//print('++++++++$url');
//print("---- UserDetails url is $url");
final response = await http.get(
Uri.parse(url),
2023-09-04 22:46:53 +05:30
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
2023-10-06 16:25:21 +05:30
'Authorization': 'Token $token'
2023-09-04 22:46:53 +05:30
},
);
if (response.statusCode == 200) {
cats = json.decode(utf8.decode(response.bodyBytes));
2023-10-06 16:25:21 +05:30
//print("--- eeeeee $cats ----");
2023-09-04 22:46:53 +05:30
}
2023-10-06 16:25:21 +05:30
return [
{"user": cats, "token": token}
];
2023-09-04 22:46:53 +05:30
}
2022-05-12 02:17:08 +05:30
}