step 3 : copy login screen app dokter

This commit is contained in:
sindhu
2024-01-12 14:22:49 +07:00
parent b4500de798
commit 3ae6622951
16 changed files with 2143 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
import '../app/constant.dart';
import '../model/auth_model.dart';
import 'base_repository.dart';
class AuthRepository extends BaseRepository {
AuthRepository({required super.dio});
Future<AuthModel> login({
required String username,
required String dokterId,
required String password,
}) async {
final param = {
"username": username,
"doctor_id": dokterId,
"password": password
// "username": "alhadad1",
// "doctor_id": "2891",
// "password": "3"
};
// final service = "${Constant.baseUrl}xauth/login";
final service = "${Constant.baseUrl}auth/login";
final resp = await post(param: param, service: service);
final result = AuthModel(
token: resp["data"]["token"],
model: AuthDoctorModel.fromJson(resp["data"]["user"]),
);
return result;
}
Future<String> logout({
required String M_UserID,
required String M_UserUsername,
}) async {
final param = {
"M_UserID": M_UserID,
"M_UserUsername": M_UserUsername
// "username": "alhadad",
// "doctor_id": "3101210841",
// "password": "riau123"
};
final service = "${Constant.baseUrl}auth/logout";
final resp = await post(param: param, service: service);
if (resp["status"] == "OK") {
return resp['status'];
} else {
return resp['message'];
}
}
Future<String> change_password({
required String token,
required String M_UserID,
required String username,
required String doctor_id,
required String new_password,
required String confirm_password,
}) async {
final param = {
"token": token,
"M_UserID": M_UserID,
"username": username,
"doctor_id": doctor_id,
"new_password": new_password,
"confirm_password": confirm_password
// "username": "alhadad",
// "doctor_id": "3101210841",
// "password": "riau123"
};
final service = "${Constant.baseUrl}auth/change_password";
final resp = await post(param: param, service: service);
// final result = AuthModel(
// token: resp["data"]["token"],
// model: AuthDoctorModel.fromJson(resp["data"]["user"]),
// );
// return result;
if (resp["status"] == "OK") {
return resp['message'];
} else {
return resp['message'];
}
}
}

View File

@@ -0,0 +1,104 @@
import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';
abstract class BaseRepository {
final Dio dio;
BaseRepository({required this.dio});
Future<Map<String, dynamic>> post({
required Map<String, dynamic> param,
required String service,
String? token,
}) async {
try {
final response = await dio.post(
// Constant.baseUrl + service,
service,
data: jsonEncode(param),
options: Options(
headers: token != null
? {
HttpHeaders.contentTypeHeader: "application/json",
HttpHeaders.authorizationHeader: "Bearer $token",
}
: {
HttpHeaders.contentTypeHeader: "application/json",
},
contentType: "application/json",
),
);
if (response.statusCode != 200) {
throw BaseRepositoryException(
message: "Invalid Http Response ${response.statusCode}",
);
}
Map<String, dynamic> jsonData = jsonDecode(response.data);
if (jsonData["status"] != "OK") {
throw BaseRepositoryException(
message: jsonData["message"],
);
} else {
return jsonData;
}
} on DioError catch (e) {
throw BaseRepositoryException(message: e.message);
} on SocketException catch (e) {
throw BaseRepositoryException(message: e.message);
} on BaseRepositoryException catch (e) {
throw BaseRepositoryException(message: e.message);
}
}
Future<Map<String, dynamic>> get({
// required Map<String, dynamic> param,
required String service,
String? token,
}) async {
try {
final response = await dio.post(
// Constant.baseUrl + service,
service,
// data: jsonEncode(param),
options: Options(
headers: token != null
? {
HttpHeaders.contentTypeHeader: "application/json",
HttpHeaders.authorizationHeader: "Bearer $token",
}
: {
HttpHeaders.contentTypeHeader: "application/json",
},
contentType: "application/json",
),
);
if (response.statusCode != 200) {
throw BaseRepositoryException(
message: "Invalid Http Response ${response.statusCode}",
);
}
Map<String, dynamic> jsonData = jsonDecode(response.data);
if (jsonData["status"] != "OK") {
throw BaseRepositoryException(
message: jsonData["message"],
);
} else {
return jsonData;
}
} on DioError catch (e) {
throw BaseRepositoryException(message: e.message);
} on SocketException catch (e) {
throw BaseRepositoryException(message: e.message);
} on BaseRepositoryException catch (e) {
throw BaseRepositoryException(message: e.message);
}
}
}
class BaseRepositoryException implements Exception {
final String message;
BaseRepositoryException({
required this.message,
});
}