Skip to content
Init is here / May 19 - 23
Back

Create User with function

  • 0
  • Users
  • Functions
  • Web
loup
25 Jun, 2023, 11:19

I try to dev a function for creating a new user. There is my function :

TypeScript
const sdk = require("node-appwrite");

module.exports = async function (req, res) {
  const client = new sdk.Client();
  const account = new sdk.Account(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 user = JSON.parse(req.variables["APPWRITE_FUNCTION_DATA"]);
  try {
    console.log('step1')
    const {$id} = await account.create('unique()', user.email, user.password, user.name)
    console.log('step2')
    await databases.createDocument(req.variables["APPWRITE_FUNCTION_DATABASE_ID"], req.variables["APPWRITE_FUNCTION_COLLECTION_ID"],$id, {
      "userId": $id,
      "username": username
    });
    console.log('step3')
    await account.createVerification(user.redirectURL)

  } catch (error) {
    console.log('error', error)
    res.json({ error: 'Error creating user', error: error});
  }
};

But the console stop at console.log('step1'). There is an example of data that I pass to the function :

TypeScript
{
    "email": "name@example.com",
    "password":"password",
    "name":"myname",
    "username": "myusername",
    "redirectURL":"http://localhost:3000/verifyEmail"
}
TL;DR
The user is trying to create a function for creating a new user. They are encountering an error when trying to use the `account.createVerification` function. They are also unsure about how to handle user authentication after signup. The suggestion is to manually make an API call to create the email verification. Additionally, the user is using the wrong endpoint (`account.create` instead of `users.create`) in their function. Solution: 1. Manually make an API call to create the email verification. 2. Use the correct endpoint `users.create` instead of `account.create` in the function.
loup
25 Jun, 2023, 11:19

My Function API got this permissions

Bouahaza
25 Jun, 2023, 11:26
loup
25 Jun, 2023, 11:31

arhh im dumbb

loup
25 Jun, 2023, 13:08

Well everything work except the .createVerification function in my function :

TypeScript
const sdk = require("node-appwrite");

module.exports = async function (req, res) {
  const client = new sdk.Client();
  const account = new sdk.Account(client);
  const users = new sdk.Users(client)
  const databases = new sdk.Databases(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 user = JSON.parse(req.variables["APPWRITE_FUNCTION_DATA"]);
  try {
    const {$id} = await users.create('unique()', user.email, undefined, user.password, user.name)
    await databases.createDocument(req.variables["APPWRITE_FUNCTION_DATABASE_ID"], req.variables["APPWRITE_FUNCTION_COLLECTION_ID"],$id, {
      "userId": $id,
      "username": user.username
    });
    await account.createVerification(user.redirectURL)
  } catch (error) {
    res.json({ error: 'Error creating user', error: error})
    throw error
  }
};
loup
25 Jun, 2023, 13:08

Ive got a role error : {"error":{"code":401,"type":"general_unauthorized_scope","response":{"message":"app.648cd565d979363284c1@service.cloud.appwrite.io (role: applications) missing scope (account)","code":401,"type":"general_unauthorized_scope","version":"0.10.42"}}}

Drake
25 Jun, 2023, 14:42

The account.createVerification requires a user session which doesn't really exist server side. You'll need to manually make an API call to create an email session and create the email verification

loup
25 Jun, 2023, 15:24

Okay so I cant createVerification without being connected ? What can I do if I don't want a user to be connected after signup ? I guess that I have to connect it automatically after the signup

loup
25 Jun, 2023, 15:27

I've never used a JWT, but with this kind of token can't we authorize the function to create a verification email?

loup
25 Jun, 2023, 15:28

Like in this docs example :

TypeScript
const sdk = require('node-appwrite');

// Init SDK
const client = new sdk.Client();

const account = new sdk.Account(client);

client
    .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setJWT('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ...') // Your secret JSON Web Token
;

const promise = account.createVerification('https://example.com');

promise.then(function (response) {
    console.log(response);
}, function (error) {
    console.log(error);
});
loup
25 Jun, 2023, 15:51

Well in any case I compared the speed of registration via an Appwrite function against the client-side, is a few seconds ago (about 5sec via the function) while it is instantaneous from the client-side. So if I have to log in to start an email check, I might as well stay on the client side

Drake
25 Jun, 2023, 17:53

5s might be a cold start. It should be faster after that

Drake
25 Jun, 2023, 17:54

There's nothing wrong with allowing users to log in after creating an account

loup
25 Jun, 2023, 18:02

Yeah I just see a lot of website that doesnt log after signup ahah but ty a lot !

Drake
25 Jun, 2023, 18:03

I would suggest restricting access to resources to verified users

loup
25 Jun, 2023, 18:11

Yeah ive got 2 functions :

  • user_create : add each new user in the userteam
  • user_verified: add role verified when user verify his email address (but isnt working for now ahah)
Drake
25 Jun, 2023, 18:23

There are already built in roles for registered users and verified users 🧐

See https://appwrite.io/docs/permissions#permission-roles

loup
25 Jun, 2023, 18:39

wait what ahah i wanna died

loup
25 Jun, 2023, 20:05

Well I just have to add that ?

loup
25 Jun, 2023, 20:06

Maybe without the [] between verified

Drake
25 Jun, 2023, 23:54
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