0

I am trying to create an ERP type application where the manager will be able to assign things to a particular user in the database through a GUI. As such I would like to replace the random UID generated by firebase using the auth.createUserWithEmailAndPassword with a name variable, I have provided a name input in the login where the employee can enter their name. What is the best way to replace the UID with this name instead?

I am using React.js here is some code snippet:

my auth.js:

import { firebase } from '../firebase/firebase';


export const login = (uid) => ({
  type: 'LOGIN',
  uid
});

export const newUser = () => {
  const username = username.value;
  const pass = password.value;
  const auth = firebase.auth();

  const promise = auth.createUserWithEmailAndPassword(username, pass);
  promise.catch(e => console.log(e.message));
};

export const startLogin = (username, password) => {
  return () => {
  const username = username.value;
  const pass = password.value;
  const auth = firebase.auth();

  const promise = auth.signInWithEmailAndPassword(username, pass);
  promise.catch(e => console.log(e.message));
  };
};

export const logout = () => ({
  type: 'LOGOUT'
});

export const startLogout = () => {
  return () => {
    return firebase.auth().signOut();
  };
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

Let me know if there is anything else you need to see to help me.

Thanks.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

Firebase Authentication users have a display name that you might consider using for this. For user accounts from social providers, it is the name that you've given there. For example: if Firebase Authentication supported Stack Overflow as an identity provider your UID might be 8603299 and your display name would be "Bruce Evens".

A few things to keep in mind there:

  • A user's display name does not have to be unique. Anyone could say their name is "Bruce Evens". So while you can use the name as a friendly description, you cannot use the name to uniquely look up a user. If you want to ensure unique user names, you'll want to keep a mapping somewhere. A common place to do this is in the Firebase Realtime Database. See these questions for some examples.
  • A user's display name may change over time. For example, I've been "puf" here on Stack Overflow, but am now "Frank van Puffelen". This leads to potential problems: if people tagged me in questions/comments as @puf back when that was my display name, that tag will now no longer be linked to my account. To prevent that sort of problem, you'd have to update all references when a user changes their display name. I've covered the basic options for that here.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I see. Considering this is an internal application for a business, i didn't want to use any login besides email and password, which do not have a display name. I do see the purpose of the mapping though. I guess I will have to set a name property on the id. – Bruce Evers Nov 19 '17 at 20:54