SOLVED - 'Login redirect'

Guys this is driving me nuts, losing the will to live with this.

People were trying to help me but due to the spam attack my profile and posts were cleared.

ISSUE:

I have two member role set up, “Client” and “Professional”

When a member logs in Iwant them to be redirected to a different member dash/profile page depending on their role.

I’m using the standard login menu/button and tried the following code in both the page and site panel.

PLEASE NOTE: I’m totally new to coding, second week in, so complete novice.

Code I am using is as follows:

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
import wixData from 'wix-data'; 

wixUsers.onLogin( (user) => {

let userId = user.id;
let isLoggedIn = user.loggedIn;
let userRole = user.role;

if(userRole ===  "Client") {wixLocation.to("/client-dash");}    

if(userRole ===  "Professional") {wixLocation.to("/candidate-dash");} 

  });

@givemeawhisky I know you tried helping me with this the other day but due to the spam attack I had all my profile wiped. Just reaching out coz genuinely struggling on this one and can’t get the code right

You have an extra }); in the end of your code. Other than that, it looks fine.

Hi @jonatandor35 absolutely banging my head against the wall on this one. The code just isn’t working?? Losing will to live with it.

@thomas80559 so first of all remove these extra parentheses. Then make sure you’re using the right page paths. If it still doesn’t work, try to console.log(userRole) and see what it logs.

Hey @jonatandor35 done all the above. Nothing. If I don’t give the code the option of where to direct depending on the users role i.e just add

{wixLocation.to("/client-dash");}   

It works by redirecting you to that page. As soon as you code in the option

 if (userRole === "Client") { wixLocation.to("/client-dash");}
 if (userRole === "Professional") { wixLocation.to("/candidate-dash");} 

It doesn’t want to play ball.

Anyone else have any ideas on this??

@thomas80559 what about the console.log(userRole); ?

@jonatandor35 I sound like a total idiot but not sure what you mean by console.log complete novice so things like this are new to me

@thomas80559 after this line:

let userRole = user.role;

Add this line:

console.log(userRole);

Then publish and on the published page, click F12, and select the console tab.
It’s supposed to write there the current userRole.
Let’s see what it will write.

Hi @jonatandor35 when I login, it displays Member in the console log, followed by console.js.53

@thomas80559 so now we can understand what’s going on. The role is Member. Not Client or Professional

@jonatandor35 so when I set up the new role ’client’ and ‘professional’ in the member area of my six dashboard do I need to do anything else?

it recognises the role when I set up page permissions. I.e if your role is ’client’ you can’t access ‘professional‘ pages if that makes sense?

Try:

wixUsers.onLogin( (user) => {
    user.getRoles()
    .then( (roles) => {
    if (roles.some(r => { r.name === "Client"})){wixLocation.to("/page1");}
    if (roles.some(r => { r.name === "Professional"})){wixLocation.to("/page2");}
    })
})

[UPDATED]

Pay attention - I’ve updated the code above.

1 Like

@jonatandor35 Unable to test it right now but I’ll let you know ASAP

@thomas80559 - No need for me to help as looks like J.D. has already done an excellent job helping you with your issue.

However, one little thing I will mention that I am not sure that you have done is to do with the Member roles themselves in your Contacts list as you say that it still says Member when you read from the console log.

You state that you have setup the Member roles of Client and Professional and setup the page permissions etc.
https://support.wix.com/en/article/managing-your-member-roles
https://support.wix.com/en/article/changing-the-permissions-of-a-member-role

However, have you actually changed any of your members to those roles themselves, as otherwise they will all still be known as Member and not any other role.
https://support.wix.com/en/article/creating-member-roles-6943237

Easy way to check is to simply go into your Wix Dashboard, click on Customer Management and then Site Members.

If your Site Members have nothing listed under their Role tab, then they will be known as Member still, however, if you have added a Member to a specific role, then they will have that role listed in their Role field.

1 Like

Hi @jonatandor35 I tried this code and got a little diflated when it didn’t work, but just spotted n error on line 9 that flags up parsing error: Unexpected token {

Tried to fix it but not sure where it’s incorrect.

@givemeawhisky thanks for this info. I did double check this and I have assigned certain members a role for testing purpose and I can see these displayed in the member table under role tab. Not sure as to why this isn;t being fed through or picked up in code???

J.D has been a genuine help on this. If his code doesn;t work I think I’m going to have to give up coz it’s just not happening which is a real shame coz it should be pretty simple I’d have though.

Please post the editor URL of your site. Only authorized Wix personnel can get access to your site in the editor. Please include the name of the page involved.

1 Like