Firebase Create User Without Sign In

How to avoid being signed in automatically when a new user is created using firebase

There is no direct way provided for web development to create user without signin. Instead there is method to create user without sign-in in Admin-SDK which will solve your problem.

To access this function you have to use node.js in firebase function.

how to sign up users without login in firebase/js

You can initialize a separate Firebase SDK instance to handle all of your account creation requests.

In it's simplest form, you can use:

let authWorkerApp = firebase.initializeApp(firebase.app().options, 'auth-worker');
let authWorkerAuth = firebase.auth(authWorkerApp);
authWorkerAuth.setPersistence(firebase.auth.Auth.Persistence.NONE); // disables caching of account credentials

authWorkerAuth.createUserWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// ...
});

If you encounter errors such as Firebase app 'auth-worker' is already initialised, you can wrap it in a safe getter to avoid such an error:

function getFirebaseApp(name, config) {
let foundApp = firebase.apps.find(app => app.name === name);
return foundApp ? foundApp : firebase.initializeApp(config || firebase.app().options, 'auth-worker');
}

let authWorkerApp = getFirebaseApp('auth-worker');

Flutter: Firebase authentication create user without logging In

Updated: firebase_core ^0.5.0 and firebase_auth ^0.18.0+1 has deprecated some of the old classes.

Below is code updated for firebase_core ^0.5.1 and firebase_auth ^0.18.2.

static Future<UserCredential> register(String email, String password) async {
FirebaseApp app = await Firebase.initializeApp(
name: 'Secondary', options: Firebase.app().options);
try {
UserCredential userCredential = await FirebaseAuth.instanceFor(app: app)
.createUserWithEmailAndPassword(email: email, password: password);
}
on FirebaseAuthException catch (e) {
// Do something with exception. This try/catch is here to make sure
// that even if the user creation fails, app.delete() runs, if is not,
// next time Firebase.initializeApp() will fail as the previous one was
// not deleted.
}

await app.delete();
return Future.sync(() => userCredential);
}

Original Answer

I experimented with the firebase authentication api and my current working solution is:

// Deprecated as of `firebase_core ^0.5.0` and `firebase_auth ^0.18.0`.
// Use code above instead.

static Future<FirebaseUser> register(String email, String password) async {
FirebaseApp app = await FirebaseApp.configure(
name: 'Secondary', options: await FirebaseApp.instance.options);
return FirebaseAuth.fromApp(app)
.createUserWithEmailAndPassword(email: email, password: password);
}

Essentially it comes down to creating a new instance of FirebaseAuth so the automatic login from createUserWithEmailAndPassword() do not affect the default instance.

How can I create a new user via Firebase Auth without signing in?

This is a good use case for the Firebase Admin SDKs. Instead of creating the user client-side, you create the user in a managed environment, like a server or Cloud Functions. You have the client make a call to your endpoint when you want to add a new user. This codelab shows how to incorporate custom claims using the Firebase Admin Auth SDK. This is a little different from what you're exactly looking for, but it can get you started in the right direction.

Firebase create user without sign in

Here is a tested solution you can apply (just implemented a few minutes before).

For creating a new user account you need the reference of FirebaseAuth.

So you can create two different FirebaseAuth objects like:

private FirebaseAuth mAuth1;
private FirebaseAuth mAuth2;

Now in the onCreate you can initialize them as:

   @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

mAuth1 = FirebaseAuth.getInstance();

FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
.setDatabaseUrl("[Database_url_here]")
.setApiKey("Web_API_KEY_HERE")
.setApplicationId("PROJECT_ID_HERE").build();

try { FirebaseApp myApp = FirebaseApp.initializeApp(getApplicationContext(), firebaseOptions, "AnyAppName");
mAuth2 = FirebaseAuth.getInstance(myApp);
} catch (IllegalStateException e){
mAuth2 = FirebaseAuth.getInstance(FirebaseApp.getInstance("AnyAppName"));
}

//..... other code here
}

To get ProjectID, WebAPI key you can go to Project Settings in your firebase project console.

Now to create the user account you have to use mAuth2, not mAuth1. And then on successful registration, you can log out that mAuth2 user.

Example:

private void createAccount(String email, String password)
{
mAuth2.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {

if (!task.isSuccessful()) {
String ex = task.getException().toString();
Toast.makeText(RegisterActivity.this, "Registration Failed"+ex,
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(RegisterActivity.this, "Registration successful",
Toast.LENGTH_SHORT).show();
mAuth2.signOut();
}

// ...
}
});

}

The point where you have to worry(actually not):

The admin should only be able to create the new user accounts. But the above solutions is allowing all authenticated user to create a new user account.

So to solve this problem you can take help of your firebase real-time database. Just add a key like "is_user_admin" and set the value as true from the console itself. You just need to validate the user before someone is trying to create a new user account. And using this approach you can set your own admin.

As of now, I don't think there is firebase-admin SDK for android. So one can use the above approach.

Can Firebase users created without a password still sign in?

Firebase Authentication users are associated with one or more providers, and many of those providers don't need the user profile to have an associated password. For example: if you sign into Firebase with your Facebook account, the Firebase Authentication profile will not have an associated password. This applies to most providers, as in most cases the password is stored elsewhere (Facebook, Google, LinkedIn, Microsoft, etc), or ephemeral (email-link, phone auth).



Related Topics



Leave a reply



Submit