Jump to content

Redirect based on user authentication


Recommended Posts

I am wondering if there is a way to redirect users to another webpage based on the role of the logged in user.  I currently have this working when a user logs in for the first time and I pasted that script below.

<script>
if ("[@authfield:Users_tbl_Role]" == "Manager"){
window.location="https://website.com/manager-portal";
}
else{
window.location="https://website.com/regular-portal";
}

</script>

I want to use this same funcionality on search results page of some of my data pages.  The data pages currently have an HTML block that is an a href hyperlink that redirect the user to "customer profiles" and accepts parameters to do so.  However, my manager users and regular users need to be redirected to different webpages.  Is it possible to have hyperlinks redirect to different webpages based on the roles of the user signed in?  I have also tried to use a button instead of a hyperlink with a similar script above to execute onClick, however, I have not been able to get that right. 

I appreciate any suggestions!  This functionality would allow me to use less data pages and thus make it simpler for me to make updates to only one data page that works for all users rather than making the same updates to several similar data pages like I currently am.

Link to comment
Share on other sites

Hello @Scott17

Try this code for your Hyperlink in HTML Block:

<div id="link"></div>
<script type="text/javascript">
document.addEventListener('DataPageReady', function (event) {
if ("[@authfield:Users_tbl_Role]" == "Manager") {
document.getElementById("link").innerHTML='<a href="https://website.com/manager-portal">Manager Portal</a>';
 }

else{
document.getElementById("link").innerHTML='<a href="https://website.com/regular-portal">Regular Portal</a>';
}

});
</script>

I hope this helps.

Link to comment
Share on other sites

17 hours ago, IamGroot said:

Hello @Scott17

Try this code for your Hyperlink in HTML Block:

<div id="link"></div>
<script type="text/javascript">
document.addEventListener('DataPageReady', function (event) {
if ("[@authfield:Users_tbl_Role]" == "Manager") {
document.getElementById("link").innerHTML='<a href="https://website.com/manager-portal">Manager Portal</a>';
 }

else{
document.getElementById("link").innerHTML='<a href="https://website.com/regular-portal">Regular Portal</a>';
}

});
</script>

 I hope this helps.

 

Hi @IamGroot ,

Thank you for the suggestion!  I gave it a try while signed in as a manager and what is happening is the first line is showing "Regular Portal" with a hyperlink to "https://website.com/regular-portal".  It should be "Manager Portal" linked to "https://website.com/manager-portal" since I am signed in as a manager.  Also, the link is only showing up on the first line of results and nothing is showing up for the other lines.  Any ideas how to resolve that?

Thank you!

Link to comment
Share on other sites

Hi @Scott17

I have found another solution for this issue without using any JavaScript code. And this is the easiest one by using only the Standard features of Caspio.

 

Just follow this following steps: 

1. Create a Formula field to your User table.

image.thumb.png.2c7673f68073179fbc8e8970ebe646e2.png

2. Insert this code to the formula field:

Quote

CASE 

WHEN [@field:Users_tbl_Role] ='Manager' 

THEN 'https://website.com/manager-portal'

ELSE 'https://website.com/regular-portal'

END

3. In your DataPage, include the formula field in your "Result's Page" and select URL link in "Render value as". You can also change the label display of the link by checking the "Enable custom display".

image.png.fa63243c73fe7d9c7204d22370ff93c6.png

4. Hit "Finish" and test it.

 

I hope this helps :) 

Link to comment
Share on other sites

Hi @IamGroot

 

I see how that would work, I still have a problem though and this is my fault for not providing enough information in my original post (sorry about that!  I appreciate all your help!).   The table that is being used in the datapage is not the user's table that stores the user's role.  It is another table holding information on customers information (the hyperlink will go to different versions of the customer profile depending on if the user is a manager or not).

So right now, I have two data pages that essentially do the exact same thing except the hyperlinks that are inserted in HTML blocks are different.  For example:

<a href=" https://website.com/manager-portal/?cid=[@field:Client_ID]&fn=[@field:Client_1_First_Name]&ln=[@field:Client_1_Last_Name]">View Profile</a>

<a href=" https://website.com/regular-portal/?cid=[@field:Client_ID]&fn=[@field:Client_1_First_Name]&ln=[@field:Client_1_Last_Name]">View Profile</a>

Do you think there is a way to use Caspios standard features for this situation?  Or do you think there is some JS code that can still be used?  Thanks again for all your help!

Link to comment
Share on other sites

1 hour ago, Scott17 said:

Hi @IamGroot

 

I see how that would work, I still have a problem though and this is my fault for not providing enough information in my original post (sorry about that!  I appreciate all your help!).   The table that is being used in the datapage is not the user's table that stores the user's role.  It is another table holding information on customers information (the hyperlink will go to different versions of the customer profile depending on if the user is a manager or not).

So right now, I have two data pages that essentially do the exact same thing except the hyperlinks that are inserted in HTML blocks are different.  For example:

<a href=" https://website.com/manager-portal/?cid=[@field:Client_ID]&fn=[@field:Client_1_First_Name]&ln=[@field:Client_1_Last_Name]">View Profile</a>

<a href=" https://website.com/regular-portal/?cid=[@field:Client_ID]&fn=[@field:Client_1_First_Name]&ln=[@field:Client_1_Last_Name]">View Profile</a>

Do you think there is a way to use Caspios standard features for this situation?  Or do you think there is some JS code that can still be used?  Thanks again for all your help!

Hi @Scott17,

Your welcome ! I would love to help you, but first can you provide the steps in this process (for example: When user logs he needs to see this Data -> when he click the link in the result he needs to go to this DataPage -> the next DataPage should received the paramameter ..... and so on).

And for me to better understand the problem, Can I also ask for the DataPage URL or WebPage?

Thanks.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...