Redirect to member only page after member login

Hi there, I see this has been asked before, but I can’t get any of the solutions to work from the community answers I’ve found. Can someone help please?

I’m trying to re-direct members to a specific page after they log in, but the code I’ve got does not work. I added it to the Master page. Code is below that I added to the masterpage.js, but it gives the following error message and does not work:

Error message - [/pages/Home.d7vax]:
Error count: 1
error: Parsing error: Unexpected token full (1:5)
[public/pages/masterPage.js]:
Error count: 1
error: Parsing error: ‘import’ and ‘export’ may only appear at the top level (4:1)

My code I added is below:

$w . onReady ( function () {

import wixUsers from ‘wix-users’ ;

import wixLocation from ‘wix-location’ ;

wixUsers . onLogin (( user ) => {

wixLocation . to ( “https://www.sybaritesporting.com/” );

});

Thanks

The error is pretty self explanatory, your import statements are within a callback function, they should only appear at the top level of the code, move the imports to the. Top of the page and it should be ok

Before you continue…

  1. Get current user email and add to collection - #15 by CODE-NINJA
  2. Get current user email and add to collection - #15 by CODE-NINJA
  3. I am trying to show only the values in the dataset that have the same email field as the user's email, so it only shows their trip.
  4. Log in check

What do you think is the wanted WORD in all of these POSTS ?

Could it be → DEPRICATED !!! ?

Let’s check the Wix/VELO-APIs ---->

Did not found → Wix-Users-API listed …

Yes, still existing for those who knows the API, or for those who read old posts and discover the old Wix-User-API.

Thanks for your help with this. I changed code as per your suggestion to below:

import wixUsers from ‘wix-users’ ;
import wixLocation from ‘wix-location’ ;

$w . onReady ( function () {

wixUsers . onLogin (( user ) => {
wixLocation . to ( “https://www.sybaritesporting.com/” );

});

But… unfortunately still not working, I get the following error, any help would be really appreciated, thanks:


deploymentId: 34d68e9c-e91a-4306-b58e-685513ea35b0
23/01/2025, 09:57:50
Status: Error

[masterPage.js]: public/pages/masterPage.js: Unexpected character ‘‘’. (3:21)
1 | // The code in this file will load on every page of your site
2 |

3 | import wixUsers from ‘wix-users’ ;
| ^
4 | import wixLocation from ‘wix-location’ ;
5 |
6 | $w . onReady ( function () {
[/pages/Home.d7vax.js]: public/pages/d7vax.js: Missing semicolon. (1:3)
1 | For full API documentation, including code examples, visit Velo Docs
| ^
2 |
3 | $w.onReady(function () {
4 | //TODO: write your page related code here…

Sorry, here is the rest of the code/error I get:

[/pages/Home.d7vax]:
Error count: 1
error: Parsing error: Unexpected token full (1:5)

[public/pages/masterPage.js]:
Error count: 1
error: Parsing error: Unexpected character ‘‘’ (3:22)

It seems you’re using a special ’ character

Thanks again, but I’m afraid that is alien to me…I struggle with code. Can you be so kind as to provide what the change should look like to the code to get it working? I’d be very appreciative.

That’s totally fine. All of us were alien to code at some point! (:

Here’s the correct code. Simply delete any previous code inside your masterPage.js file and paste this:

import wixLocationFrontend from 'wix-location-frontend';
import { authentication } from 'wix-members-frontend';

authentication.onLogin(async (member) => {
    wixLocationFrontend.to('https://www.sybaritesporting.com/');
});

Just to let you know what the users above suggested:

  • wix-users is an old API and is now depricated and replaced by the new wix-members-frontend, which we have used here.
  • import statements should always be at the beginning of your code, as you’d want to first import all the relevant libraries / APIs that are necessary for the code to run before actually calling them through the code. Else it will result in an error like the one you faced previously.

Looking at the error logs you’ve provided, it does seem that your homepage code might have some errors in it too, so make sure to check and rectify those out.

Cheers!

1 Like

Thank you for this help. I’m very, very grateful and it worked perfectly. You’ve made my day!

1 Like