mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-27 11:16:45 +00:00
Add recovery_key_cubit.dart
This commit is contained in:
parent
804e2750da
commit
a096e7e732
|
@ -628,7 +628,7 @@ class ServerApi extends ApiMap {
|
||||||
.replaceAll('"', '');
|
.replaceAll('"', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<ApiResponse<RecoveryTokenStatus>> getRecoveryTokenStatus() async {
|
Future<ApiResponse<RecoveryKeyStatus>> getRecoveryTokenStatus() async {
|
||||||
Response response;
|
Response response;
|
||||||
|
|
||||||
var client = await getClient();
|
var client = await getClient();
|
||||||
|
@ -639,7 +639,7 @@ class ServerApi extends ApiMap {
|
||||||
return ApiResponse(
|
return ApiResponse(
|
||||||
errorMessage: e.message,
|
errorMessage: e.message,
|
||||||
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
statusCode: e.response?.statusCode ?? HttpStatus.internalServerError,
|
||||||
data: RecoveryTokenStatus(exists: false, valid: false));
|
data: RecoveryKeyStatus(exists: false, valid: false));
|
||||||
} finally {
|
} finally {
|
||||||
close(client);
|
close(client);
|
||||||
}
|
}
|
||||||
|
@ -654,17 +654,23 @@ class ServerApi extends ApiMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<ApiResponse<String>> generateRecoveryToken(
|
Future<ApiResponse<String>> generateRecoveryToken(
|
||||||
DateTime expiration, int uses) async {
|
DateTime? expiration,
|
||||||
|
int? uses,
|
||||||
|
) async {
|
||||||
Response response;
|
Response response;
|
||||||
|
|
||||||
var client = await getClient();
|
var client = await getClient();
|
||||||
|
var data = {};
|
||||||
|
if (expiration != null) {
|
||||||
|
data['expiration'] = expiration.toIso8601String();
|
||||||
|
}
|
||||||
|
if (uses != null) {
|
||||||
|
data['uses'] = uses;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
response = await client.post(
|
response = await client.post(
|
||||||
'/auth/recovery_token',
|
'/auth/recovery_token',
|
||||||
data: {
|
data: data,
|
||||||
'expiration': expiration.toIso8601String(),
|
|
||||||
'uses': uses,
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
} on DioError catch (e) {
|
} on DioError catch (e) {
|
||||||
print(e.message);
|
print(e.message);
|
||||||
|
|
72
lib/logic/cubit/recovery_key/recovery_key_cubit.dart
Normal file
72
lib/logic/cubit/recovery_key/recovery_key_cubit.dart
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
import 'package:selfprivacy/logic/api_maps/server.dart';
|
||||||
|
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/json/recovery_token_status.dart';
|
||||||
|
|
||||||
|
part 'recovery_key_state.dart';
|
||||||
|
|
||||||
|
class RecoveryKeyCubit
|
||||||
|
extends ServerInstallationDependendCubit<RecoveryKeyState> {
|
||||||
|
RecoveryKeyCubit(ServerInstallationCubit serverInstallationCubit)
|
||||||
|
: super(serverInstallationCubit, RecoveryKeyState.initial());
|
||||||
|
|
||||||
|
final api = ServerApi();
|
||||||
|
|
||||||
|
@override
|
||||||
|
void load() async {
|
||||||
|
if (serverInstallationCubit.state is ServerInstallationFinished) {
|
||||||
|
final status = await _getRecoveryKeyStatus();
|
||||||
|
if (status == null) {
|
||||||
|
emit(state.copyWith(loadingStatus: LoadingStatus.error));
|
||||||
|
} else {
|
||||||
|
emit(state.copyWith(status: status, loadingStatus: LoadingStatus.good));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
emit(state.copyWith(loadingStatus: LoadingStatus.uninitialized));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<RecoveryKeyStatus?> _getRecoveryKeyStatus() async {
|
||||||
|
final ApiResponse<RecoveryKeyStatus> response =
|
||||||
|
await api.getRecoveryTokenStatus();
|
||||||
|
if (response.isSuccess) {
|
||||||
|
return response.data;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> refresh() async {
|
||||||
|
emit(state.copyWith(loadingStatus: LoadingStatus.refreshing));
|
||||||
|
final status = await _getRecoveryKeyStatus();
|
||||||
|
if (status == null) {
|
||||||
|
emit(state.copyWith(loadingStatus: LoadingStatus.error));
|
||||||
|
} else {
|
||||||
|
emit(state.copyWith(status: status, loadingStatus: LoadingStatus.good));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String> generateRecoveryKey({
|
||||||
|
DateTime? expirationDate,
|
||||||
|
int? numberOfUses,
|
||||||
|
}) async {
|
||||||
|
final ApiResponse<String> response =
|
||||||
|
await api.generateRecoveryToken(expirationDate, numberOfUses);
|
||||||
|
if (response.isSuccess) {
|
||||||
|
refresh();
|
||||||
|
return response.data;
|
||||||
|
} else {
|
||||||
|
throw GenerationError(response.errorMessage ?? 'Unknown error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void clear() {
|
||||||
|
emit(state.copyWith(loadingStatus: LoadingStatus.uninitialized));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class GenerationError extends Error {
|
||||||
|
final String message;
|
||||||
|
|
||||||
|
GenerationError(this.message);
|
||||||
|
}
|
37
lib/logic/cubit/recovery_key/recovery_key_state.dart
Normal file
37
lib/logic/cubit/recovery_key/recovery_key_state.dart
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
part of 'recovery_key_cubit.dart';
|
||||||
|
|
||||||
|
enum LoadingStatus {
|
||||||
|
uninitialized,
|
||||||
|
refreshing,
|
||||||
|
good,
|
||||||
|
error,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class RecoveryKeyState extends ServerInstallationDependendState {
|
||||||
|
const RecoveryKeyState(this._status, this.loadingStatus);
|
||||||
|
|
||||||
|
RecoveryKeyState.initial()
|
||||||
|
: this(RecoveryKeyStatus(exists: false, valid: false), LoadingStatus.refreshing);
|
||||||
|
|
||||||
|
final RecoveryKeyStatus _status;
|
||||||
|
final LoadingStatus loadingStatus;
|
||||||
|
|
||||||
|
bool get exists => _status.exists;
|
||||||
|
bool get isValid => _status.valid;
|
||||||
|
DateTime? get generatedAt => _status.date;
|
||||||
|
DateTime? get expiresAt => _status.date;
|
||||||
|
int? get usesLeft => _status.usesLeft;
|
||||||
|
@override
|
||||||
|
List<Object> get props => [_status, loadingStatus];
|
||||||
|
|
||||||
|
RecoveryKeyState copyWith({
|
||||||
|
RecoveryKeyStatus? status,
|
||||||
|
LoadingStatus? loadingStatus,
|
||||||
|
}) {
|
||||||
|
return RecoveryKeyState(
|
||||||
|
status ?? this._status,
|
||||||
|
loadingStatus ?? this.loadingStatus,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,23 +1,34 @@
|
||||||
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:json_annotation/json_annotation.dart';
|
import 'package:json_annotation/json_annotation.dart';
|
||||||
|
|
||||||
part 'recovery_token_status.g.dart';
|
part 'recovery_token_status.g.dart';
|
||||||
|
|
||||||
@JsonSerializable()
|
@JsonSerializable()
|
||||||
class RecoveryTokenStatus {
|
class RecoveryKeyStatus extends Equatable {
|
||||||
RecoveryTokenStatus({
|
RecoveryKeyStatus({
|
||||||
required this.exists,
|
required this.exists,
|
||||||
required this.valid,
|
required this.valid,
|
||||||
this.date,
|
this.date,
|
||||||
this.expiration,
|
this.expiration,
|
||||||
this.uses_left,
|
this.usesLeft,
|
||||||
});
|
});
|
||||||
|
|
||||||
final bool exists;
|
final bool exists;
|
||||||
final DateTime? date;
|
final DateTime? date;
|
||||||
final DateTime? expiration;
|
final DateTime? expiration;
|
||||||
final int? uses_left;
|
@JsonKey(name: 'uses_left')
|
||||||
|
final int? usesLeft;
|
||||||
final bool valid;
|
final bool valid;
|
||||||
|
|
||||||
factory RecoveryTokenStatus.fromJson(Map<String, dynamic> json) =>
|
factory RecoveryKeyStatus.fromJson(Map<String, dynamic> json) =>
|
||||||
_$RecoveryTokenStatusFromJson(json);
|
_$RecoveryKeyStatusFromJson(json);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [
|
||||||
|
exists,
|
||||||
|
date,
|
||||||
|
expiration,
|
||||||
|
usesLeft,
|
||||||
|
valid,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,8 @@ part of 'recovery_token_status.dart';
|
||||||
// JsonSerializableGenerator
|
// JsonSerializableGenerator
|
||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
RecoveryTokenStatus _$RecoveryTokenStatusFromJson(Map<String, dynamic> json) =>
|
RecoveryKeyStatus _$RecoveryKeyStatusFromJson(Map<String, dynamic> json) =>
|
||||||
RecoveryTokenStatus(
|
RecoveryKeyStatus(
|
||||||
exists: json['exists'] as bool,
|
exists: json['exists'] as bool,
|
||||||
valid: json['valid'] as bool,
|
valid: json['valid'] as bool,
|
||||||
date:
|
date:
|
||||||
|
@ -15,5 +15,5 @@ RecoveryTokenStatus _$RecoveryTokenStatusFromJson(Map<String, dynamic> json) =>
|
||||||
expiration: json['expiration'] == null
|
expiration: json['expiration'] == null
|
||||||
? null
|
? null
|
||||||
: DateTime.parse(json['expiration'] as String),
|
: DateTime.parse(json['expiration'] as String),
|
||||||
uses_left: json['uses_left'] as int?,
|
usesLeft: json['uses_left'] as int?,
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue