Question:
I’ve setup members pages to always go to the Sign Up page. However, for some pages, I would like to go directly to the Login Page. I’m okay with adding url?mode=login to the URL. I’ve added the following code snippet to the Sign Up to present the Login Page, but the login button doesn’t work and none of the url links work either. Any other ways to achieve this?
if (queryParams.mode) {
// Extract the 'mode' parameter from the URL
const mode = queryParams.mode;
console.log("mode =", mode);
if (mode === "login") {
const options = {
mode: 'login',
modal: true
};
authentication
.promptLogin(options)
.then(() => {
console.log("Member is logged in");
})
.catch((error) => {
console.error(error);
});
}
}
Clear all the previous code that you might have written for this and use the one I’ve provided below:
import wixLocationFrontend from 'wix-location-frontend';
import { authentication } from 'wix-members-frontend';
var query;
$w.onReady(function () {
query = wixLocationFrontend.query;
const options = {
mode: 'signup', // Sets the default mode to signup
modal: true
};
const isLoggedIn = authentication.loggedIn(); // Check if member is logged in
if (!isLoggedIn) { // Only if the member is NOT logged in, prompt them to log in
if (query.mode) { // Extract the 'mode' parameter from the URL
const mode = query.mode;
if (mode == "login") { // If mode is set to login, change the mode to login.
options.mode = "login";
}
}
authentication
.promptLogin(options)
.then(() => {
console.log("Member is logged in");
})
.catch((error) => {
console.error(error);
wixLocationFrontend.to(wixLocationFrontend.url); // Refresh the page in case of error while signing in
});
}
});
Place this code inside each page that you want the mode to be set to login.
Just make sure that this page is NOT set to be accessible by Members Only as the code is already taking care of that. Else it might not work as expected.
Yes, good idea. I didn’t consider making it a regular/non-member page and controlling login from the page itself. Thanks.