mirror of
https://git.meshkee.com/applications/cssd.git
synced 2026-10-13 20:08:55 +03:30
beta commit
This commit is contained in:
+255
-249
@@ -1,249 +1,255 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../config/urls.dart';
|
||||
import '../config/user.dart';
|
||||
|
||||
class Auth with ChangeNotifier {
|
||||
String _token;
|
||||
DateTime _expiaryDate;
|
||||
|
||||
Map<String, dynamic> _user = {};
|
||||
|
||||
bool get isAuth {
|
||||
return token != null;
|
||||
}
|
||||
|
||||
String get token {
|
||||
if (_expiaryDate != null &&
|
||||
_expiaryDate.isAfter(DateTime.now()) &&
|
||||
_token != null) {
|
||||
return _token;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, dynamic> get user {
|
||||
return {..._user};
|
||||
}
|
||||
|
||||
Future<String> requestCode(String cellNumber) async {
|
||||
final response = await http.post(
|
||||
Uri.parse(Url.requestResetPass),
|
||||
body: {
|
||||
'cell_number': cellNumber,
|
||||
},
|
||||
// headers: {'X-Requested-With': 'XMLHttpRequest'},
|
||||
);
|
||||
final responseData = json.decode(response.body);
|
||||
if (responseData['status'] == 'Faild') {
|
||||
return 'NotUser';
|
||||
} else {
|
||||
return 'Done';
|
||||
}
|
||||
// print(responseData);
|
||||
}
|
||||
|
||||
Future<String> sendCode(String code) async {
|
||||
final response = await http.post(
|
||||
Uri.parse(Url.verifyResetPass),
|
||||
body: {
|
||||
'code': code,
|
||||
},
|
||||
// headers: {'X-Requested-With': 'XMLHttpRequest'},
|
||||
);
|
||||
final responseData = json.decode(response.body);
|
||||
// if (responseData['status']=='Faild') {
|
||||
// return 'NotUser';
|
||||
// } else {
|
||||
// return 'Done';
|
||||
// }
|
||||
print(responseData);
|
||||
if (responseData['status'] == 'Faild') {
|
||||
return 'wrong';
|
||||
} else {
|
||||
return responseData['access_token'];
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> sendRePass(String password, String token) async {
|
||||
print(password);
|
||||
print(token);
|
||||
|
||||
final response = await http.post(
|
||||
Uri.parse(Url.sendResetPass),
|
||||
body: {
|
||||
'password': password,
|
||||
'password_confirmation': password,
|
||||
},
|
||||
headers: {'Authorization': token, 'X-Requested-With': 'XMLHttpRequest'},
|
||||
// headers: {'X-Requested-With': 'XMLHttpRequest'},
|
||||
);
|
||||
final responseData = json.decode(response.body);
|
||||
// if (responseData['status']=='Faild') {
|
||||
// return 'NotUser';
|
||||
// } else {
|
||||
// return 'Done';
|
||||
// }
|
||||
print(responseData);
|
||||
if (responseData['status'] == 'Success') {
|
||||
return 'done';
|
||||
} else {
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> login(String cellNumber, String password) async {
|
||||
final response = await http.post(
|
||||
Uri.parse(Url.login),
|
||||
body: {
|
||||
'cell_number': cellNumber,
|
||||
'password': password,
|
||||
},
|
||||
// headers: {'X-Requested-With': 'XMLHttpRequest'},
|
||||
);
|
||||
final responseData = json.decode(response.body);
|
||||
print(responseData);
|
||||
if (response.statusCode == 401) {
|
||||
if (responseData['message'] == 'The user credentials were incorrect.') {
|
||||
return 'badinput'; // incorrect user or pass
|
||||
} else {
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
_token = responseData['access_token'];
|
||||
_expiaryDate = DateTime.now().add(
|
||||
Duration(
|
||||
seconds: responseData['expires_in'],
|
||||
),
|
||||
);
|
||||
User.token = _token;
|
||||
// print(isAuth);
|
||||
|
||||
// print(User.token);
|
||||
var localUser = await getUserData();
|
||||
// print("------ in auth ===========");
|
||||
print(localUser);
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final userData = json.encode({
|
||||
'token': _token,
|
||||
'expiaryDate': _expiaryDate.toIso8601String(),
|
||||
'user': user
|
||||
});
|
||||
prefs.setString('userData', userData);
|
||||
notifyListeners();
|
||||
return 'true';
|
||||
}
|
||||
|
||||
Future<bool> tryAutoLogin() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
if (!prefs.containsKey('userData')) {
|
||||
return false;
|
||||
}
|
||||
final userData =
|
||||
json.decode(prefs.getString('userData')) as Map<String, Object>;
|
||||
final expiaryDate = DateTime.parse(userData['expiaryDate']);
|
||||
if (expiaryDate.isBefore(DateTime.now())) {
|
||||
return false;
|
||||
}
|
||||
_token = userData['token'];
|
||||
_expiaryDate = expiaryDate;
|
||||
_user = userData['user'];
|
||||
User.token = _token;
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> getUserData() async {
|
||||
final response = await http.get(
|
||||
Uri.parse(Url.getUserInfo),
|
||||
headers: {'Authorization': 'Bearer ' + token},
|
||||
);
|
||||
// print(response.body);
|
||||
if (response.statusCode == 200) {
|
||||
var data = json.decode(response.body)['data'];
|
||||
_user.addAll(
|
||||
{
|
||||
'id': data['user']['id'],
|
||||
'name': data['user']['name'],
|
||||
'email': data['user']['email'],
|
||||
'cell_number': data['user']['cell_number'],
|
||||
'address': data['user']['address'],
|
||||
'gender': data['user']['gender'],
|
||||
'medical_center': data['user']['medical_center']['name'],
|
||||
'roles': data['roles'],
|
||||
},
|
||||
);
|
||||
// print(_user);
|
||||
notifyListeners();
|
||||
return _user;
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setUserData(id, name, email) async {
|
||||
var response = await http.post(
|
||||
Uri.parse('https://cssd-doc.com/api/UpdateInfo'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + token,
|
||||
HttpHeaders.contentTypeHeader: 'application/json',
|
||||
},
|
||||
body: json.encode(
|
||||
{
|
||||
"id": id,
|
||||
"name": name,
|
||||
"email": email,
|
||||
// "country_code": "0098",
|
||||
// "cell_number": "09338736511",
|
||||
// "gender": "Male",
|
||||
// "verified": "Verified",
|
||||
// "status": "Active",
|
||||
// "medical_center_id": 1,
|
||||
// "address": null,
|
||||
// "created_at": null,
|
||||
// "updated_at": "2020-04-23 21:54:52",
|
||||
// "medical_center": {
|
||||
// "id": 1,
|
||||
// "name": "B",
|
||||
// "address": "1",
|
||||
// "phone_number": "12345678951",
|
||||
// "city_id": 1,
|
||||
// "created_at": null,
|
||||
// "updated_at": "2020-01-12 05:38:35"
|
||||
// },
|
||||
// "roles": [
|
||||
// {
|
||||
// "id": 1,
|
||||
// "name": "Admin",
|
||||
// "guard_name": "web",
|
||||
// "created_at": null,
|
||||
// "updated_at": null,
|
||||
// "pivot": {
|
||||
// "model_id": 1,
|
||||
// "role_id": 1,
|
||||
// "model_type": "App\\User",
|
||||
// },
|
||||
// },
|
||||
// ],
|
||||
},
|
||||
),
|
||||
);
|
||||
print(response.body);
|
||||
}
|
||||
|
||||
void logout() async {
|
||||
_token = null;
|
||||
_expiaryDate = null;
|
||||
_user = {};
|
||||
User.token = null;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
prefs.remove('userData');
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../config/urls.dart';
|
||||
import '../config/user.dart';
|
||||
|
||||
class Auth with ChangeNotifier {
|
||||
String? _token;
|
||||
DateTime? _expiaryDate;
|
||||
|
||||
Map<String, dynamic> _user = {};
|
||||
|
||||
bool get isAuth {
|
||||
return token != null;
|
||||
}
|
||||
|
||||
String? get token {
|
||||
if (_expiaryDate != null &&
|
||||
_expiaryDate!.isAfter(DateTime.now()) &&
|
||||
_token != null) {
|
||||
return _token!;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, dynamic> get user {
|
||||
return {..._user};
|
||||
}
|
||||
|
||||
Future<String> requestCode(String cellNumber) async {
|
||||
final response = await http.post(
|
||||
Uri.parse(Url.requestResetPass),
|
||||
body: {
|
||||
'cell_number': cellNumber,
|
||||
},
|
||||
// headers: {'X-Requested-With': 'XMLHttpRequest'},
|
||||
);
|
||||
final responseData = json.decode(response.body);
|
||||
if (responseData['status'] == 'Faild') {
|
||||
return 'NotUser';
|
||||
} else {
|
||||
return 'Done';
|
||||
}
|
||||
// print(responseData);
|
||||
}
|
||||
|
||||
Future<String> sendCode(String code) async {
|
||||
final response = await http.post(
|
||||
Uri.parse(Url.verifyResetPass),
|
||||
body: {
|
||||
'code': code,
|
||||
},
|
||||
// headers: {'X-Requested-With': 'XMLHttpRequest'},
|
||||
);
|
||||
final responseData = json.decode(response.body);
|
||||
// if (responseData['status']=='Faild') {
|
||||
// return 'NotUser';
|
||||
// } else {
|
||||
// return 'Done';
|
||||
// }
|
||||
print(responseData);
|
||||
if (responseData['status'] == 'Faild') {
|
||||
return 'wrong';
|
||||
} else {
|
||||
return responseData['access_token'];
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> sendRePass(String password, String token) async {
|
||||
print(password);
|
||||
print(token);
|
||||
|
||||
final response = await http.post(
|
||||
Uri.parse(Url.sendResetPass),
|
||||
body: {
|
||||
'password': password,
|
||||
'password_confirmation': password,
|
||||
},
|
||||
headers: {'Authorization': token, 'X-Requested-With': 'XMLHttpRequest'},
|
||||
// headers: {'X-Requested-With': 'XMLHttpRequest'},
|
||||
);
|
||||
final responseData = json.decode(response.body);
|
||||
// if (responseData['status']=='Faild') {
|
||||
// return 'NotUser';
|
||||
// } else {
|
||||
// return 'Done';
|
||||
// }
|
||||
print(responseData);
|
||||
if (responseData['status'] == 'Success') {
|
||||
return 'done';
|
||||
} else {
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> login(String cellNumber, String password) async {
|
||||
final response = await http.post(
|
||||
Uri.parse(Url.login),
|
||||
body: {
|
||||
'cell_number': cellNumber,
|
||||
'password': password,
|
||||
},
|
||||
// headers: {'X-Requested-With': 'XMLHttpRequest'},
|
||||
);
|
||||
final responseData = json.decode(response.body);
|
||||
print(responseData);
|
||||
if (response.statusCode == 401) {
|
||||
if (responseData['message'] == 'The user credentials were incorrect.') {
|
||||
return 'badinput'; // incorrect user or pass
|
||||
} else {
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
_token = responseData['access_token'];
|
||||
_expiaryDate = DateTime.now().add(
|
||||
Duration(
|
||||
seconds: responseData['expires_in'],
|
||||
),
|
||||
);
|
||||
User.token = _token!;
|
||||
// print(isAuth);
|
||||
|
||||
// print(User.token);
|
||||
var localUser = await getUserData();
|
||||
// print("------ in auth ===========");
|
||||
print(localUser);
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final userData = json.encode({
|
||||
'token': _token,
|
||||
'expiaryDate': _expiaryDate!.toIso8601String(),
|
||||
'user': user
|
||||
});
|
||||
prefs.setString('userData', userData);
|
||||
notifyListeners();
|
||||
return 'true';
|
||||
}
|
||||
|
||||
Future<bool> tryAutoLogin() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
if (!prefs.containsKey('userData')) {
|
||||
return false;
|
||||
}
|
||||
final userData =
|
||||
json.decode(prefs.getString('userData')!) as Map<String, dynamic>;
|
||||
final expiaryDate = DateTime.parse(userData['expiaryDate']);
|
||||
if (expiaryDate.isBefore(DateTime.now())) {
|
||||
return false;
|
||||
}
|
||||
_token = userData['token'];
|
||||
_expiaryDate = expiaryDate;
|
||||
_user = userData['user'];
|
||||
User.token = _token!;
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> getUserData() async {
|
||||
final response = await http.get(
|
||||
Uri.parse(Url.getUserInfo),
|
||||
headers: {'Authorization': 'Bearer ' + token!},
|
||||
);
|
||||
// print(response.body);
|
||||
if (response.statusCode == 200) {
|
||||
var data = json.decode(response.body)['data'];
|
||||
_user.addAll(
|
||||
{
|
||||
'id': data['user']['id'],
|
||||
'name': data['user']['name'],
|
||||
'email': data['user']['email'],
|
||||
'cell_number': data['user']['cell_number'],
|
||||
'address': data['user']['address'],
|
||||
'gender': data['user']['gender'],
|
||||
'medical_center': data['user']['medical_center']['name'],
|
||||
'roles': data['roles'],
|
||||
},
|
||||
);
|
||||
// print(_user);
|
||||
notifyListeners();
|
||||
return _user;
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setUserData(id, name, email) async {
|
||||
var response = await http.post(
|
||||
Uri.parse('https://cssd-doc.com/api/UpdateInfo'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + token!,
|
||||
HttpHeaders.contentTypeHeader: 'application/json',
|
||||
},
|
||||
body: json.encode(
|
||||
{
|
||||
"id": id,
|
||||
"name": name,
|
||||
"email": email,
|
||||
// "country_code": "0098",
|
||||
// "cell_number": "09338736511",
|
||||
// "gender": "Male",
|
||||
// "verified": "Verified",
|
||||
// "status": "Active",
|
||||
// "medical_center_id": 1,
|
||||
// "address": null,
|
||||
// "created_at": null,
|
||||
// "updated_at": "2020-04-23 21:54:52",
|
||||
// "medical_center": {
|
||||
// "id": 1,
|
||||
// "name": "B",
|
||||
// "address": "1",
|
||||
// "phone_number": "12345678951",
|
||||
// "city_id": 1,
|
||||
// "created_at": null,
|
||||
// "updated_at": "2020-01-12 05:38:35"
|
||||
// },
|
||||
// "roles": [
|
||||
// {
|
||||
// "id": 1,
|
||||
// "name": "Admin",
|
||||
// "guard_name": "web",
|
||||
// "created_at": null,
|
||||
// "updated_at": null,
|
||||
// "pivot": {
|
||||
// "model_id": 1,
|
||||
// "role_id": 1,
|
||||
// "model_type": "App\\User",
|
||||
// },
|
||||
// },
|
||||
// ],
|
||||
},
|
||||
),
|
||||
);
|
||||
print(response.body);
|
||||
}
|
||||
|
||||
void logout() async {
|
||||
_token = null;
|
||||
_expiaryDate = null;
|
||||
_user = {};
|
||||
User.token = '';
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
prefs.remove('userData');
|
||||
var response = await http.post(
|
||||
Uri.parse(
|
||||
Url.logOut,
|
||||
),
|
||||
);
|
||||
print(response.statusCode);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user