Ok, to keep the whole thing simple.
FRONT-END-CODE:
import { authentication } from 'wix-members-frontend';
import { get_CurrentMember, get_ContactData } from 'backend/vnLoginMGM';
$w.onReady(async()=> {
//----------------------
const currentMember = await check_LoginState();
console.log('Current-Member: ', currentMember);
const loginEmail = currentMember.loginEmail;
console.log('Login-Email: ', loginEmail);
const contactID = currentMember.contactId;
console.log('Contact-ID: ', contactID);
//----------------------
const contactData = await get_ContactData(contactID);
console.log('Contact-Data: ', contactData);
//----------------------
});
//-------------------- [ LOGIN-CHECK ] -----------------------
async function check_LoginState() {console.log('-----CHECK-----');
const isLoggedIn = authentication.loggedIn();
if(isLoggedIn) {
let currentMember = await get_CurrentMember({fieldsets:['FULL']});
if(currentMember) {return currentMember;} else {return undefined}
} else { }
}
.
.
.
BACKEND-CODE:
import { contacts } from 'wix-crm-backend';
import { currentMember } from 'wix-members-backend';
export function get_ContactData(contactID) {
return contacts.getContact(contactID, {suppressAuth:true})
.then((contact)=> {return contact})
.catch((error) => {return error;});
}
So the whole code will return you something like…
And do not use the V.2-Version —> seems to be buggy !!!
----> //import { contacts } from ‘wix-crm.v2’; <----- NOT GOOD !!!
So now you have a basic simple setup, which checks right from loading on your current page, if a user is logged-in or not.
If logged-in → get MEMBERS-DATA → GET CONTACT-ID → GET-CONTACT-DATA
If not → Do WHAT YOU WANT !
So, you just were able to provide the CONTACT-ID from FRONTEND to send it to BACKEND → to get CONTACT-INFO back to FRONTEND.
What exactly you want to provide on FRONTEND → it’s on your choice.
In this example i just sent the whole CONTACT-DATA back to frontend.
And i think your question about of how to use the values on FRONTEND is already replied with this example.
Oh!!! And do not forget…
→ my BACKEND-MODULE has a different name → ‘backend/vnLoginMGM’;
EDIT: Forgot that in my version everything is running on backend…so the whole BACKEND-setup one more time…
import { contacts } from 'wix-crm-backend';
import { currentMember } from 'wix-members-backend';
export function get_CurrentMember(options) {
return currentMember.getMember(options)
.then((member) => {return member;})
.catch((error)=> {console.error(error);});
}
export function get_ContactData(contactID) {
return contacts.getContact(contactID, {suppressAuth:true})
.then((contact)=> {return contact})
.catch((error) => {return error;});
}
And always keep your code and it’s structure clean, then you will understand your own setup better (including COSOLE-LOGS).
In your previous codes → you have used …CONTACTS from WIX-CRM.V2
I did the same → but i was not able to get —> suppressAuth to work.
It only worked for ADMIN, not for ordinary user.
Switching back to the old (current API version) everything worked perfectly, for ADMIN and for ORDINARY-USER.
And additionaly INFO: Of course there would be even a third option you could use, if those first 2 ones would not work. You could simply use the old and depricated Wix-Users-API (but surely not recommended at all anymore)…
// Get USER-DETAILS....
async function get_UserDetails(userID) {
return wixUsersBackend.getUser(userID).then((user)=> {
return (user);}).catch((error)=> {console.log(error);})
;}