Create item in separate collection on Member signup

Hey All,
Need a little help!
I have a members area (wix.members.area) on my website. On sign up I would like to create a new list item in another collection and insert the Display Name (nickname) and associated ID into corresponding fields.
I believe this will fix the issue I’m have (and it seems many others) with the read write permissions. That being when there is no data in the collection associated with the logged in user you are unable to write anything to the text boxes.

Hi David :raised_hand_with_fingers_splayed:

Since you’re running sensitive code, you know it should run on the backend (server), and not on the frontend (visitors’ browsers), there are tons of articles and threads in this regard if you’re not familiar with it.

You need to shift the signup code to the backend, so once the user is signed up, get his ID and create an entry in your other database, here’s an example:

// Backend (Server): members.jsw
import members from 'wix-members-backend';
import { insert } from 'wix-data';

export async function signup({ email: '', password: '', firstName, lastName }) {
    const result = await members.authentication.register(
        email,
        password,
        {
            contactInfo: { firstName, lastName },
            privacyStatus: 'PRIVATE'
        }
    );
    
    console.log(result.member); // Should print:
    /*
        {
            _id: 'system_id',
            loginEmail: 'email@domain.com',
            ....
        }
    */
    
    // You can now create an entry based on the member's ID and nickname
    const member_data = {
        _id: result.member._id,
        nickname: result.member.nickname,
        property: value,
        // ...
    }
    
    await insert('col', member_data, { suppressAuth: true });
    
    return Promise.resolve({
        ok: true;
        sessionToken: result.member.sessionToken,
        approvalToken: result.member.approvalToken
    });
}

Now on your page.

import { signup } from 'backend/members.jsw';
import { authentication } from 'wix-members';
import { to } from 'wix-location';

$w('#signupBtn').onClick(() => {
    const email = $w('#email').value;
    const password = $w('#password').value;
    const firstName = $w('#firstName').value;
    const lastName = $w('#lastName').value;
    
    return signup({ email, password, firstName, lastName }).then(async (result) => {
        if (result.ok) {
            // Log the user in:            
            await authentication.applySessionToken(result.sessionToken);
            
            // Redirect the user to the home page.
            to('/');
        }
    })
})

Hope this helps~!
Ahmad

Thanks so much for your help Ahmad.
I’m familiar with server modules and have tried the code don’t it quite suits my application. I don’t think I’m explaining myself properly. Let me give it another go.

SCENARIO:
I have a collection called “additionalProfile”. This collection will be use in a public (visitor) facing table.
To access the form to complete each field in the “additionalProfile” collection, each person will need to sign up as a member to my site. To do this I have set up the native wix member area (PrivateMembersData). Once a member they will need to navigate to a custom private page (“Additional Profile”), setup in the members area. Here I would like them fill out and the form which will submit to the “additionalProfile” collection.

PROBLEM:
Naturally I would only like the current member to add and edit their the item that is associated with their userID (when creating a new item). Additionally I would like the form to be READ/WRITE. The issue is the member can’t begin the form because there is no item in the collection associated with their userID. Hence all the disabled input fields in READ/WRITE mode.

SOLUTION:
I believe the solution would be:
On sign up, using the native wix member login. A function is called to create a new item in the “additionalProfile” collection. The new userID and .nickname is copied across into the item. This would now give a user reference in the collection and enable all the input fields to complete the form.

Unfortunately this extends beyond my coding skills but I really appreciate your input.
I’m not even sure it’s possible but it looks like your on the right track.

P.S. I did also have the idea of starting the “additionalProfile” dataset as WRITE and have some code check the the collection onLoad for an item with the some userID as the logged in user. Then change the dataset to READ/WRITE if true.
Though I suspect this would reduce load speed when the collection gets bigger.

@davidlist There’s no way of running any code if you’re using the native signup box, you need to create your own login/signup page, take our website’s login page as an example, and since you’re familiar with the backend, you should NOT use the datasets as they’re not as secure as running your code on the backend, datasets are frontend applications after all.

You can follow my example if you just want to create a blank entry for the user in the database, and you’ll still be able to use the datasets, or even better, you can go all backend, and have a function that retrieves users’ entries, and update them if necessary, this is the pro way to do it.

@ahmadnasriya thanks so much for your help. Though I found a way around it, more of a hack really.

I was able to create verification or T&Cs agreement members page that writes a value to the collection “agreed” string. Once the item is created the submit button is disabled if the “_owner” field matches the current logged in user.
Now all fields for the available to be used.
Always more than one way to skin a cat :scream_cat:

@davidlist great to hear that you found a way around :wink: