I copied some login code from some other platform and the following features are missing on the code and I can not do it myself. After SignUp and Login I want users to be directed to a page with name 'Dashboard' and when they Logout I want them to be redirected to my Home page. The following is my code: Help...
#
// For full API documentation, including code examples, visit http://wix.to/94BuAAs
import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
$w.onReady( () => {
if(wixUsers.currentUser.loggedIn) {
$w("#loginButton").label = "Logout";
//$w("#profileButton").show();
}
else {
$w("#loginButton").label = "My Account";
//$w("#profileButton").hide();
}
} );
export function loginreg_click(event) {
//Add your code for this event here:
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w("#loginButton").label = "My Account";
//$w("#profileButton").hide();
} );
}
// user is logged out
else {
let userId;
let userEmail;
// 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("Members")
.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("Members", toInsert)
.catch( (err) => {
console.log(err);
} );
}
// update buttons accordingly
$w("#loginButton").label = "Logout";
wixLocation.to.to('/Dashboard');
//$w("#profileButton").show();
} )
.catch( (err) => {
console.log(err);
} );
}
}
When you mention about logging in, logging out and signup, well the code you have used in your post is from the tutorial about making your own custom members profile area.
https://support.wix.com/en/article/corvid-tutorial-building-your-own-members-area
Have a read of this tutorial as this is not for making your own custom login and signup lightboxes and neither does it feature a custom logout function either.
It is simply to create a members profile page which being dynamic is unique to that user and only they themselves can access it and edit or alter anything on their update page too.
This profile page and update profile page can be anything you want it to be, it doesn't have to be kept as a member profile page, it can be your member dashboard page etc, or whatever you want to put on that page yourself, the choice is yours and you are not restricted to what Wix have put on the tutorial itself.
Plus Nayeli (Code Queen) already has an youtube video which takes you through this tutorial, along with her own website page about it that you can check out yourself.
https://www.youtube.com/watch?v=Orl8GJNzG5s - from 2017
https://www.youtube.com/watch?v=yLCOqsVHhD0 - updated version from 2018
https://codequeen.wixsite.com/membership-dashboard
As for logging out the user and redirecting to another page, then you can simply add a logout button as Steve has suggested in this great previous post here.
https://www.wix.com/corvid/forum/community-discussion/this-is-how-to-logout-and-go-to-the-page-of-your-choice
As for moving a user onto another page after they have logged themselves in or signed themselves up, well this is done in code through the Wix Location API and the .to() function from it, see here for an example of the API reference and it's functions, you will also notice it in the code that you have used too.
https://www.wix.com/corvid/reference/wix-location.html#to
As for signup and logins, you can either use Wix own window or the custom signup through Wix Forms options, or you can create your own custom login and signup lightboxes.
https://support.wix.com/en/article/about-the-member-signup-form
https://support.wix.com/en/article/editing-your-member-signup-settings-for-the-default-form
https://support.wix.com/en/article/creating-a-custom-signup-form-for-your-members-area
If you do the custom lightbox method, then you need to change your member signup settings to suit your own custom lightboxes.
https://support.wix.com/en/article/about-the-member-signup-form#corvid-form