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)
})
}