1+ import 'package:cloud_functions/cloud_functions.dart' ;
12import 'package:firebase_auth/firebase_auth.dart' ;
23import 'package:flutter/foundation.dart' ;
34import 'package:google_sign_in/google_sign_in.dart' ;
@@ -9,10 +10,12 @@ import '../services/base/failure.dart';
910class AuthenticationRepository {
1011 AuthenticationRepository ({
1112 required this .firebaseAuth,
13+ required this .firebaseFunctions,
1214 required this .googleSignIn,
1315 });
1416
1517 final FirebaseAuth firebaseAuth;
18+ final FirebaseFunctions firebaseFunctions;
1619 final GoogleSignIn googleSignIn;
1720
1821 User ? get currentUser => firebaseAuth.currentUser;
@@ -25,6 +28,9 @@ class AuthenticationRepository {
2528 password: params.password,
2629 );
2730
31+ // I'm not creating a new user on firestore and using this instead
32+ // because all I have to store is full name and the firebase auth user
33+ // object is faster to access
2834 _userCredential.user! .updateDisplayName (params.fullName);
2935
3036 await _userCredential.user! .sendEmailVerification ();
@@ -129,6 +135,23 @@ class AuthenticationRepository {
129135 }
130136 }
131137
138+ Future <String > deleteUser () async {
139+ try {
140+ final HttpsCallable callable = firebaseFunctions.httpsCallable (
141+ 'deleteUserAccount' ,
142+ options: HttpsCallableOptions (timeout: const Duration (seconds: 10 )),
143+ );
144+
145+ final result = await callable ();
146+
147+ logout ();
148+
149+ return result.data;
150+ } on FirebaseFunctionsException catch (ex) {
151+ throw Failure (ex.message ?? 'Something went wrong!' );
152+ }
153+ }
154+
132155 Future <void > logout () async {
133156 if (! kIsWeb) {
134157 await googleSignIn.signOut ();
@@ -140,6 +163,7 @@ class AuthenticationRepository {
140163final authenticationRepository = Provider (
141164 (ref) => AuthenticationRepository (
142165 firebaseAuth: FirebaseAuth .instance,
166+ firebaseFunctions: FirebaseFunctions .instance,
143167 googleSignIn: GoogleSignIn (),
144168 ),
145169);
0 commit comments