How can I link User ID from Firebase Auth to UserID from Firestore in Flutter Using FirebaseUI SignInScreen?

44 views Asked by At

I'm using FirebaseUI for flutter to provide a sign in screen with phone and google auth providers. When someone signs in using their phone number or google auth for the first time, a user is automatically created and added to Firebase Auth. I want to create a document with that UID as the UserID for Firestore. I know the FirebaseAuth.instance.createUserWithEmailAndPassword() function returns a callback of UserCredential but I can't find where this is set or how it's returned in the provided SignInScreen from firebaseUI Auth.

EDIT: I added a parameter to SignInScreen called actions which appears to contain what I need, but it turns out those events never actually fire.

Here is the code I'm using for the SignInScreen:

import 'package:firebase_auth/firebase_auth.dart'
    hide EmailAuthProvider, PhoneAuthProvider;
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
import 'package:firebase_ui_oauth_google/firebase_ui_oauth_google.dart';
import 'package:flutter/material.dart';

import 'home.dart';
class AuthGate extends StatelessWidget {
  const AuthGate({super.key});

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<User?>(
      stream: FirebaseAuth.instance.authStateChanges(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return SignInScreen(
            actions: [
              AuthStateChangeAction<UserCreated>((context, state) {
                print('AuthStateUserCreated');
              }),
              AuthStateChangeAction<SignedIn>((context, state) {
                print('AuthStateSignedIn');
              }),
            ],
            showAuthActionSwitch: false,
            providers: [
              PhoneAuthProvider(),
              GoogleProvider(
                  clientId:
                      "MY-CLIENT-ID"),
            ],
            headerBuilder: (context, constraints, shrinkOffset) {
              return Padding(
                padding: const EdgeInsets.all(20),
                child: AspectRatio(
                  aspectRatio: 1,
                  child: Image.asset('assets/flutterfire_300x.png'),
                ),
              );
            },
            subtitleBuilder: (context, action) {
              return Padding(
                padding: const EdgeInsets.symmetric(vertical: 8.0),
                child: action == AuthAction.signIn
                    ? const Text('Welcome to Dyad, please sign in!')
                    : const Text('Welcome to Flutterfire, please sign up!'),
              );
            },
            footerBuilder: (context, action) {
              return const Padding(
                padding: EdgeInsets.only(top: 16),
                child: Text(
                  'By signing in, you agree to our terms and conditions.',
                  style: TextStyle(color: Colors.grey),
                ),
              );
            },
            sideBuilder: (context, shrinkOffset) {
              return Padding(
                padding: const EdgeInsets.all(20),
                child: AspectRatio(
                  aspectRatio: 1,
                  child: Image.asset('assets/flutterfire_300x.png'),
                ),
              );
            },
          );
        }

        return const HomeScreen();
      },
    );
  }
}
0

There are 0 answers