4

In my system I need to create users from an authenticated user, but for this I need to create a user through the createUserWithEmailAndPassword method and when I create the user through this method the authentication status changes to the user that was created at the moment.

Is there a way to create users from an authenticated user without making signIn for the user created after it was created by the createUserWithEmailAndPassword method?

I have a system developed in Angular + Node.js

  createUser(user: Usuario): Promise<firebase.auth.UserCredential> {
    return this._angularFireAuth.auth.createUserWithEmailAndPassword(user.email, user.password);
  }
Luiz
  • 449
  • 5
  • 17

4 Answers4

4

[ANSWER]

You can create a new Firebase App context in your client and then call createUserWithEmailAndPassword() again. This will not be re-authenticate to the the new user created. Reference.

var authApp = firebase.initializeApp({
// ...
}, 'authApp');
var detachedAuth = authApp.auth();

detachedAuth.createUserWithEmailAndPassword('foo@example.com', 'asuperrandompassword');

Potential Problem when doing this is: Firebase App named '[DEFAULT]' already exists (app/duplicate-app).

[OTHER SCENARIO (for others references)]

I assume you meant either one of these:

  1. You want the signed-in user to keep signed-in even after closing the browser. If this is the case, you could use the persistence provide by the firebase auth.

  2. You want to hide your sign-in button when the user state change from "not signed-in" to "signed-in". Then you can check this post.

  3. Other reference: How to keep user logged in with Firebase no matter what?

Hope it helps!

Angus
  • 3,680
  • 1
  • 12
  • 27
  • I want to keep the user authenticated when a new user is created, in my system it is possible to create users when there is an authenticated user. – Luiz Aug 08 '18 at 01:54
  • I hope you can tell me the workflow, so is the flow like this: 1. Sign-up with email and password 2. Close browser 3. Open again, still signed-in or 1. New user but already have record in the auth (you change in the auth section yourself beforehand) 2. They can sign in without sign up themself. OR? Give us scenario to help you – Angus Aug 08 '18 at 02:01
  • 1. Through an authenticated user I create another user. 2. When creating a user, you must continue logged in with the user who created the new user instead of changing for the created user. SOLUTION: When I call the `createUserWithEmailAndPassword ()` method it automatically sets the user created as the authenticated user and I do not want this to happen. – Luiz Aug 08 '18 at 02:07
  • 1
    @Luiz Oh, you mean when user A somehow use your website to create a new account for user B, after signing-up, user A is logged out but user B is signed-in? – Angus Aug 08 '18 at 02:09
  • 2
    That's right ... user A is an administrator user and this is creating a new user B for someone else to access, but user A must remain authenticated after user B has been created. – Luiz Aug 08 '18 at 02:15
  • @Luiz I did not do this in angular before, will update the answer once I get it – Angus Aug 08 '18 at 02:17
  • At the moment I reauthenticate the user who created a new user, but before that it changes the user state for the created user and this is bad for me. – Luiz Aug 08 '18 at 02:19
  • @Luiz it is tricky here, as firebase 'encourage' developer to use firebase.auth().currentUser; I found a thread take a look here: https://stackoverflow.com/questions/37553362/firebase-create-user-manually – Angus Aug 08 '18 at 02:26
  • I like your idea and logic should work but there is an error, I will have to see what is needed to avoid this error: `FirebaseError {code: "app / duplicate-app", message: "Firebase: Firebase App named '[DEFAULT "[DEFAULT]", "[DEFAULT]: Firebase: Firebase App named '[DEFAULT] ... Task (http: // localhost: 4200 / polyfills)" already exists (app / duplicate-app) .js: 7623: 31) "}` – Luiz Aug 08 '18 at 02:47
  • @Luiz I see, the problem now is you are probably re-initializing the firebaseApp, take a look here https://github.com/zeit/next.js/issues/1999 I saw some potential solutions – Angus Aug 08 '18 at 02:56
  • It worked for me now I made a mistake ... I forgot to pass the parameter on call `initializeApp ({config}, 'authApp');` now it's all right! That's what I needed, thank you! – Luiz Aug 08 '18 at 02:58
  • No problem, glad I can help you out (Even I got no experience in Angular) Cheers! – Angus Aug 08 '18 at 03:00
1

After create user, you need call the signInWithEmailAndPassword sign in.

    signInFirebaseWithEmailAndPassword(
       userName: string,
       password: string
    ): Promise<firebase.auth.UserCredential> {
       return this._angularFireAuth.auth.signInWithEmailAndPassword(userName, password);
    }
youDaily
  • 1,372
  • 13
  • 21
  • I do not want to change the status of the authenticated user, so do not avoid changing the status of the authenticated user ... – Luiz Aug 08 '18 at 02:50
0

Sounds like an administrator function. See Introduction to the Admin Auth API

User Management

It is not always convenient to have to visit the Firebase console in order to manage your Firebase users. The admin user management API provides programmatic access to those same users. It even allows you to do things the Firebase console cannot, such as retrieving a user's full data and changing a user's password, email address or phone number.

Community
  • 1
  • 1
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
  • my bad, I read `Is there a way to create users from an authenticated user` to mean an admin type role. Can you elaborate on what type of user you want to allow access to create other users (via the email method)? – Ronnie Royston Aug 08 '18 at 01:56
  • It is an administrator user who is allowed to create other users ... But when I create a user being that I am with that authenticated user (administrator) it changes the authentication state for the created user. – Luiz Aug 08 '18 at 02:03
  • Any reason you can't stand up a node server and user the Admin SDK to handle these moves, adds, and changes (MAC's)? It's a easy, quick, and free... (e.g. cloud9 IDE __private__ workspace or on your laptop)? – Ronnie Royston Aug 08 '18 at 02:06
0

You have to create a new app instance for signing up purpose, because when you use the existing app instance to sign up, the current user is automatically signed out, and the new user is signed in, therefore to prevent the current user from signing out, a new app instance is needed. The advantage of the following method is you can use the existing firebase configuration file in environment file. initializeApp() function uses the following method signature; hence you have to use a different name for the new instance. When you don't use a name, the name is "[DEFAULT]". Once the user is signed up, you can safely delete the app instance.

initializeApp ( options :  Object ,  name ? :  string ) : App

async signup(username: string, password: string): Promise<void | Observable<never>>  { 

    const firebaseApp: firebase.app.App = firebase.initializeApp(environment.firebaseConfig, 'signupInstance');

    firebaseApp.auth().createUserWithEmailAndPassword(email, password)
    .then((user: auth.UserCredential) => {
      // use user here. 

      firebaseApp.delete();

    }, (error) => {
      firebaseApp.delete();
      return throwError(error);
    });
  }
Don Dilanga
  • 2,722
  • 1
  • 18
  • 20