CSFrequency / react-firebase-hooks

React Hooks for Firebase.
Apache License 2.0
3.55k stars 304 forks source link

createUserWithEmailAndPassword does not return user credentials after creation #251

Closed grubersjoe closed 1 year ago

grubersjoe commented 2 years ago

Hey. Very nice project, thanks for maintaining this!

I'm using the createUserWithEmailAndPassword() hook, but I've noticed that it does not return the user credentials like in this Firebase example. I think this is really useful when you want to immediately set data for the new user. In my case I want to set an username that is entered in the registration form:

import { createUserWithEmailAndPassword } from 'firebase/auth';

 const onSubmit: SubmitHandler<RegisterForm> = data => {
  setLoading(true);
  createUserWithEmailAndPassword(firebaseAuth, data.email, data.password)
    .then(credentials => setUserName(credentials.user.uid, data.name))
    .catch(setError)
    .finally(() => setLoading(false));
};

The above version uses createUserWithEmailAndPassword from firebase/auth directly but I'd love if the hook would support this out of the box. Simply returning the user inside the hook should be sufficient:

const signInWithEmailAndPassword = async (
  email: string,
  password: string,
) => {
  setLoading(true);
  setError(undefined);
  try {
    const user = await firebaseSignInWithEmailAndPassword(
      auth,
      email,
      password,
    );
    setLoggedInUser(user);

    return user; // <<<
  } catch (err) {
    setError(err as AuthError);
  } finally {
    setLoading(false);
  }
};

What do you think? I can also open a PR if you like.

chrisbianca commented 1 year ago

@grubersjoe Apologies for the delay. This seems like a sensible idea to me and would welcome a PR!

grubersjoe commented 1 year ago

Hey. Thank's for adding this. Highly appreciated :)!