feat: sync auth to new api
This commit is contained in:
@@ -1,44 +1,67 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:dio/dio.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/models/response/auth_response_model.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class AuthRemoteDatasource {
|
||||
final Dio dio = DioClient.instance;
|
||||
Future<Either<String, AuthResponseModel>> login(
|
||||
String email, String password) async {
|
||||
final url = Uri.parse('${Variables.baseUrl}/api/login');
|
||||
final response = await http.post(
|
||||
url,
|
||||
body: {
|
||||
'email': email,
|
||||
'password': password,
|
||||
},
|
||||
);
|
||||
final url = '${Variables.baseUrl}/api/v1/auth/login';
|
||||
log(url);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return Right(AuthResponseModel.fromJson(response.body));
|
||||
} else {
|
||||
return const Left('Failed to login');
|
||||
try {
|
||||
final response = await dio.post(
|
||||
url,
|
||||
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
|
||||
Future<Either<String, bool>> logout() async {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final url = Uri.parse('${Variables.baseUrl}/api/logout');
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
);
|
||||
try {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final url = '${Variables.baseUrl}/api/v1/auth/logout';
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return const Right(true);
|
||||
} else {
|
||||
return const Left('Failed to logout');
|
||||
final response = await dio.post(
|
||||
url,
|
||||
options: Options(
|
||||
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';
|
||||
|
||||
class AuthResponseModel {
|
||||
final String? status;
|
||||
final String? token;
|
||||
final User? user;
|
||||
final String? token;
|
||||
final User? user;
|
||||
|
||||
AuthResponseModel({
|
||||
this.status,
|
||||
this.token,
|
||||
this.user,
|
||||
});
|
||||
AuthResponseModel({
|
||||
this.token,
|
||||
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(
|
||||
status: json["status"],
|
||||
factory AuthResponseModel.fromMap(Map<String, dynamic> json) =>
|
||||
AuthResponseModel(
|
||||
token: json["token"],
|
||||
user: json["user"] == null ? null : User.fromMap(json["user"]),
|
||||
);
|
||||
);
|
||||
|
||||
Map<String, dynamic> toMap() => {
|
||||
"status": status,
|
||||
Map<String, dynamic> toMap() => {
|
||||
"token": token,
|
||||
"user": user?.toMap(),
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
class User {
|
||||
final int? id;
|
||||
final String? name;
|
||||
final String? email;
|
||||
final DateTime? emailVerifiedAt;
|
||||
final dynamic twoFactorSecret;
|
||||
final dynamic twoFactorRecoveryCodes;
|
||||
final dynamic twoFactorConfirmedAt;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
final String? role;
|
||||
final String? id;
|
||||
final String? organizationId;
|
||||
final String? outletId;
|
||||
final String? name;
|
||||
final String? email;
|
||||
final String? role;
|
||||
final bool? isActive;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
|
||||
User({
|
||||
this.id,
|
||||
this.name,
|
||||
this.email,
|
||||
this.emailVerifiedAt,
|
||||
this.twoFactorSecret,
|
||||
this.twoFactorRecoveryCodes,
|
||||
this.twoFactorConfirmedAt,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
this.role,
|
||||
});
|
||||
User({
|
||||
this.id,
|
||||
this.organizationId,
|
||||
this.outletId,
|
||||
this.name,
|
||||
this.role,
|
||||
this.isActive,
|
||||
this.email,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
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"],
|
||||
organizationId: json["organization_id"],
|
||||
outletId: json["outlet_id"],
|
||||
name: json["name"],
|
||||
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"],
|
||||
);
|
||||
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,
|
||||
"organization_id": organizationId,
|
||||
"outlet_id": outletId,
|
||||
"name": name,
|
||||
"email": email,
|
||||
"email_verified_at": emailVerifiedAt?.toIso8601String(),
|
||||
"two_factor_secret": twoFactorSecret,
|
||||
"two_factor_recovery_codes": twoFactorRecoveryCodes,
|
||||
"two_factor_confirmed_at": twoFactorConfirmedAt,
|
||||
"role": role,
|
||||
"is_active": isActive,
|
||||
"created_at": createdAt?.toIso8601String(),
|
||||
"updated_at": updatedAt?.toIso8601String(),
|
||||
"role": role,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user