Skip to content
Rishan Solutions
Rishan Solutions
  • PowerApps
  • SharePoint online
    • Uncategorized
    • Uncategorized
  • PowerAutomate
Rishan Solutions
Latest Posts
  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025 June 24, 2025
  • Recursive Queries in T-SQL May 7, 2025
  • Generating Test Data with CROSS JOIN May 7, 2025
  • Working with Hierarchical Data May 7, 2025
  • Using TRY_CAST vs CAST May 7, 2025
  • Dynamic SQL Execution with sp_executesql May 7, 2025

Redirecting after login based on role

Posted on April 23, 2025April 23, 2025 by Rishan Solutions

Loading

Redirecting users to different pages based on their role is an important feature for portals and web applications, ensuring that users are directed to the most relevant section of the site immediately after logging in. In Power Pages (formerly Power Apps Portals), you can achieve this by utilizing role-based security to conditionally redirect users based on their assigned roles. This allows for a personalized experience for users, ensuring they access the content that matches their role.

Here’s a step-by-step guide on how to implement role-based redirection after login in Power Pages.


Step 1: Understand Role-Based Security in Power Pages

In Power Pages, security is managed using Web Roles. Web Roles define what a user can access on the portal. These roles are tied to Dataverse security roles, which are used to determine the permissions and access levels within the portal.

You may have different web roles, such as:

  • Administrator
  • Customer
  • Manager
  • Guest

When a user logs in, you want to redirect them based on the web role they are assigned. For example:

  • Admins might be redirected to the admin dashboard.
  • Customers might be redirected to the homepage or their profile page.
  • Managers might be redirected to a team management page.

Step 2: Create Web Roles in Power Pages

Before setting up redirection, you need to ensure that the necessary Web Roles are created for your portal users.

  1. Go to Power Pages Portal Management:
    • Navigate to the Portal Management app in Power Apps.
  2. Create Web Roles:
    • Go to Security > Web Roles.
    • Click on New to create new roles, such as Administrator, Customer, Manager, etc.
    • Assign appropriate permissions to each role based on the features users should access.

Step 3: Use JavaScript for Role-Based Redirection

To redirect users after login based on their role, you will use JavaScript to check the user’s role and then redirect them accordingly. This can be done by accessing the current user’s role through the Liquid templating or by using JavaScript on the portal pages.

Example Using JavaScript for Role-Based Redirection:

  1. Get the Current User’s Web Role: You can use the Liquid templating language to determine the user’s role or query it via the JavaScript SDK. Here’s how to do it:
window.addEventListener('DOMContentLoaded', function() {
var userRoles = window.location.pathname.includes('authenticated') ? getUserRoles() : [];

if (userRoles.includes('Administrator')) {
// Redirect to admin dashboard
window.location.href = '/admin-dashboard';
} else if (userRoles.includes('Customer')) {
// Redirect to customer homepage
window.location.href = '/customer-home';
} else if (userRoles.includes('Manager')) {
// Redirect to manager page
window.location.href = '/manager-dashboard';
} else {
// Default redirection (if no role matches)
window.location.href = '/default-page';
}
});

// Function to fetch roles (This function can be customized to match your needs)
function getUserRoles() {
// Placeholder for getting user roles from portal
// This could involve querying the Dataverse security roles or pulling data from a user profile.
return ['Customer']; // For demonstration, return a static role. Replace with dynamic logic.
}
  1. Insert JavaScript into Portal:
    • Go to the Portal Management app in Power Pages.
    • Navigate to Web Pages > Your Login Page or Home Page.
    • Add this script in the JavaScript section or inject it into your custom webpage using a web template.

Explanation of the Script:

  • The script uses window.location.pathname to check if the page is being accessed by an authenticated user.
  • The getUserRoles() function (which is a placeholder) can be replaced with logic to retrieve the user’s actual roles, perhaps through the portal API or by querying Dataverse.
  • Depending on the roles the user has, the page redirects them to the appropriate URL.

Step 4: Use Liquid to Fetch User Roles (Alternative)

