Initialized Auth pages for future work

Implemented Login functionality
This commit is contained in:
Mohammad Salameh
2024-03-07 10:29:19 +03:00
parent 4087f9c71c
commit f734801e94
28 changed files with 728 additions and 110 deletions
+9 -1
View File
@@ -1,3 +1,11 @@
abstract class ApiEndpoints {
static const String apiKey = '';
static const String baseUrl = 'http://100.107.182.63:4001';
static const String signUp = '$baseUrl/authentication/user/signup';
static const String login = '$baseUrl/authentication/user/login';
static const String deleteUser = '$baseUrl/authentication/user/delete/{id}';
static const String sendOtp = '$baseUrl/authentication/user/send-otp';
static const String verifyOtp = '$baseUrl/authentication/user/verify-otp';
static const String forgetPassword =
'$baseUrl/authentication/user/forget-password';
}
+140
View File
@@ -0,0 +1,140 @@
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:syncrow_app/features/auth/model/token.dart';
import 'package:syncrow_app/features/auth/model/verify_code.dart';
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
import 'package:syncrow_app/services/api/http_service.dart';
class AuthenticationAPI {
static Future<Token> verifyPassCode(VerifyPassCode data) async {
final response = await HTTPService().post(
path: ApiEndpoints.verifyOtp,
body: data.toJson(),
showServerMessage: false,
expectedResponseModel: (json) {
Token token = Token.fromJson(json);
return token;
});
return response;
}
static Future<Token> loginWithEmail(
{required String email, required String password}) async {
final response = await HTTPService().post(
path: ApiEndpoints.login,
body: jsonEncode({
"email": email,
"password": password,
}),
showServerMessage: false,
expectedResponseModel: (json) {
Token token = Token.fromJson(json['data']);
return token;
});
debugPrint("response: $response");
return response;
}
// static Future<SuccessResponse> updateUserInfo(
// Map<String, dynamic> data) async {
// final response = await HTTPService().postRequest(
// path: APIConstants.updateUserInfo,
// body: data,
// expectedResponseModel: (json) {
// SuccessResponse token = SuccessResponse.fromJson(json);
// return token;
// });
// return response;
// }
//
// static Future<LoginWithPhoneResponse> loginWithPhone(
// {required LoginWithPhone data, String? recaptchaToken}) async {
// final response = await HTTPService().postRequest(
// path: APIConstants.loginWithChannel,
// body: data.toJson(),
// options: Options(headers: {
// 'captcha-token': recaptchaToken,
// 'captcha-site-key': Platform.isAndroid
// ? dotenv.env['RECAPTCH_ANDROID_SITE_KEY'] ?? ''
// : dotenv.env['RECAPTCH_IOS_SITE_KEY'] ?? ''
// }),
// expectedResponseModel: (json) {
// LoginWithPhoneResponse result = LoginWithPhoneResponse.fromJson(json);
// return result;
// });
// return response;
// }
//
// static Future<List<dynamic>> countryCodes() async {
// final response = await HTTPService().postRequest(
// path: APIConstants.getCountyCode,
// expectedResponseModel: (json) {
// List<dynamic> result = json.toList();
// return result;
// });
// return response;
// }
//
// static Future<bool> sendNotificationToken({
// required Map<String, String> data,
// }) async {
// final response = await HTTPService().postRequest(
// path: APIConstants.notificationToken,
// body: data,
// showServerMessage: false,
// expectedResponseModel: (json) {
// bool checked = false;
// if (json != null) {
// if (json['success']) {
// checked = true;
// }
// }
// return checked;
// });
// return response;
// }
//
// static Future<bool> logout() async {
// final response = await HTTPService().postRequest(
// path: APIConstants.logout,
// expectedResponseModel: (json) {
// bool checked = false;
// // print(json);
// if (json != null) {
// if (json['success']) {
// checked = true;
// }
// }
// return checked;
// });
// return response;
// }
//
// static Future<bool> deleteAccount() async {
// final response = await HTTPService().postRequest(
// path: APIConstants.deleteAccount,
// expectedResponseModel: (json) {
// bool checked = false;
// if (json != null) {
// if (json['success']) {
// checked = true;
// }
// }
// return checked;
// });
// return response;
// }
// static Future<Token> refreshToken(Map<String, dynamic> data) async {
// final response = await HTTPService().postRequest(
// path: APIConstants.refreshToken,
// showServerMessage: false,
// body: data,
// expectedResponseModel: (json) {
// Token token = Token.fromJson(json);
// return token;
// });
// return response;
// }
}
+38 -40
View File
@@ -1,44 +1,42 @@
import 'package:dio/dio.dart';
class HTTPInterceptor extends InterceptorsWrapper {
@override
void onResponse(Response response, ResponseInterceptorHandler handler) async {
// Pass the response to the next interceptor or response handler.
return handler.next(response);
}
@override
void onRequest(RequestOptions options,
RequestInterceptorHandler handler) async {
// TODO: Implement logic for adding headers to requests.
// This method is called before a request is sent.
super.onRequest(options, handler);
}
@override
void onError(DioException err, ErrorInterceptorHandler handler) async {
// TODO: Implement error handling logic.
// This method is called when an error occurs during a request.
super.onError(err, handler);
}
/// Validates the response and returns true if it is successful (status code 2xx).
Future<bool> validateResponse(Response response) async {
if (response.statusCode != null) {
if (response.statusCode! >= 200 && response.statusCode! < 300) {
// If the response status code is within the successful range (2xx),
// return true indicating a successful response.
return true;
} else {
// If the response status code is not within the successful range (2xx),
// return false indicating an unsuccessful response.
return false;
}
} else {
// If the response status code is null, return false indicating an unsuccessful response.
return false;
}
}
// @override
// void onResponse(Response response, ResponseInterceptorHandler handler) async {
// // Pass the response to the next interceptor or response handler.
// return handler.next(response);
// }
//
// @override
// void onRequest(RequestOptions options,
// RequestInterceptorHandler handler) async {
// // TODO: Implement logic for adding headers to requests.
// // This method is called before a request is sent.
// super.onRequest(options, handler);
// }
//
// @override
// void onError(DioException err, ErrorInterceptorHandler handler) async {
// // TODO: Implement error handling logic.
// // This method is called when an error occurs during a request.
// super.onError(err, handler);
// }
//
// /// Validates the response and returns true if it is successful (status code 2xx).
// Future<bool> validateResponse(Response response) async {
// if (response.statusCode != null) {
// if (response.statusCode! >= 200 && response.statusCode! < 300) {
// // If the response status code is within the successful range (2xx),
// // return true indicating a successful response.
// return true;
// } else {
// // If the response status code is not within the successful range (2xx),
// // return false indicating an unsuccessful response.
// return false;
// }
// } else {
// // If the response status code is null, return false indicating an unsuccessful response.
// return false;
// }
// }
}
+39 -27
View File
@@ -1,42 +1,32 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
import 'package:syncrow_app/services/api/http_interceptor.dart';
import 'package:syncrow_app/services/locator.dart';
import 'http_interceptor.dart';
class HTTPService {
final Dio client = locator<Dio>();
final navigatorKey = GlobalKey<NavigatorState>();
late Dio client = serviceLocator.get<Dio>();
// final navigatorKey = GlobalKey<NavigatorState>();
String certificateString = "";
static Dio setupDioClient() {
final client = Dio(
Dio setupDioClient() {
client = Dio(
BaseOptions(
// TODO add base url
// baseUrl: URLConstants.baseURL,
baseUrl: ApiEndpoints.baseUrl,
receiveDataWhenStatusError: true,
followRedirects: false,
connectTimeout: const Duration(milliseconds: 60000),
receiveTimeout: const Duration(milliseconds: 60000),
connectTimeout: const Duration(minutes: 1),
receiveTimeout: const Duration(minutes: 1),
),
);
// (client.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () {
// client. = (X509Certificate cert, String host, int port) {
// // TODO add SSL certificate
// // if(cert.pem == certificateString){ // Verify the certificate
// // return true;
// // }
// // return false;
// return true;
// };
// return client;
// };
client.interceptors.add(locator<HTTPInterceptor>());
client.interceptors.add(serviceLocator.get<HTTPInterceptor>());
return client;
}
Future<T> getRequest<T>({
Future<T> get<T>({
required String path,
Map<String, dynamic>? queryParameters,
required T Function(dynamic) expectedResponseModel,
@@ -55,7 +45,7 @@ class HTTPService {
}
}
Future<T> postRequest<T>(
Future<T> post<T>(
{required String path,
Map<String, dynamic>? queryParameters,
Options? options,
@@ -65,16 +55,18 @@ class HTTPService {
try {
final response = await client.post(path,
data: body, queryParameters: queryParameters, options: options);
print("post response is $response");
debugPrint("status code is ${response.statusCode}");
debugPrint("response data is ${response.data}");
return expectedResponseModel(response.data);
} catch (error) {
debugPrint("******* Error");
debugPrint("******* Error ********");
debugPrint(error.toString());
rethrow;
}
}
Future<T> patchRequest<T>(
Future<T> patch<T>(
{required String path,
Map<String, dynamic>? queryParameters,
dynamic body,
@@ -94,7 +86,7 @@ class HTTPService {
}
}
Future<T> downloadRequest<T>(
Future<T> download<T>(
{required String path,
required String savePath,
Map<String, dynamic>? queryParameters,
@@ -118,4 +110,24 @@ class HTTPService {
rethrow;
}
}
//delete
Future<T> delete<T>({
required String path,
Map<String, dynamic>? queryParameters,
required T Function(dynamic) expectedResponseModel,
bool showServerMessage = true,
}) async {
try {
final response = await client.delete(
path,
queryParameters: queryParameters,
);
return expectedResponseModel(response.data);
} catch (error) {
debugPrint("******* Error");
debugPrint(error.toString());
rethrow;
}
}
}