Creating automatic redirect for logged in members

I have two pages, the home page and dashboard page. If a user has signed into my site, I want them to be automatically redirected to the dashboard page from the home page. If the user is NOT signed in, then they should stay on the home page. how do I create this?

Hi Liam,

You can do that by writing a code on the home page that will check if the user is logged in or not, and if it is, redirect him to the dashboard page.

Here’s how:

import { currentUser } from 'wix-users';
import { to } from 'wix-location';

$w.onReady(() => {
    // Check to see if the current user is logged in.
    if (currentUser.loggedIn) {
        // Redirect the user to the dashboard
        to('/dashboard');
    }
})

Hope this helps~!
Ahamd

Thanks Ahamd!! It works well, however it only redirects to the Dashboard page once the home page has finished loading. Is there a way to make it redirect to the Dashboard before it has to finish loading the entire home page as this takes long for it to redirect to the dashboard page.

An example of what I want to achieve is like the Netflix page. If you are logged into Netflix and then go to Netflix.com , it automatically takes you to the movies collection page and it works fast.

I notice Wix code (Velo) takes very long to load.

Thanks again!

@liam10 Yup, it’s slow(known issue, not bug) :frowning:

That’s because the script of the page is loaded after the page is ready, that’s why it takes longer, instead, you can use a router page to check the current user, and redirect them immediately, before any page is loaded.

@ahmadnasriya I wrote the code and did all the hard work and the easiest bit, Velo doesn’t allow me to do. There is no way to set the Router as the home page. Wix is good for front end but I wouldn’t touch Velo with a ten foot pole to build apps.

I used wix-router as suggested by Ahmad and it works faster

my routers.js

import {redirect} from "wix-router"; 
import wixUsersBackend from 'wix-users-backend';

export function route_Router(request) {
    if (wixUsersBackend.currentUser.loggedIn)
        return redirect("../page1");
    return redirect("../page2");
}

Hey Helen, how did you get this applied to the home page? I setup the router code but it only lets me use it on a prefix under the main domain, not the home page. That doesn’t help when I’m trying to direct member traffic from the homepage to a similar dashboard page.

thx.