optimized
This commit is contained in:
@ -1,16 +1,69 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:rogapp/model/auth_user.dart';
|
||||
|
||||
import '../utils/const.dart';
|
||||
|
||||
class AuthService {
|
||||
Future<AuthUser?> userLogin(String email, String password) async {
|
||||
final serverUrl = ConstValues.currentServer();
|
||||
final url = '$serverUrl/api/login/';
|
||||
|
||||
class AuthService{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
static Future<Map<String, dynamic>> changePassword(String oldpassword, String newpassword, String token) async {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
static Future<Map<String, dynamic>> changePassword(
|
||||
String oldpassword, String newpassword, String token) async {
|
||||
Map<String, dynamic> changePassword = {};
|
||||
String serverUrl = ConstValues.currentServer();
|
||||
String url = '$serverUrl/api/change-password/';
|
||||
print('++++++++$url');
|
||||
//print('++++++++$url');
|
||||
final http.Response response = await http.put(
|
||||
Uri.parse(url),
|
||||
headers: <String, String>{
|
||||
@ -24,28 +77,25 @@ class AuthService{
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
changePassword = json.decode(utf8.decode(response.bodyBytes));
|
||||
changePassword = json.decode(utf8.decode(response.bodyBytes));
|
||||
}
|
||||
return changePassword;
|
||||
}
|
||||
|
||||
|
||||
static Future<Map<String, dynamic>> login(String email, String password) async {
|
||||
print("------- in logged email $email pwd $password ###### --------");
|
||||
static Future<Map<String, dynamic>> login(
|
||||
String email, String password) async {
|
||||
//print("------- in logged email $email pwd $password ###### --------");
|
||||
Map<String, dynamic> cats = {};
|
||||
String serverUrl = ConstValues.currentServer();
|
||||
String url = '$serverUrl/api/login/';
|
||||
print('++++++++$url');
|
||||
//print('++++++++$url');
|
||||
//String url = 'http://localhost:8100/api/login/';
|
||||
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
|
||||
}),
|
||||
body: jsonEncode(<String, String>{'email': email, 'password': password}),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
@ -54,41 +104,36 @@ class AuthService{
|
||||
return cats;
|
||||
}
|
||||
|
||||
|
||||
static Future<Map<String, dynamic>> register(String email, String password) async {
|
||||
static Future<Map<String, dynamic>> register(
|
||||
String email, String password) async {
|
||||
Map<String, dynamic> cats = {};
|
||||
String serverUrl = ConstValues.currentServer();
|
||||
String url = '$serverUrl/api/register/';
|
||||
print('++++++++$url');
|
||||
//print('++++++++$url');
|
||||
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
|
||||
}),
|
||||
body: jsonEncode(<String, String>{'email': email, 'password': password}),
|
||||
);
|
||||
print(response.body);
|
||||
//print(response.body);
|
||||
if (response.statusCode == 200) {
|
||||
cats = json.decode(utf8.decode(response.bodyBytes));
|
||||
}
|
||||
return cats;
|
||||
}
|
||||
|
||||
static Future<Map<String, dynamic>> deleteUser(String token) async {
|
||||
static Future<Map<String, dynamic>> deleteUser(String token) async {
|
||||
Map<String, dynamic> cats = {};
|
||||
String serverUrl = ConstValues.currentServer();
|
||||
String url = '$serverUrl/api/delete-account/';
|
||||
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'
|
||||
}
|
||||
);
|
||||
//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'
|
||||
});
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
cats = json.decode(utf8.decode(response.bodyBytes));
|
||||
@ -96,50 +141,45 @@ class AuthService{
|
||||
return cats;
|
||||
}
|
||||
|
||||
|
||||
static Future<List<dynamic>?> UserDetails(int userid) async {
|
||||
static Future<List<dynamic>?> userDetails(int userid) async {
|
||||
List<dynamic> cats = [];
|
||||
String serverUrl = ConstValues.currentServer();
|
||||
String url = '$serverUrl/api/userdetials?user_id=$userid';
|
||||
print('++++++++$url');
|
||||
print("---- UserDetails url is $url");
|
||||
final response = await http.get(Uri.parse(url),
|
||||
//print('++++++++$url');
|
||||
//print("---- UserDetails url is $url");
|
||||
final response = await http.get(
|
||||
Uri.parse(url),
|
||||
headers: <String, String>{
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
|
||||
cats = json.decode(utf8.decode(response.bodyBytes));
|
||||
}
|
||||
return cats;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static Future<List<dynamic>?> userForToken(String token) async {
|
||||
Map<String, dynamic> cats = {};
|
||||
String serverUrl = ConstValues.currentServer();
|
||||
String url = '$serverUrl/api/user/';
|
||||
print('++++++++$url');
|
||||
print("---- UserDetails url is $url");
|
||||
final response = await http.get(Uri.parse(url),
|
||||
//print('++++++++$url');
|
||||
//print("---- UserDetails url is $url");
|
||||
final response = await http.get(
|
||||
Uri.parse(url),
|
||||
headers: <String, String>{
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
'Authorization': 'Token $token'
|
||||
'Authorization': 'Token $token'
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
|
||||
cats = json.decode(utf8.decode(response.bodyBytes));
|
||||
print("--- eeeeee $cats ----");
|
||||
//print("--- eeeeee $cats ----");
|
||||
}
|
||||
return [{"user":cats, "token":token}];
|
||||
return [
|
||||
{"user": cats, "token": token}
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user