Login Button Redirect

Hi everyone.

I created a Login button using the suggested Wix code (see below).

The button works perfectly on these two required features:

  • redirect to the ‘home page’ after logout
  • redirect to the ‘services’ page after login

What I would like the button to do is to redirect to a third page (called ‘profile’) if the user is new and chooses to sign up instead of log in.

Any ideas please?

LOGIN BUTTON CODE
export function loginButton_click() {
// if a user is already logged in
if (wixUsers.currentUser.loggedIn) {
// go to home page
wixLocation.to(“/”);
// log the user out
wixUsers.logout()
.then( () => {
// and update buttons accordingly
setButtons( true );
} );
}
// if a user is already logged out
else {
let userId;
let userEmail;

// then prompt the user to log in
wixUsers.promptLogin( {“mode”: “login”} )
.then( (user) => {
userId = user.id;
return user.getEmail();
} )
.then( (email) => {
// check if there is an item for the user in the collection
userEmail = email;
return wixData.query(“Users”)
.eq(“_id”, userId)
.find();
} )
.then( (results) => {
// if an item for the user is not found
if (results.items.length === 0) {
// create an item
const toInsert = {
“_id”: userId,
“email”: userEmail
};
// add the item to the collection
wixData.insert(“Users”, toInsert)
. catch ( (err) => {
console.log(err);
} );
}
else
// update buttons accordingly
setButtons( true );
wixLocation.to(“/services”);
} )
. catch ( (err) => {
console.log(err);
} );
}
}

Oberdan

The button is a login button not a sign up button. Why not have a sign up button that uses the wixUsers.promptLogin( ) in sign up mode like so:

wixUsers.promptLogin( {"mode": "signup"} )
.then((userRecord) => {
    wixLocation.to('/profile');
});

Alternatively you can create your own signup using the wixUser.register() function on your profile page.

Steve

Thank you Steve. I have a signup button like that on the main body of the page, but have run out of space on the nav bar.
Given that on the login page users can switch to Signup, I was hoping there may be a way to write an if/else script that would redirect the user depending on the action they completed: login or signup.

Hi Oberdan:

Without a link to your page and some context it is hard to understand what effect you are trying to implement. Basically either the button is a login button or it is a register button. The only way to automate registration is if the login attempt fails. So you would need to catch a login failure and register instead you would need to add a catch() that catches a login failure, tests the reason (you need a invalid user or password error). Then display a password element and change the name of the button to Register and use the button to register. If you do this then you can test for the button name in the loginButton_click() code and then perform the related login or register function followed by the redirecting to the pages you need to.

Steve

Hello Steve. Thank you for the comments. Here is the additional information.

FULLER CONTEXT
I want members to only book services after they complete the Member Profile (where they must accept the T&Cs to continue).
When existing members login, the site sends them straight to the services page, where they can book.
It is different for new members who wish to sign up.
I have Signup buttons on the main body of the page, which are set up as “mode: signup”, so the redirection is as I need it to be. However, the Login/Signup button in the nav bar only works when completing a login. If a user switches to Signup mid-process it won’t do what I need it to do.

CURRENT URL

I hope this clarifies the need

Hi Oberdan

OK then the solution you probably want to implement is your own Login and Sign up page.

If you use the login and register functions instead of the promptLogin function.

This way you can build a lightbox that loads a page similar to the normal wix one. You can call login() if they are registered or you can call register(). Make sure you have the settings set to the way you need them to be.

If you need more help let me know.

Steve

Hi Steve,

Again thank you for the input.

Over the last few days I have tried your suggested solutions. They address the original need, but I then stumble upon other sets of challenges, which I was unable to address. For example:

  • Adding a “Confirm Password” field on the customised Signup page
  • Not being able to display error messages like “Wrong email or password” or “Email address already registered”
  • Not knowing how to use the customised Signup page to insert new users into my database instead of the universal CRM

For now I am going to stick with the existing solution and revisit it if it creates problems.

All the best, Oberdan

Hi Oberdan:

I suggest you explore the various Wix tutorials, articles and examples and get familiar with the platform.

For example you can perform input validation and display error messages in a variety of ways. Custome validation is one way andit is explained at this link.
https://support.wix.com/en/wix-code/user-input
Adding additional forms for password entry and validation check these links:

CReating a new member record and adding the user information into a local data collection check this link out:

Cheers

1 Like

For oberan where should I put the code?

1 Like

Hi Antonio, I solved this issue by first checking if the logged in user met a specific condition. You can find the code in my other post here https://www.wix.com/code/home/forum/community-discussion/redirect-to-other-url-if-boolean-value-in-collection-false-resolved

1 Like

I want to allow members to log in on page A and after successfully logging in, be automatically redirected to page B. If they’re already logged, in they would go directly to page B on their browser. Just starting out and I need the code for this…in it’s entirety if possible. Thanks in advance for any help you can provide!

You would be much better off posting this forum question as a new post in the forum and not just tagging on to the end of a post that is over a year old now.

You can use Wix Users API to check if the current user is logged in and from that result either have the user login and then moved onto another page, or just simply have the member moved to another page if they are already logged in.
https://www.wix.com/corvid/reference/wix-users.html

Please make a new post in this forum if you require any more help with this.