How to switch databases after user login

There are times when you may want to have a user log in to your app and then have the app redirect to use a different database.
This is not a difficult thing to do but there is a different process for SQL and MultiValue databases and both require a minor custom code change as described below.


How to switch databases after user login (MultiValue database)
This technique assumes that you are connecting through to a MultiValued database using Core Objects.
In order to switch over to a different database after the use has successfully logged in (which will authenticate using the database specified in the app design), you need to add custom code to 2 custom code files.
The first is Web\Scripts\App\App.js
In this file, the App.custom.loginCompleted function needs to be defined as follows:
App.custom.loginCompleted = function (User) {
App.utility.contextDataAdd("LoginProfileToUse", User.MvNetLogin);
}
The above code assumes that the required mv.NET login profile name is held in the MvNetLogin property of the AppUser record (which is passed in via the User argument). The purpose of this code is to set the required value into the context data area, which allows the value to be accessed in the middle tier.
The second piece of custom code needs to be added to the file:
RepositoryAccess\Repositories\MultiValue\CoreObjects\_RepositoryConnection.cs
In this file, the LoginProfileNameOverride function needs to be defined as:
public static string LoginProfileNameOverride(string LoginProfileName, Dictionary<string, string[]> ContextData)
{
if (ContextData.ContainsKey("LoginProfileToUse"))
return ContextData["LoginProfileToUse"][0];
else
return LoginProfileName;
}
The above code looks for the context data value added by the first piece of code above and if it is present it uses its value to override the default login profile for the app.


How to switch databases after user login (SQL database)
This technique assumes that you are connecting through to aa SQL databases.
In order to switch over to a different database after the use has successfully logged in (which will authenticate using the database specified in the app design), you need to add custom code to 2 custom code files.
The first is Web\Scripts\App\App.js
In this file, the App.custom.loginCompleted function needs to be defined as follows:
App.custom.loginCompleted = function (User) {
App.utility.contextDataAdd("ConnectionData", User.DBConnectionData);
}
The above code assumes that the required connection data is held in the DBConnectionData property of the AppUser record (which is passed in via the User argument). The purpose of this code is to set the required value into the context data area, which allows the value to be accessed in the middle tier.
The second piece of custom code needs to be added to the file:
RepositoryAccess\Repositories\SQL\{database type}\RepositoryConnection.cs
In this file, the ConnectionString function needs to be defined as:
public string ConnectionString(string DefaultConnectionString, string RepositoryId, RepositorySet RepositorySet, Dictionary<string, string[]> ContextData)
{
if (ContextData.ContainsKey("ConnectionData"))
return ContextData["ConnectionData"][0];
else
return null;
}
The above code looks for the context data value added by the first piece of code above and if it is present it uses its value to override the default connection string for the app.