Sub topic 13
Customizing the login process with login flows
This chapter assumes you can build a simple screen flow in Flow Builder and can assign a profile to a user. It also assumes you know the difference between authentication (proving who you are) and authorization (what you may do). Earlier chapters covered MFA, session settings, and IP restrictions. Login flows add your own logic after a user has signed in.
What a login flow is
A login flow is a screen flow that Salesforce runs after a user authenticates and before the user reaches the org. The user sees the flow screens in the browser. Salesforce lets them into the org only when the flow finishes. If the flow ends by signing the user out, the session ends.
Typical uses include:
- Showing a terms-of-use or security acknowledgment screen.
- Asking a user to register a phone number or a verification method.
- Collecting a custom second factor, such as a one-time code sent through your own service.
- Checking login context (IP address, login type, location) and blocking or redirecting risky logins.
- Sending users to a specific page when they finish.
Login flows complement built-in MFA. They do not replace it. Use the built-in MFA requirement for verification, and use login flows for steps that Salesforce does not provide.
Where the flow runs in the login sequence
The login flow comes late in the sequence. Credential checks, SSO, IP restrictions, and any MFA challenge all happen first.
Because the user is already authenticated when the flow starts, the flow can read information about the user and the login. It also means a flow is not a substitute for network restrictions. A login that fails the IP check never reaches the flow.
Building the flow
Create the flow in Flow Builder as a Screen Flow. Activate it before you connect it to anyone.
Special variables
Salesforce passes login context into the flow through input variables. You create them yourself in the flow. The names must match exactly, and each must be marked as available for input or output as described below.
| Variable | Direction | Purpose |
|---|---|---|
| `LoginFlow_UserId` | Input | Id of the user who is signing in |
| `LoginFlow_LoginType` | Input | How the user signed in, such as an application or SSO login |
| `LoginFlow_IpAddress` | Input | Source IP address of the login |
| `LoginFlow_FinishLocation` | Output | URL where the user lands when the flow finishes |
| `LoginFlow_ForceLogout` | Output | Boolean. When true, Salesforce ends the session |
Salesforce documents more context variables, such as browser, platform, and location details. Check the current list in the Salesforce help for your release before you depend on one. Create only the variables your logic uses.
A simple pattern
A flow that enforces an acknowledgment screen can look like this:
- A Screen element shows the policy text and a required checkbox.
- A Decision element checks whether the box was ticked.
- If it was, the flow ends normally and the user continues.
- If it was not, an Assignment element sets
LoginFlow_ForceLogoutto true, and the flow ends.
A flow that reads LoginFlow_IpAddress can add a branch. For example, it can require an extra confirmation screen for logins from outside your corporate range.
Connecting the flow to users
A login flow applies to users through their profile. Go to Setup, search for Login Flows in Quick Find, and click New. Then:
- Enter a name for the connection.
- Select the active flow.
- Select the user license and the profile.
- Save.
Each profile can have one login flow. Everyone with that profile gets the flow at their next login, so connect it first to a test profile with a few users before you widen it.
Experience Cloud site users sign in through profiles as well, so a login flow can apply to them. Test that path separately, because the screens appear inside the site's login experience.
Finishing a flow from custom code
You can build screens as Lightning components inside a flow. When a custom component needs to complete the login, call Auth.SessionManagement.finishLoginFlow from Apex. It tells Salesforce the user has passed your custom check and returns the page where the user should land.
public with sharing class LoginFlowController {
@AuraEnabled
public static String finish(String startUrl) {
PageReference destination = Auth.SessionManagement.finishLoginFlow(startUrl);
return destination.getUrl();
}
}Only call finishLoginFlow after your own verification has succeeded. If the method runs on an unverified path, the check is meaningless.
Common errors and edge cases
Login flows sit on the sign-in path, so mistakes lock people out. Plan for these cases.
- A broken flow blocks everyone on the profile. If the flow throws an unhandled error or has a screen with no way forward, users on that profile cannot get in. Keep a break-glass administrator account on a profile with no login flow. An administrator can also remove the connection on the Login Flows page to restore access.
- Inactive or deleted flows. Deactivating the flow that is connected to a profile causes login problems. Remove the connection before you retire a flow.
- Permissions inside the flow. The flow runs while the user is signing in. Elements that read or write records can fail if the user lacks access. Test with a standard user, not as an administrator.
- Unreachable end states. Every path should end by finishing the flow or setting
LoginFlow_ForceLogout. Debug each branch. - API and integration logins. A login flow is an interactive, browser-based step. Integrations that sign in through the API or use a non-interactive OAuth flow have no screen to show, so do not rely on a login flow to control them. Use IP restrictions, connected app policies, and permission design for those.
- Replacing MFA with a login flow. A custom code check is easy to get wrong. Use it for steps that Salesforce MFA cannot do. Keep the built-in MFA requirement in place.
- Mobile and site differences. Screens that work in a desktop browser may render differently in the mobile app or in a site. Test on each client your users use.
Verifying the effect
After you connect a flow, check login history to confirm that users are still signing in successfully. This query lists the last seven days of logins, with source IP and status.
SELECT UserId, LoginTime, LoginType, SourceIp, Status, Application
FROM LoginHistory
WHERE LoginTime = LAST_N_DAYS:7
ORDER BY LoginTime DESC
LIMIT 200Compare the number of failed or missing logins for the affected profile before and after the change. A sudden drop in successful logins from one profile suggests the flow is blocking people.
A rollout checklist for a new login flow:
- Build and debug the flow in a sandbox.
- Connect it to a test profile with a small number of users.
- Confirm each branch, including the forced logout, works.
- Confirm a break-glass administrator can still sign in.
- Connect the flow to the production profile outside peak login hours.
- Review login history the same day.
Summary
A login flow is a screen flow that runs after authentication and before the user reaches the org. You connect it to a profile and user license on the Login Flows page. It reads login context through special variables and can redirect the user or force a logout. Use it for custom steps, keep built-in MFA as the main verification, and test carefully because mistakes block sign-in.
Exercise
A login flow connected to the Sales profile has a bug, and no Sales user can sign in. How do you restore access, and what should have been in place beforehand?
Show answer
Sign in as an administrator on a profile with no login flow, open Setup, go to Login Flows, and remove or change the connection for the Sales profile. Beforehand, you should have tested the flow in a sandbox and with a small group, and kept a break-glass administrator account on a profile with no login flow.
Exercise
Which flow variable do you set to true to end a user's session from inside a login flow, and why can't a login flow be used to control an API integration's sign-in?
Show answer
Set LoginFlow_ForceLogout to true. A login flow shows screens in an interactive browser session, and an API or non-interactive integration login has no screen to show, so you must control those with IP restrictions, connected app policies, and permissions.