62 lines
1.7 KiB
Dart
62 lines
1.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'package:pshared/api/responses/error/server.dart';
|
|
import 'package:pshared/provider/resource.dart';
|
|
import 'package:pshared/service/account.dart';
|
|
import 'package:pshared/utils/exception.dart';
|
|
|
|
class EmailVerificationProvider extends ChangeNotifier {
|
|
Resource<bool> _resource = Resource(data: null, isLoading: false);
|
|
String? _token;
|
|
|
|
Resource<bool> get resource => _resource;
|
|
bool get isLoading => _resource.isLoading;
|
|
bool get isSuccess => _resource.data == true;
|
|
Exception? get error => _resource.error;
|
|
ErrorResponse? get errorResponse =>
|
|
_resource.error is ErrorResponse ? _resource.error as ErrorResponse : null;
|
|
bool get canResendVerification {
|
|
final err = errorResponse;
|
|
if (err == null) return false;
|
|
switch (err.error) {
|
|
case 'not_found':
|
|
case 'token_expired':
|
|
case 'data_conflict':
|
|
case 'internal_error':
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<void> verify(String token) async {
|
|
final trimmed = token.trim();
|
|
if (trimmed.isEmpty) {
|
|
_setResource(
|
|
Resource(
|
|
data: null,
|
|
isLoading: false,
|
|
error: Exception('Email verification token is empty.'),
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
if (_token == trimmed && _resource.isLoading) return;
|
|
_token = trimmed;
|
|
_setResource(Resource(data: null, isLoading: true));
|
|
try {
|
|
await AccountService.verifyEmail(trimmed);
|
|
_setResource(Resource(data: true, isLoading: false));
|
|
} catch (e) {
|
|
_setResource(
|
|
Resource(data: null, isLoading: false, error: toException(e)),
|
|
);
|
|
}
|
|
}
|
|
|
|
void _setResource(Resource<bool> resource) {
|
|
_resource = resource;
|
|
notifyListeners();
|
|
}
|
|
}
|