cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
edtroit
Helper I
Helper I

Redirect a user, based on their webrole, to a certain page only after they log in.

Hi Everyone,

 

I'm trying to redirect a user, based on their webrole, to a certain page only after they log in.

 

I originally tried something like this with help from @OliverRodrigues, but I came to realize this did not account for checking when the user logged in. https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/400534/redirect-based-on-webrole/1...

 

{% if user.roles contains 'WebRoleOne' %}
        {% assign redirectURL = sitemarkers["PageOne"].url %}
    {% else %}
        {% assign redirectURL = sitemarkers["PageTwo"].url %}
    {% endif %}
    
     <script>
         window.location.href = '{{ redirectURL }}';
    </script>

 

 

 

I have tried the method from https://himbap.com/blog/?p=2983 without any luck. 

 

 

<script type="text/javascript">  
            if(window.sessionStorage) {  
              if(!(sessionStorage.getItem("firstredirect")))  
              {  
                     sessionStorage.setItem("firstredirect","1");  
                     window.location.href = '{{ redirectURL }}';  
              }  
               }  
           </script>

 

 

 

 

@nhayduk, I saw your reply from this older post PowerApps Portal: redirect authorized user to a different web page. Could you possibly explain more how you would figure out if a user is logging in?  

 

 

Thank you to anyone for your help!

 

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @edtroit,

 

Time to edit your Header Web Template - make the following change:

Search for "links/login" - it should look like the following, or similar

<a role="menuitem" aria-label="{{ snippets["links/login"] | default:resx["Sign_In"] | escape }}" href="{% if homeurl%}/{{ homeurl }}{% endif %}{{ website.sign_in_url_substitution }}">
     {{ snippets["links/login"] | default:resx["Sign_In"] | escape }}
</a>

This is the liquid & HTML code in your navigation bar/header that shows your Sign In button. We will add a "return URL" to it, so that when the user completes their sign in they will go to the home page with a specific parameter so we know they just signed in.

 

Make it this instead - notice we add "?signin=true" to the end of the anchor's href:

<a role="menuitem" aria-label="{{ snippets["links/login"] | default:resx["Sign_In"] | escape }}" href="{% if homeurl%}/{{ homeurl }}{% endif %}{{ website.sign_in_url_substitution }}?signin=true">
     {{ snippets["links/login"] | default:resx["Sign_In"] | escape }}
</a>

Now change the code I mentioned before to this:

{% if user and params.signin %}
  {% if user.roles contains 'WebRoleOne' %}
    {% assign redirectURL = sitemarkers["PageOne"].url %}
  {% else %}
    {% assign redirectURL = sitemarkers["PageTwo"].url %}
  {% endif %}

  <script>
    window.location.href = '{{ redirectURL }}';
  </script>
{% endif %}

 

ALTERNATIVELY - if you want to have some real fun, use the steps outlined in this blog post to have one page that shows one or the other form based on Web Role (instead of based on Status, like the example).

View solution in original post

6 REPLIES 6
justinburch
Microsoft
Microsoft

Hi @edtroit,

It sounds like you only want to redirect if the user is logged in - if the user has WebRoleOne, go to page 1, otherwise page 2. Else (if not logged in) stay on Home page.

Seeing if a user is logged in is as simple as {% if user %}, as there is no user object for an anonymous user.

 

{% if user %}
  {% if user.roles contains 'WebRoleOne' %}
    {% assign redirectURL = sitemarkers["PageOne"].url %}
  {% else %}
    {% assign redirectURL = sitemarkers["PageTwo"].url %}
  {% endif %}

  <script>
    window.location.href = '{{ redirectURL }}';
  </script>
{% endif %}

 

Hi @edtroit 

can you please expand a bit more your requirement? also can you tell us if you are facing any errors? or the code is not working?

 

where are you adding the code? and what's the behaviour?




If you like this post, give a Thumbs up. Where it solved your request, Mark it as a Solution to enable other users find it.

Power Apps Portals Super User


Oliver Rodrigues


 

Hey @justinburch and @OliverRodrigues  thank you thus far for your help!

 

  1. can you please expand a bit more your requirement?
    • I have a portal with two web forms for two different user types and these are on restricted web pages based on web roles. I would like for the user to be sent to the page with the web form, that their web role allows them to see, when they log in vs the home page.
  2. also can you tell us if you are facing any errors? or the code is not working?
    • I do not see any errors, but the code does not work like I would like.
  3. where are you adding the code? and what's the behaviour?
    • I placed the code at the top of my local Home Web page. I tried the code suggested by @justinburch and it worked redirecting the user once logged in to the correct page, but after that the user cannot leave that page. Whenever another link is clicked it reverts back to the redirect page based on the user web role. I would like the user to be able to access the rest of the site too, the redirect is just to make it easier for the user, because the form should be their first interaction with the site upon login. 

So getting closer just need to figure out why the redirect wont stop after log in. Thank you again for the help.

 

 

Hi @edtroit,

 

Time to edit your Header Web Template - make the following change:

Search for "links/login" - it should look like the following, or similar

<a role="menuitem" aria-label="{{ snippets["links/login"] | default:resx["Sign_In"] | escape }}" href="{% if homeurl%}/{{ homeurl }}{% endif %}{{ website.sign_in_url_substitution }}">
     {{ snippets["links/login"] | default:resx["Sign_In"] | escape }}
</a>

This is the liquid & HTML code in your navigation bar/header that shows your Sign In button. We will add a "return URL" to it, so that when the user completes their sign in they will go to the home page with a specific parameter so we know they just signed in.

 

Make it this instead - notice we add "?signin=true" to the end of the anchor's href:

<a role="menuitem" aria-label="{{ snippets["links/login"] | default:resx["Sign_In"] | escape }}" href="{% if homeurl%}/{{ homeurl }}{% endif %}{{ website.sign_in_url_substitution }}?signin=true">
     {{ snippets["links/login"] | default:resx["Sign_In"] | escape }}
</a>

Now change the code I mentioned before to this:

{% if user and params.signin %}
  {% if user.roles contains 'WebRoleOne' %}
    {% assign redirectURL = sitemarkers["PageOne"].url %}
  {% else %}
    {% assign redirectURL = sitemarkers["PageTwo"].url %}
  {% endif %}

  <script>
    window.location.href = '{{ redirectURL }}';
  </script>
{% endif %}

 

ALTERNATIVELY - if you want to have some real fun, use the steps outlined in this blog post to have one page that shows one or the other form based on Web Role (instead of based on Status, like the example).

THANK YOU @justinburch  this worked perfectly! I look forward to reading your blog post to see the alternative possibilities. 

 

Thank you everyone else for your time and help too!

 

 

Hi,

 

I am trying to add the same solution but it gives me Tag ‘{%’ was not properly terminated with regexp: (?-mix:\%\}) error when added ?signin=true In header template 

 

attached are the screenshots in pdf

 

requirement is “whenever a portal contact access portal based on brand value access should be restricted. 

solution: trying to show unauthorised web page when contact log ins. 

Helpful resources

Announcements
October Events

Mark Your Calendars

So many events that are happening this month - don't miss out!

MPP IDEAS

Ideas

Discover ideas and concepts from users like you for how to use Power Pages and take your work to the next level.

Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Carousel Community Blog

Check out the Community Blog

Read all about the most recent blogs in the community!

Users online (2,706)