mirror of
https://git.meshkee.com/applications/cssd.git
synced 2026-10-10 19:19:17 +03:30
296 lines
7.9 KiB
Dart
Executable File
296 lines
7.9 KiB
Dart
Executable File
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;
|
|
}
|
|
|
|
bool isloading = false;
|
|
|
|
void resetLoading() {
|
|
isloading = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
void setLoading() {
|
|
isloading = true;
|
|
notifyListeners();
|
|
}
|
|
|
|
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 passwordConfirmation, String token) async {
|
|
print(password);
|
|
print(token);
|
|
|
|
final response = await http.post(
|
|
Uri.parse(Url.sendResetPass),
|
|
body: {
|
|
'password': password,
|
|
'password_confirmation': passwordConfirmation,
|
|
},
|
|
headers: {
|
|
'Authorization': token != 'Bearer null' ? token : User.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' &&
|
|
responseData['message'] == 'Success Change Password') {
|
|
return 'done';
|
|
} else {
|
|
_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 '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 == 400) {
|
|
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://www.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();
|
|
}
|
|
}
|