[SOLVED] Add a default team for new User and adding verified role for verified email account
- 0
- Teams
- Functions
- Web
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'
You'll need to create two functions.
- On user create
- 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.
What is the function api key ?
When accessing Appwrite You can do it in one of two ways:
- Server side
- Client side https://appwrite.io/docs/sdks
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
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 ?
In your case you'll need the Auth one.
As you want the function to have access to some Auth functions.
Okay well only Auth ?
Seems like it, yes.
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 ?
For now, there's no direct way. You can upvote this one https://github.com/appwrite/appwrite/issues/5425
What I'm doing is creating a file named local-server and run it each time with mocked variables, etc.
Okay np but I guess my function will be very simple so
Well something like that should work (for the function who add the default Team for new user) ?
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' });
}
};
But do I have to add the MEMBER rôle ? Or I can leave blank ?
I think the function name is createMembership instead of createTeamMember, like so:
const r = await teams.createMembership(teamID, email, [], 'url');
You can leave it empty
If I got that :
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 :
await teams.createMembership(teamId, user.email, user.$id, [], 'url');
Oh, you're right You'll need to use the email But you have it
So just like this
await teams.createMembership(teamId, user.email, [], 'url');
but I have to add the id no ?
No..
I know it sounds a bit confusing but it works that way
Appwrite gonna make the link between email and the corresponding user id ?
Yes
Recommended threads
- Unable to create Sites or Functions with...
Heya, I was looking at the appwrite documentation for Sites API with the server api: https://appwrite.io/docs/references/cloud/server-nodejs/sites I can’t fin...
- Triggers and call function in function p...
Hello, Today we are experiencing several issues with Appwrite Cloud. Functions triggered by events, or functions called from another function, are taking an e...
- TablesDB `updateRows` returns `database_...
Hi Appwrite team! I’m seeing a strange issue with TablesDB bulk row updates on a self-hosted Appwrite instance. **Environment** - Appwrite self-hosted `1.9.0` ...