How to redirect from a 403 to login page/lightbox??

If a site member is logged out but tries to access their member page directly (say from a bookmark/history) it will display a 403 error page as they are only accessing the site as a visitor, not a logged in user

How would it be possible to redirect to another page instead of the useless 403?

For example, can I prompt the user to log in by displaying the login page / light box, or simply redirect the visitor back to the home page?

Fairly confident with client side code but believe this requires using the backend/routers which I am only just starting to learn about…

Please help!!! Many thanks in advance!

Hi Jamie,

If a visitor tried to access a members only page, it should be automatically be displayed with a login prompt.
Can you share your site please?

As a side note, you can use wix location API (wix-location-frontend - Velo API Reference - Wix.com) in order to redirect users to other pages (client side code only).

Thanks,
Idan.

Hi thanks for your response Idan,

The page(s) in question are members only dynamic pages, linked to a collection that has site member author permission to read

I am using a custom built Register lightbox and Login lightbox and my code uses wixUsers.login and wixUsers.register (rather than the wixUsers.promptLogin function).

I have tried this in the front end code:

$w.onReady(() => {

if (wixUsers.currentUser.role === “Visitor”) {
wixWindow.openLightbox(“Login”)
})

IF the dynamic collection read permissions AND the page permissions are both set to ‘anyone’, this code works fine.

IF the dynamic collection read permission is ‘anyone’ BUT the page permission is ‘members only’ it reroutes to the WIX login lightbox/page (before my onReady code is able to launch my custom login lightbox)

IF the dynamic collection read permissions AND the page permissions are both set to ‘members only’, it goes to 403 page (again, before my onReady code is able to launch my custom login lightbox).

Definitely need to use a router, correct?? I have been trying this code in the router.js (with the necessary imports) but have got nothing to work yet:

export function Client_router(request) {

if (wixRouter.wixRouterUser.role === “Visitor”)

return redirect(“https://www.google.com”)
}

also tried this:

export function Client_router(request) {

let user = request.user
if (user.role === “Visitor”)

return redirect(“https://www.google.com”)
}

Many thanks for any help!

Hey Jamie,

Regarding the second scenario you’ve described (login is prompt), this is the desired behavior. When you think about it, you wouldn’t want your code to run in case user is not a member (it might be for UX aspects, or because of security concerns).

Regarding the third scenario, this is how routers work. However, you can write custom code to change that.
Take a look at the beforeRouter hook: wix-router - Velo API Reference - Wix.com.

Good luck,
Idan.

Thank you again for the response. I have been trying to use the beforeRouter hook but I just can’t get it to work!

I have tried a few variations of the following code, but I can’t see the problem with what I have used? I expected this snippet to work, but it has no effect - visitors are still redirected to a 403 page, instead of my chosen redirect page (currently set as google.com for testing purposes)…

import {redirect} from ‘wix-router’;

export function Client_beforeRouter(request) {

if (request.user.role === “Visitor”)
return redirect(“https://www.google.com”)
}

I want to be able to give my Clients links to specific member pages - if they are not logged in I want to redirect to my custom login page instead of 403. If someone can help me fix/replace this code with something that works that will be amazing…

Thanks

Anyone able to advise on this?

Hi @info68 ,

Your code seems fine. Did you make sure the beforeRouter even runs?
It should be written in a routers.js file, in your backend folder. Also, make sure the prefix matches the name of your router (router name appears in the URL).

Good luck,
Idan.

Not opening login prompt on some pages but not others before showing a 403 error is inconsistent behavior. It’s also a poor user experience to get an error message instead of login prompt. I think it’s a bug and they should just fix it.

However, I found a workaround using a combination of a router and redirect page. I set up the router just as in Jamie’s code above, except it redirects them to a custom redirect page.

import { redirect } from 'wix-router';

export function collection_beforeRouter(request) {
    if (request.prefix === "/collection" && request.user.role === "Visitor")
       return redirect("/redirect?item=" + request.path[1]);
}

I created a blank static page called “redirect”. It’s set to members only so they need to login. After they log in, they are redirected to the dynamic page.

import wixLocation from "wix-location";

$w.onReady(function () {
 const query = wixLocation.query;
 
 if(query.item) {
        wixLocation.to(wixLocation.baseUrl + "/collection/" + query.item);
    } else {
        wixLocation.to(wixLocation.baseUrl + "/");
    }
});

I’m using baseUrl here because there is another bug that persists query parameters on subsequent pages. https://www.wix.com/corvid/forum/community-discussion/remove-query-w-o-replacing-url-using-wix-location-to-path