feat: sync auth to new api
This commit is contained in:
parent
bd7e925306
commit
576f687d21
@ -2,5 +2,5 @@ class Variables {
|
|||||||
static const String appName = 'POS Kasir Resto App';
|
static const String appName = 'POS Kasir Resto App';
|
||||||
static const String apiVersion = 'v1';
|
static const String apiVersion = 'v1';
|
||||||
// static const String baseUrl = 'http://192.168.1.202:8000';
|
// static const String baseUrl = 'http://192.168.1.202:8000';
|
||||||
static const String baseUrl = 'https://pos-app-tablet.enaklo.co.id';
|
static const String baseUrl = 'https://enaklo-pos-be.altru.id';
|
||||||
}
|
}
|
||||||
|
|||||||
22
lib/core/network/dio_client.dart
Normal file
22
lib/core/network/dio_client.dart
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// lib/core/network/dio_client.dart
|
||||||
|
import 'package:awesome_dio_interceptor/awesome_dio_interceptor.dart';
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
|
||||||
|
class DioClient {
|
||||||
|
static final Dio _dio = Dio(BaseOptions(
|
||||||
|
connectTimeout: const Duration(seconds: 10),
|
||||||
|
receiveTimeout: const Duration(seconds: 10),
|
||||||
|
headers: {
|
||||||
|
'Accept': 'application/json',
|
||||||
|
},
|
||||||
|
))
|
||||||
|
..interceptors.add(
|
||||||
|
AwesomeDioInterceptor(
|
||||||
|
logRequestTimeout: true,
|
||||||
|
logRequestHeaders: true,
|
||||||
|
logResponseHeaders: true,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
static Dio get instance => _dio;
|
||||||
|
}
|
||||||
@ -1,44 +1,67 @@
|
|||||||
|
import 'dart:developer';
|
||||||
|
|
||||||
import 'package:dartz/dartz.dart';
|
import 'package:dartz/dartz.dart';
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
import 'package:enaklo_pos/core/constants/variables.dart';
|
import 'package:enaklo_pos/core/constants/variables.dart';
|
||||||
|
import 'package:enaklo_pos/core/network/dio_client.dart';
|
||||||
import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart';
|
import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart';
|
||||||
import 'package:enaklo_pos/data/models/response/auth_response_model.dart';
|
import 'package:enaklo_pos/data/models/response/auth_response_model.dart';
|
||||||
import 'package:http/http.dart' as http;
|
|
||||||
|
|
||||||
class AuthRemoteDatasource {
|
class AuthRemoteDatasource {
|
||||||
|
final Dio dio = DioClient.instance;
|
||||||
Future<Either<String, AuthResponseModel>> login(
|
Future<Either<String, AuthResponseModel>> login(
|
||||||
String email, String password) async {
|
String email, String password) async {
|
||||||
final url = Uri.parse('${Variables.baseUrl}/api/login');
|
final url = '${Variables.baseUrl}/api/v1/auth/login';
|
||||||
final response = await http.post(
|
log(url);
|
||||||
url,
|
|
||||||
body: {
|
|
||||||
'email': email,
|
|
||||||
'password': password,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
try {
|
||||||
return Right(AuthResponseModel.fromJson(response.body));
|
final response = await dio.post(
|
||||||
} else {
|
url,
|
||||||
return const Left('Failed to login');
|
data: {
|
||||||
|
'email': email,
|
||||||
|
'password': password,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
return Right(AuthResponseModel.fromMap(response.data));
|
||||||
|
} else {
|
||||||
|
return const Left('Failed to login');
|
||||||
|
}
|
||||||
|
} on DioException catch (e) {
|
||||||
|
log("Dio error: ${e.message}");
|
||||||
|
return Left(e.response?.data['message'] ?? 'Login gagal');
|
||||||
|
} catch (e) {
|
||||||
|
log("Unexpected error: $e");
|
||||||
|
return const Left('Unexpected error occurred');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//logout
|
//logout
|
||||||
Future<Either<String, bool>> logout() async {
|
Future<Either<String, bool>> logout() async {
|
||||||
final authData = await AuthLocalDataSource().getAuthData();
|
try {
|
||||||
final url = Uri.parse('${Variables.baseUrl}/api/logout');
|
final authData = await AuthLocalDataSource().getAuthData();
|
||||||
final response = await http.post(
|
final url = '${Variables.baseUrl}/api/v1/auth/logout';
|
||||||
url,
|
|
||||||
headers: {
|
|
||||||
'Authorization': 'Bearer ${authData.token}',
|
|
||||||
'Accept': 'application/json',
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
final response = await dio.post(
|
||||||
return const Right(true);
|
url,
|
||||||
} else {
|
options: Options(
|
||||||
return const Left('Failed to logout');
|
headers: {
|
||||||
|
'Authorization': 'Bearer ${authData.token}',
|
||||||
|
'Accept': 'application/json',
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
return const Right(true);
|
||||||
|
} else {
|
||||||
|
return const Left('Failed to logout');
|
||||||
|
}
|
||||||
|
} on DioException catch (e) {
|
||||||
|
return Left(e.response?.data['message'] ?? 'Logout gagal');
|
||||||
|
} catch (e) {
|
||||||
|
return const Left('Unexpected error occurred');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,85 +1,83 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
class AuthResponseModel {
|
class AuthResponseModel {
|
||||||
final String? status;
|
final String? token;
|
||||||
final String? token;
|
final User? user;
|
||||||
final User? user;
|
|
||||||
|
|
||||||
AuthResponseModel({
|
AuthResponseModel({
|
||||||
this.status,
|
this.token,
|
||||||
this.token,
|
this.user,
|
||||||
this.user,
|
});
|
||||||
});
|
|
||||||
|
|
||||||
factory AuthResponseModel.fromJson(String str) => AuthResponseModel.fromMap(json.decode(str));
|
factory AuthResponseModel.fromJson(String str) =>
|
||||||
|
AuthResponseModel.fromMap(json.decode(str));
|
||||||
|
|
||||||
String toJson() => json.encode(toMap());
|
String toJson() => json.encode(toMap());
|
||||||
|
|
||||||
factory AuthResponseModel.fromMap(Map<String, dynamic> json) => AuthResponseModel(
|
factory AuthResponseModel.fromMap(Map<String, dynamic> json) =>
|
||||||
status: json["status"],
|
AuthResponseModel(
|
||||||
token: json["token"],
|
token: json["token"],
|
||||||
user: json["user"] == null ? null : User.fromMap(json["user"]),
|
user: json["user"] == null ? null : User.fromMap(json["user"]),
|
||||||
);
|
);
|
||||||
|
|
||||||
Map<String, dynamic> toMap() => {
|
Map<String, dynamic> toMap() => {
|
||||||
"status": status,
|
|
||||||
"token": token,
|
"token": token,
|
||||||
"user": user?.toMap(),
|
"user": user?.toMap(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
class User {
|
class User {
|
||||||
final int? id;
|
final String? id;
|
||||||
final String? name;
|
final String? organizationId;
|
||||||
final String? email;
|
final String? outletId;
|
||||||
final DateTime? emailVerifiedAt;
|
final String? name;
|
||||||
final dynamic twoFactorSecret;
|
final String? email;
|
||||||
final dynamic twoFactorRecoveryCodes;
|
final String? role;
|
||||||
final dynamic twoFactorConfirmedAt;
|
final bool? isActive;
|
||||||
final DateTime? createdAt;
|
final DateTime? createdAt;
|
||||||
final DateTime? updatedAt;
|
final DateTime? updatedAt;
|
||||||
final String? role;
|
|
||||||
|
|
||||||
User({
|
User({
|
||||||
this.id,
|
this.id,
|
||||||
this.name,
|
this.organizationId,
|
||||||
this.email,
|
this.outletId,
|
||||||
this.emailVerifiedAt,
|
this.name,
|
||||||
this.twoFactorSecret,
|
this.role,
|
||||||
this.twoFactorRecoveryCodes,
|
this.isActive,
|
||||||
this.twoFactorConfirmedAt,
|
this.email,
|
||||||
this.createdAt,
|
this.createdAt,
|
||||||
this.updatedAt,
|
this.updatedAt,
|
||||||
this.role,
|
});
|
||||||
});
|
|
||||||
|
|
||||||
factory User.fromJson(String str) => User.fromMap(json.decode(str));
|
factory User.fromJson(String str) => User.fromMap(json.decode(str));
|
||||||
|
|
||||||
String toJson() => json.encode(toMap());
|
String toJson() => json.encode(toMap());
|
||||||
|
|
||||||
factory User.fromMap(Map<String, dynamic> json) => User(
|
factory User.fromMap(Map<String, dynamic> json) => User(
|
||||||
id: json["id"],
|
id: json["id"],
|
||||||
|
organizationId: json["organization_id"],
|
||||||
|
outletId: json["outlet_id"],
|
||||||
name: json["name"],
|
name: json["name"],
|
||||||
email: json["email"],
|
email: json["email"],
|
||||||
emailVerifiedAt: json["email_verified_at"] == null ? null : DateTime.parse(json["email_verified_at"]),
|
|
||||||
twoFactorSecret: json["two_factor_secret"],
|
|
||||||
twoFactorRecoveryCodes: json["two_factor_recovery_codes"],
|
|
||||||
twoFactorConfirmedAt: json["two_factor_confirmed_at"],
|
|
||||||
createdAt: json["created_at"] == null ? null : DateTime.parse(json["created_at"]),
|
|
||||||
updatedAt: json["updated_at"] == null ? null : DateTime.parse(json["updated_at"]),
|
|
||||||
role: json["role"],
|
role: json["role"],
|
||||||
);
|
isActive: json["is_active"],
|
||||||
|
createdAt: json["created_at"] == null
|
||||||
|
? null
|
||||||
|
: DateTime.parse(json["created_at"]),
|
||||||
|
updatedAt: json["updated_at"] == null
|
||||||
|
? null
|
||||||
|
: DateTime.parse(json["updated_at"]),
|
||||||
|
);
|
||||||
|
|
||||||
Map<String, dynamic> toMap() => {
|
Map<String, dynamic> toMap() => {
|
||||||
"id": id,
|
"id": id,
|
||||||
|
"organization_id": organizationId,
|
||||||
|
"outlet_id": outletId,
|
||||||
"name": name,
|
"name": name,
|
||||||
"email": email,
|
"email": email,
|
||||||
"email_verified_at": emailVerifiedAt?.toIso8601String(),
|
"role": role,
|
||||||
"two_factor_secret": twoFactorSecret,
|
"is_active": isActive,
|
||||||
"two_factor_recovery_codes": twoFactorRecoveryCodes,
|
|
||||||
"two_factor_confirmed_at": twoFactorConfirmedAt,
|
|
||||||
"created_at": createdAt?.toIso8601String(),
|
"created_at": createdAt?.toIso8601String(),
|
||||||
"updated_at": updatedAt?.toIso8601String(),
|
"updated_at": updatedAt?.toIso8601String(),
|
||||||
"role": role,
|
};
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -52,7 +52,7 @@ class OrderBloc extends Bloc<OrderEvent, OrderState> {
|
|||||||
total: event.totalPriceFinal,
|
total: event.totalPriceFinal,
|
||||||
paymentMethod: event.paymentMethod,
|
paymentMethod: event.paymentMethod,
|
||||||
totalItem: totalItem,
|
totalItem: totalItem,
|
||||||
idKasir: userData.user?.id ?? 1,
|
idKasir: int.parse(userData.user!.id ?? "0"),
|
||||||
namaKasir: userData.user?.name ?? 'Kasir A',
|
namaKasir: userData.user?.name ?? 'Kasir A',
|
||||||
transactionTime: DateTime.now().toIso8601String(),
|
transactionTime: DateTime.now().toIso8601String(),
|
||||||
customerName: event.customerName,
|
customerName: event.customerName,
|
||||||
|
|||||||
32
pubspec.lock
32
pubspec.lock
@ -46,6 +46,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.13.0"
|
version: "2.13.0"
|
||||||
|
awesome_dio_interceptor:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: awesome_dio_interceptor
|
||||||
|
sha256: "4aef4adfdd9d8fda159870277b898a97986c6624baaf42f8a986d3130860d007"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.3.0"
|
||||||
barcode:
|
barcode:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -222,6 +230,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.0.0"
|
version: "3.0.0"
|
||||||
|
colorize:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: colorize
|
||||||
|
sha256: "584746cd6ba1cba0633b6720f494fe6f9601c4170f0666c1579d2aa2a61071ba"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "3.0.0"
|
||||||
connectivity_plus:
|
connectivity_plus:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@ -326,6 +342,22 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "7.0.2"
|
version: "7.0.2"
|
||||||
|
dio:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: dio
|
||||||
|
sha256: "253a18bbd4851fecba42f7343a1df3a9a4c1d31a2c1b37e221086b4fa8c8dbc9"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "5.8.0+1"
|
||||||
|
dio_web_adapter:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: dio_web_adapter
|
||||||
|
sha256: "7586e476d70caecaf1686d21eee7247ea43ef5c345eab9e0cc3583ff13378d78"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.1"
|
||||||
esc_pos_utils_plus:
|
esc_pos_utils_plus:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
|||||||
@ -59,6 +59,8 @@ dependencies:
|
|||||||
widgets_to_image: ^1.0.0
|
widgets_to_image: ^1.0.0
|
||||||
flutter_esc_pos_network: ^1.0.3
|
flutter_esc_pos_network: ^1.0.3
|
||||||
flutter_screenutil: ^5.9.3
|
flutter_screenutil: ^5.9.3
|
||||||
|
dio: ^5.8.0+1
|
||||||
|
awesome_dio_interceptor: ^1.3.0
|
||||||
# imin_printer: ^0.6.10
|
# imin_printer: ^0.6.10
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user