From dd66e7c7471df97c0176084db6c86a4ade7c01ca Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Wed, 16 Apr 2025 12:02:19 +0400 Subject: [PATCH] added client token login --- lib/features/auth/bloc/auth_cubit.dart | 14 ++++++++++++++ lib/services/api/api_links_endpoints.dart | 2 +- lib/services/api/authentication_api.dart | 22 +++++++++++++++++++++- lib/services/api/http_service.dart | 15 ++++++++++++++- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/features/auth/bloc/auth_cubit.dart b/lib/features/auth/bloc/auth_cubit.dart index 2d37c7d..f0025b2 100644 --- a/lib/features/auth/bloc/auth_cubit.dart +++ b/lib/features/auth/bloc/auth_cubit.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:syncrow_app/features/auth/model/login_with_email_model.dart'; import 'package:syncrow_app/features/auth/model/signup_model.dart'; @@ -217,9 +218,22 @@ class AuthCubit extends Cubit { signUp() async { emit(AuthLoginLoading()); final response; + + final clientId = dotenv.env['CLIENT_ID'] ?? ''; + final clientSecret = dotenv.env['CLIENT_SECRET'] ?? ''; + try { List userFullName = fullName.split(' '); + + final clientToken = await AuthenticationAPI.fetchClientToken( + clientId: clientId, + clientSecret: clientSecret, + ); + + final accessToken = clientToken['accessToken']; + response = await AuthenticationAPI.signUp( + accessToken: accessToken, model: SignUpModel( hasAcceptedAppAgreement: true, email: email.toLowerCase(), diff --git a/lib/services/api/api_links_endpoints.dart b/lib/services/api/api_links_endpoints.dart index ea4a606..9c76012 100644 --- a/lib/services/api/api_links_endpoints.dart +++ b/lib/services/api/api_links_endpoints.dart @@ -11,7 +11,7 @@ abstract class ApiEndpoints { static const String sendOtp = '/authentication/user/send-otp'; static const String verifyOtp = '/authentication/user/verify-otp'; static const String forgetPassword = '/authentication/user/forget-password'; - + static const String clientLogin = 'client/token'; ////////////////////////////////////// Spaces /////////////////////////////////////// ///Community Module diff --git a/lib/services/api/authentication_api.dart b/lib/services/api/authentication_api.dart index d061dae..6579e4d 100644 --- a/lib/services/api/authentication_api.dart +++ b/lib/services/api/authentication_api.dart @@ -25,11 +25,15 @@ class AuthenticationAPI { return response; } - static Future signUp({required SignUpModel model}) async { + static Future signUp({ + required SignUpModel model, + required String accessToken, + }) async { final response = await HTTPService().post( path: ApiEndpoints.signUp, body: model.toJson(), showServerMessage: false, + accessToken: accessToken, expectedResponseModel: (json) => json['statusCode'] == 201); return response; } @@ -63,4 +67,20 @@ class AuthenticationAPI { expectedResponseModel: (json) => json['data']); return response; } + + static Future> fetchClientToken({ + required String clientId, + required String clientSecret, + }) async { + final response = await HTTPService().post( + path: ApiEndpoints.clientLogin, + body: { + 'clientId': clientId, + 'clientSecret': clientSecret, + }, + showServerMessage: false, + expectedResponseModel: (json) => json['data'], + ); + return response; + } } diff --git a/lib/services/api/http_service.dart b/lib/services/api/http_service.dart index 36e8e71..cdc04d0 100644 --- a/lib/services/api/http_service.dart +++ b/lib/services/api/http_service.dart @@ -48,13 +48,26 @@ class HTTPService { Options? options, dynamic body, bool showServerMessage = true, + String? accessToken, required T Function(dynamic) expectedResponseModel}) async { try { + final authOptions = options ?? + Options( + headers: accessToken != null + ? { + 'Authorization': 'Bearer $accessToken', + 'Content-Type': 'application/json', + } + : { + 'Content-Type': 'application/json', + }, + ); + final response = await client.post( path, data: body, queryParameters: queryParameters, - options: options, + options: authOptions, ); return expectedResponseModel(response.data); } catch (error) {