How to redirect a site member to their own members profile page

I’ve seen this question come up a couple of times on the forum, with no clear answer given. This code applies to the Wix members area and profiles, but you could modify it for a customized members area as well. (to add the wix members area please see this support article)

For a while I’ve been wanting to structure the social area on our website in a better way, and a menu link to the profile page of the current user would definitely be handy for that. (The alternative, the members menu in the members area doesn’t always seem to function, sometimes I hide a link from the members menu and it still displays) So now I’ve written code to redirect a user to their own profile. In this example we create a router page called www.examplewebsite.com/myprofile. I hope it is helpful!

You need to create a ‘myprofile’ router page in the editor (from the … symbol at ‘pages’ in the sidebar) for this to work. After that, we need some code in the backend. Go the backend on the same site menu and create two files: routers.js and members.jsw

This is the code example using routers.js. It redirects the user either to their profile, or, if they are not logged in, to a page with the address /welcome

import { findProfileURL } from 'backend/members'
import { redirect } from "wix-router";
export function myprofile_Router(request) {
console.log("router active")
let pages = request.pages;
let userId = request.user.id;
if (userId === null || userId === undefined) {
return redirect("/welcome");
}
return findProfileURL(userId)
.then((url) => {
return redirect(url);
})
}
export function myprofile_SiteMap(request) {
return []
}

and then we create the function findprofileURL in members.jsw

import wixData from 'wix-data';
let options = {
"suppressAuth": true,
"suppressHooks": false
};

export function findProfileURL(ID) {
var url = "/profile/"
return findMemberById(ID).then((user)=>{
let slug = user.slug
url = url + slug
//  console.log(url)
return url
})
.catch((err) => {
return "/welcome"
})
}

export function findMemberById(ID) {
return wixData.query("Members/PrivateMembersData").eq("_id", ID).find(options).then((filteredmembers) => {
let user = filteredmembers.items[0]
return user
})
.catch((err) => {
let em = "could not search members database by Id." + err
console.log(em)
})
}

If you use the Wix Members Profile tutorial as shown here and have move control and add additional things to what the Wix Members app own profile page allows you.
https://support.wix.com/en/article/corvid-tutorial-building-your-own-members-area

Then you can easily move the site member onto their own profile page as shown in the tutorial.

export function profileButton_click(event) {   wixLocation.to(`/Members/${wixUsers.currentUser.id}`);
}

The same within a site that I have got with a link to the Site members own My Account page from the websites Members Area section.

export function myaccountbutton_onclick(event) {
wixLocation.to(`/account/my-account`);
}

The same with the profile page too, where you can simply set that up to be the same as shown in the page settings.
https://www.yourwebsite/profile/{username}/profile

Finally, the Wix Members My Account page shows each site member their own Community URL that ALL site members can see when they are logged into the site.

Quote:

The same with the profile page too, where you can simply set that up to be the same as shown in the page settings.
https://www.yourwebsite/profile/{username}/profile

Hi GOS, I found that to get to the profile page of the wix members app, adding in a username didn’t work. Instead of the username the profile page URL is composed of the first bit (/profile/), and then the slug. The slug can be found in the PrivateMembersData database.

For example, I have a test account that I registered with an email address. The slug is hello21994 and the profile page URL is https://www.lemurianstarchildoracle.com/profile/hello21994/profile

If I look in the membersdatabase the slug is sometimes the first part of the email address, but other times a number of five digits is added to that.