Back

[SOLVED] Enforce `ID.unique()` for all user accounts created

  • 0
  • Accounts
jSnake🐍🕊
4 May, 2023, 21:01

yeah accounts api for the client side access and users for server side with an API key

TL;DR
The user was able to solve the initial issue of enforcing `ID.unique()` for all user accounts created. They faced a new issue where they couldn't use their host IP from their local network and received a `connect ECONNREFUSED 127.0.0.1:4443` error. They also tried using an HTTP endpoint instead of HTTPS but encountered the same error. They asked for assistance and mentioned that their local network IP is not static and their router may not allow it. The issue was marked as solved after updating Git. They also mentioned their next challenge of getting TypeScript functions to work and advised ensuring the Appwrite CLI
jSnake🐍🕊
4 May, 2023, 21:03

im on 1.3.1 appwrite version, maybe the 1.3.2 version uses 'ID.unique()'?

safwan
4 May, 2023, 21:04

not a 100% sure. I'm away from my laptop so can't check immediately

Drake
4 May, 2023, 21:37

Function call: ID.unique()

jSnake🐍🕊
4 May, 2023, 21:40

where is that function defined? what should I import from the appwrite node sdk?

jSnake🐍🕊
4 May, 2023, 21:43

i would think that would have to happen on server side to ensure it is unique/valid or a collision would result in an error unless the function just returns the string 'unique()'

Drake
4 May, 2023, 21:57

Yes it's in the Appwrite node SDK. It's imported the same way you import Client

jSnake🐍🕊
4 May, 2023, 22:03

Ah, thanks, found it

TypeScript
class ID {
    static custom(id) {
        return id;
    }
    static unique() {
        return 'unique()';
    }
}

guess I missed it with my client wrapper

safwan
5 May, 2023, 05:15

heyo, is your issue resolved? if it hasn't feel free to ask any more questions, but if it has, let me know so I can mark it as solved 😄 <:appwritepeepo:902865250427215882>

jSnake🐍🕊
5 May, 2023, 06:18

well i started on creating a function to do this but now im getting this from the cli

TypeScript
Error Command failed: git clone --depth 1 --sparse https://github.com/appwrite/functions-starter .
Cloning into '.'...
fatal: cannot change to 'https://github.com/appwrite/functions-starter': No such file or directory
error: failed to initialize sparse-checkout
safwan
5 May, 2023, 06:27
safwan
5 May, 2023, 06:27

It was the same issue, and updating git fixed it

safwan
5 May, 2023, 06:28

also make sure your appwrite CLI is up to date

jSnake🐍🕊
5 May, 2023, 06:45

yup just updated git and worked too, thanks for the help! saved a lot of time there my next challenge is to see if I can get typescript'd functions

safwan
5 May, 2023, 07:04

In that case, I'll mark the issue as solved?

jSnake🐍🕊
5 May, 2023, 07:15

well i guess I will try to get the user creation function working and share it here for others

safwan
5 May, 2023, 08:02

alright, that's better

safwan
5 May, 2023, 08:03

if you need any help, let me know 🙂

jSnake🐍🕊
5 May, 2023, 08:33

getting a connect ECONNREFUSED 127.0.0.1:4443 error for some reason with the function

jSnake🐍🕊
5 May, 2023, 08:37

tried using http endpoint instead of https, same error

jSnake🐍🕊
5 May, 2023, 08:44

hmm using my host ip from my local network worked, that's not cool, my local network ip isn't static and I don't think my router even allows that..

jSnake🐍🕊
5 May, 2023, 08:45

welp guess that's a new question lol

jSnake🐍🕊
5 May, 2023, 09:01
TypeScript
module.exports = async function (req, res) {
    const client = new sdk.Client();
    const users = new sdk.Users(client);

    if (
        !req.variables['APPWRITE_FUNCTION_ENDPOINT'] ||
        !req.variables['APPWRITE_FUNCTION_API_KEY'] ||
        !req.variables['APPWRITE_FUNCTION_PROJECT_ID']
    ) {
        console.error("Environment variables are not set. Function cannot use Appwrite SDK.");
        res.json({
            success: false,
            error: "Server Error",
        }, 500);
        return;
    } else {
        client
            .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
            .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
            .setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
            .setSelfSigned(true);
    }

    if (!req.payload) {
        console.error("Invalid request");
        res.json({
            success: false,
            error: "Invalid request",
        }, 400);
        return;
    }

    const data = JSON.parse(req.payload);

    /* Additional validaiton omitted */

    if (!isValidPassword(data.password)) {
        console.error("Invalid password");
        res.json({
            success: false,
            error: "Invalid password",
        }, 400);
        return;
    }

    try {
        const user = await users.create(sdk.ID.unique(), data.email, undefined, data.password);
        res.json({
            success: true,
            data: {
                userId: user.$id,
                username: data.username,
            },
        });
    } catch (error) {
        res.json({
            success: false,
            error: error.message,
        }, error?.code || 400);
        return;
    }
};
jSnake🐍🕊
5 May, 2023, 09:01

I suppose it works, but got more things to figure out. can just mark this resolved

jSnake🐍🕊
5 May, 2023, 09:17

[SOLVED] Enforce ID.unique() for all user accounts created

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