Assign a role automatically to a memeber at registration

Hello everybody

I developed a little website for the municipal campsite of my village. Everything works. Until now, clients could not create their member account. They could book without registering. From now on this will no longer be the case, so I created a registration form, which works very well. My problem is that the client does not have an automatically assigned role. And depending on the role assigned to them, they have access to certain pages of the site and not others.
How do I assign them a role directly upon registration form (via role ID?)
I created several roles (I have their IDs). I would like, when they register, that they recover their role automatically).

i send you below the attachments. (It would be easier to answer maybe :slight_smile:

Thank you in advance for your help. I hope you can help me.

Have a nice day everyone

Product:
WIX EDITOR

What are you trying to achieve:
Assign a role automatically by code.

here the second page of the code.

Oups, i forgot this in the attachment

Below my import declarationin in the top level of my module :slight_smile:

import { authentication } from ‘wix-members-frontend’;
import wixLocation from ‘wix-location’;
import {session} from ‘wix-storage’;
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;

let emails = ;

import { Permissions, webMethod } from "wix-web-module";
import { badges } from "wix-members-backend";

export const myAssignMembersFunction = webMethod(Permissions.Anyone, () => {
  const badgeId = "3fcaacc0-a3a7-464f-9ba9-f211bdcec9fc";
  const memberIds = [
    "efab296e-2687-4751-9956-ee73200dd4bb",
    "3403e13b-8826-4af6-aa19-18784bb84a8e",
    "28d35f86-6694-4455-9dff-aff5d450b482",
  ];

  return badges
    .assignMembers(badgeId, memberIds)
    .then((assignedMembers) => {
      return assignedMembers;
    })
    .catch((error) => {
      console.error(error);
    });
});

/* Promise resolves to:
 * {
 *   "memberIds": [
 *     "efab296e-2687-4751-9956-ee73200dd4bb",
 *     "3403e13b-8826-4af6-aa19-18784bb84a8e",
 *     "28d35f86-6694-4455-9dff-aff5d450b482"
 *   ]
 * }
 */

Here you can see an example function to assign a badge for a member or for several members at once, depending on how much members (member-IDs) you put into the ARRAY.

Paste this function onto your BACKEND and send YOUR WISHED → ARRAY-DATA …

memberIds = [
    "efab296e-2687-4751-9956-ee73200dd4bb",
    "3403e13b-8826-4af6-aa19-18784bb84a8e",
    "28d35f86-6694-4455-9dff-aff5d450b482",
  ];

…from your FRONTEND to your backend, starting the ‘myAssignMembersFunction()’ on your backend like…

myAssignMembersFunction(memberIds) ;

…or… like …

myAssignMembersFunction([
    "efab296e-2687-4751-9956-ee73200dd4bb",
    "3403e13b-8826-4af6-aa19-18784bb84a8e",
    "28d35f86-6694-4455-9dff-aff5d450b482",
  ]);

All the IDs which you will have sent to the backend, will be assigned to a specific badge.

In this example it will be …
const badgeId = "3fcaacc0-a3a7-464f-9ba9-f211bdcec9fc";

Which is a fixed (already integrated) BADGE-ID within the export-function on your backend. But since you will want to send the specific BADEG-ID also from your frontend to your backend, you will need to edit/optimmize your backend-function, which will then recieve the BADGE-ID also from your frontend.

So your optimized function will look like…

export const myAssignMembersFunction = webMethod(Permissions.Anyone, (badgeId, memberIds) => {  
  return badges
    .assignMembers(badgeId, memberIds)
    .then((assignedMembers) => {
      return assignedMembers;
    }).catch((error) => {console.error(error);});
});

So in this case you can send your wished BADGE-ID and your wished MEMBER-IDs to your backend, to assign the choosen badge to your choosen members, calling it like…

myAssignMembersFunction(badgeId, memberIds)

BY THE WAY: Try to use the integrated option of CODE-BLOCKS instead of using pictures of your CODE.

2 Likes

Hi CODE-NINJA

Thank you very much. It’s ok now … :grinning: :ok_hand:
A new role is now assigned to each of my clients who register on the site.
Cool …
Ok, il will now use the code blocks (I don’t know how to do it, but il will find :wink:
Thanks a lot.

1 Like

As you can see, i presented my CODE always as CODE-BLOCK, which will show you a well organized and structured and even highlighted/colored code.

2024-10-09 22_04_58-Window

Performated text <-----> CODE-BLOCK

1 Like

Ah ok; thank you for the information :grinning: