Email Confirmation and refactor for snackbar
This commit is contained in:
50
frontend/pshared/lib/provider/email_verification.dart
Normal file
50
frontend/pshared/lib/provider/email_verification.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:flutter/material.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;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user