Email Confirmation and refactor for snackbar
This commit is contained in:
@@ -191,6 +191,15 @@ class AccountProvider extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> resendVerificationEmail(String email) async {
|
||||
try {
|
||||
await AccountService.resendVerificationEmail(email);
|
||||
} catch (e) {
|
||||
_setResource(_resource.copyWith(error: toException(e)));
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
_authState = AuthState.empty;
|
||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,16 @@ class AccountService {
|
||||
await getPUTResponse(_objectType, 'password', ForgotPasswordRequest.build(login: email).toJson());
|
||||
}
|
||||
|
||||
static Future<void> resendVerificationEmail(String email) async {
|
||||
_logger.fine('Resending verification email');
|
||||
await getPUTResponse(_objectType, 'email', {'login': email});
|
||||
}
|
||||
|
||||
static Future<void> verifyEmail(String token) async {
|
||||
_logger.fine('Verifying email');
|
||||
await getGETResponse(_objectType, 'verify/$token');
|
||||
}
|
||||
|
||||
static Future<void> resetPassword(String accountRef, String token, String newPassword) async {
|
||||
_logger.fine('Resetting password for account: $accountRef');
|
||||
await getPOSTResponse(_objectType, 'password/reset/$accountRef/$token', ResetPasswordRequest.build(password: newPassword).toJson());
|
||||
|
||||
Reference in New Issue
Block a user