Skip to content
Back

[SOLVED] Add a default team for new User and adding verified role for verified email account

  • 0
  • Teams
  • Functions
  • Web
loup
16 Jun, 2023, 15:33

My goal is to create a function which give a default team for new user call 'user' Team. And when the user verify his email, another function gave him the role 'verified'

TL;DR
Solution: To add a default team for new users and assign a "verified" role to users with verified email accounts, you need to create two functions: one for user creation and one for user verification. When creating the functions, make sure to include the necessary variables that can be sent to the function. To access Appwrite, you can do it either server-side or client-side. In this case, you'll be using the server-side, which has more capabilities and no rate-limit on the number of requests. To do this, you need to create an API key with function permissions, specifically the "Auth" permission for accessing
Binyamin
16 Jun, 2023, 15:35

You'll need to create two functions.

  1. On user create
  2. On user verified

If you look here https://appwrite.io/docs/functions#functionVariables You can see the list of all the variables that can be sent to your function.

Additionally, you can add custom variables like APPWRITE_FUNCTION_PROJECT_ID for example in your function Settings page.

Binyamin
16 Jun, 2023, 15:36
loup
16 Jun, 2023, 15:39

What is the function api key ?

Binyamin
16 Jun, 2023, 15:41

When accessing Appwrite You can do it in one of two ways:

In function you'll usaully use the Server side. The server side has much more capabilities and no rate-limit on number of requests.

But, for that you'll need to create an Api key in the project overview page. https://appwrite.io/docs/keys

loup
16 Jun, 2023, 15:44

Okay I understand the benefit of using server side against client side. So I have to create an API KEY with Function permissions right ? Like this ?

Binyamin
16 Jun, 2023, 15:45

In your case you'll need the Auth one. As you want the function to have access to some Auth functions.

loup
16 Jun, 2023, 15:46

Okay well only Auth ?

Binyamin
16 Jun, 2023, 15:46

Seems like it, yes.

loup
16 Jun, 2023, 15:48

Probably a stupid question but there is a way to try locally the function ? I guess I dont have to deploy the function each time right ?

Binyamin
16 Jun, 2023, 15:49

For now, there's no direct way. You can upvote this one https://github.com/appwrite/appwrite/issues/5425

Binyamin
16 Jun, 2023, 15:49

What I'm doing is creating a file named local-server and run it each time with mocked variables, etc.

loup
16 Jun, 2023, 15:53

Okay np but I guess my function will be very simple so

loup
16 Jun, 2023, 16:03

Well something like that should work (for the function who add the default Team for new user) ?

TypeScript
module.exports = async function (req, res) {
  const client = new sdk.Client();
  const teams = new sdk.Teams(client);

  if (
    !req.variables['APPWRITE_FUNCTION_ENDPOINT'] ||
    !req.variables['APPWRITE_FUNCTION_API_KEY']
  ) {
    console.warn("Environment variables are not set. Function cannot use Appwrite SDK.");
  } else {
    client
      .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
      .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
      .setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
      .setSelfSigned(true);
  }

  const teamId = 'APPWRITE_FUNCTION_TEAM_ID';
  const user = JSON.parse(req.variables["APPWRITE_FUNCTION_EVENT_DATA"]);

  try {
    await teams.createTeamMember(teamId, user.$id, ['MEMBER']);
    res.json({ success: `User ${userId} added to the team successfully` });
  } catch (error) {
    console.error('Error adding user to the team:', error);
    res.json({ error: 'Error adding user to the team' });
  }
};
loup
16 Jun, 2023, 16:04

But do I have to add the MEMBER rôle ? Or I can leave blank ?

Binyamin
16 Jun, 2023, 16:06

I think the function name is createMembership instead of createTeamMember, like so:

TypeScript
const r = await teams.createMembership(teamID, email, [], 'url');
Binyamin
16 Jun, 2023, 16:06

You can leave it empty

loup
16 Jun, 2023, 16:12

If I got that :

TypeScript
await teams.createMembership(teamId, user.$id, [], 'url');

How Appwrite gonna know if user.$id its an email or an id ? How can I gave him the two like that :

TypeScript
await teams.createMembership(teamId, user.email, user.$id, [], 'url');
Binyamin
16 Jun, 2023, 16:14

Oh, you're right You'll need to use the email But you have it

Binyamin
16 Jun, 2023, 16:14

So just like this

TypeScript
await teams.createMembership(teamId, user.email, [], 'url');
loup
16 Jun, 2023, 16:14

but I have to add the id no ?

Binyamin
16 Jun, 2023, 16:14

No..

Binyamin
16 Jun, 2023, 16:15

I know it sounds a bit confusing but it works that way

loup
16 Jun, 2023, 16:15

Appwrite gonna make the link between email and the corresponding user id ?

Binyamin
16 Jun, 2023, 16:15

Yes

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more