Selecting multi database connection based on email domain with auth0-spa.js

I wondering how I be able to select connection based on email domain used for login using auth0-spa-js that in fact is a very promising for SPA?

I read here possibility to do such connectionResolver to achieve such logic with Lock.js. But how with auth0-spa-js?

Thank for your input.

1 Like

Hi @matinfo
What you are after would not be really the responsibility of auth0-spa-js.
auth0-spa-js (running in the context of your SPA) will redirect the user to your Auth0 domain to authenticate, but then the authentication (and deciding which connection to use) will happen on the Auth0 side (on the hosted login page).
If you enable customization on the hosted login page, you’ll get Lock in the default template to handle the authentication UI. In that code you can use the connectionResolver to put your own login for deciding which connection to use.

Hope that helps!

1 Like

HI @nicolas_sabena, ok, for that point, thanks.

But how I be able to passe with such logic above to the component, like this:

ui_locales={localStorage.getItem(“lng”) || config.get(“DEFAULT_LANGUAGE”)}

Or connexion props in this case is not needs?

In most cases, the application (client) asking for an authentication should not be concerned with the connection that the user is going to use to authenticate, so auth0-spa-js should not need a connection parameter.

They connection should be decided on the login phase (either automatically based on the user’s email address or some other context, or by asking the user). That’s why the connectionResolver is an option that you configure in Lock, the UI login widget that runs on the hosted login page.

1 Like

@nicolas_sabena, first thank you for your fast support.

I confirm, that all work like expected :ok_hand:.

But for information, I noticed, when login window is shown, the settings of default database Username-Password-Authentication is used, settings like Disable Sign Ups.

1 Like

I also have issue on this. I have enable to databases for SPA application and API but I only use Username-Password-Authentication database only for other database users it gives Wrong Email or password. Even didn’t send an email for reset password

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.

Hey @iamchathu

I’m afraid I’m not understanding the problem fully. When an application (be it SPA or regular web app or native) requests a token using a browser approach (a redirection to Auth0’s /authorize endpoint), the application is usually not concerned with the authentication method (including which DB connection will be used).
The decision on what DB connection to use (if any) happens at the login page. When using the “classic” hosted login experience coupled with Lock (the UI widget), you’ll automatically be using the DB connection assigned to the application. If there’s more than one you’ll get an unspecified behavior, so you should either provide a connectionResolver or ensure that there’s only one DB connection enabled for the app. If you use Auth0.js, you are on your own and your Javascript code will need to decide with connection to use based on context (e.g. clientID requesting the authentication, user selection, or whatever makes sense for your case).