If you prefer using Liquid templating, it is possible to directly access the user’s roles and set up redirection within the portal. However, Liquid can’t directly perform redirection, so you would need to use it in conjunction with JavaScript.

For example, you can fetch the user’s roles via Liquid:

{% assign user_roles = user.roles %}
{% if user_roles contains 'Administrator' %}
<script>
window.location.href = "/admin-dashboard";
</script>
{% elsif user_roles contains 'Customer' %}
<script>
window.location.href = "/customer-home";
</script>
{% elsif user_roles contains 'Manager' %}
<script>
window.location.href = "/manager-dashboard";
</script>
{% else %}
<script>
window.location.href = "/default-page";
</script>
{% endif %}

This template checks if the current user has a specific role and dynamically inserts a <script> tag to redirect them accordingly.

Liquid Logic Breakdown:

  • user.roles: Fetches the current user’s roles.
  • contains: Checks if a specific role is assigned to the user.
  • The appropriate <script> tag is inserted to redirect the user based on their role.

Step 5: Test the Redirection Logic

Once you’ve set up the redirection logic, you should test it to ensure that it’s working as expected.

  1. Test Different Roles: Log in as a user with each of the different roles (e.g., Administrator, Customer, Manager) and verify that you are redirected to the correct page.
  2. Check for Edge Cases: Ensure users who don’t have any roles or whose roles don’t match your conditions are redirected to a default page.
  3. Use Browser Developer Tools: Open the browser’s developer tools to inspect if the correct role-based redirect is triggered and whether there are any errors.

Step 6: Handle Edge Cases

  1. User Without Roles: Ensure that if a user doesn’t have any assigned roles, they’re redirected to a general or default page. For example, if a user without roles tries to access the site, you may want to redirect them to a public or login page.
  2. Role Changes: If roles are updated frequently, make sure to test the system after role changes to ensure that redirection still works as expected.

Step 7: Security Considerations

  • Security of Redirects: Ensure that the role information you are using for redirection is securely managed. Avoid allowing users to manipulate the redirection logic by modifying URLs or roles.
  • Access Control: Even though users are redirected based on their roles, ensure that proper role-based access control (RBAC) is set up on the portal to prevent unauthorized access to certain pages.
Posted Under Power PagesJavaScript Liquid Templating portal access control Power Apps Portals Power Pages Redirect After Login Role-Based Access Role-based Redirection user authentication Web Roles

Post navigation

Deep-linking into portal forms
Route-based form loading

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025
  • Recursive Queries in T-SQL
  • Generating Test Data with CROSS JOIN
  • Working with Hierarchical Data
  • Using TRY_CAST vs CAST

Recent Comments

  1. Michael Francis on Search , Filter and Lookup in power apps
  2. A WordPress Commenter on Hello world!

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • March 2024
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • January 2022

Categories

  • Active Directory
  • AI
  • AngularJS
  • Blockchain
  • Button
  • Buttons
  • Choice Column
  • Cloud
  • Cloud Computing
  • Data Science
  • Distribution List
  • DotNet
  • Dynamics365
  • Excel Desktop
  • Extended Reality (XR) – AR, VR, MR
  • Gallery
  • Icons
  • IoT
  • Java
  • Java Script
  • jQuery
  • Microsoft Teams
  • ML
  • MS Excel
  • MS Office 365
  • MS Word
  • Office 365
  • Outlook
  • PDF File
  • PNP PowerShell
  • Power BI
  • Power Pages
  • Power Platform
  • Power Virtual Agent
  • PowerApps
  • PowerAutomate
  • PowerPoint Desktop
  • PVA
  • Python
  • Quantum Computing
  • Radio button
  • ReactJS
  • Security Groups
  • SharePoint Document library
  • SharePoint online
  • SharePoint onpremise
  • SQL
  • SQL Server
  • Template
  • Uncategorized
  • Variable
  • Visio
  • Visual Studio code
  • Windows
© Rishan Solutions 2025 | Designed by PixaHive.com.
  • Rishan Solutions