#[Solved] trouble deleting users

371 messages · Page 1 of 1 (latest)

pure oxide
#

Hello I've been wondering if it's possible to delete a user from the Auth menu. I was able to write a cloud function that gets the uid and runs Users.delete(uid) doesn't delete the user in the Auth tab of the appwrite console. Would love to hear from other people more experienced

undone lantern
#

What was the results of the Users.delete function?

pure oxide
#

It says "user role: guests" "account required"

undone lantern
#

Can you share the code of the function?

pure oxide
#

I'm calling the function wrong I think. I'm using a http POST

#

Sure

#

That's the function I'm using dart for it

#

And here is how I'm sending the uid to it

undone lantern
#

What did you put inside the deleteUserFunctionId variable?

pure oxide
#

The payload I was trying to send

#

I'm probably doing this all wrong and there's a simple way of passing the uid to the function

undone lantern
# pure oxide

This code is inside the App in your flutter project?

pure oxide
#

The first one is a function folder I deployed and the second one inside my flutter app lib folder both are in my flutter project

undone lantern
#

Okay, good

pure oxide
#

Awesome

#

Not sure if the second one is right but I think the first one is since I followed the documentation for it

#

I just tried sending the uid string using http as a payload and I think that's the problem

#

Not sure how other appwriters send uids and other stuff to functions

undone lantern
#

So you'll need to change a few things.

First you can if you want to use Appwrite flutter package (https://appwrite.io/docs/getting-started-for-flutter)

API Key
The API key let's you access Appwrite resource on a server level https://appwrite.io/docs/sdks#server
That's means that unless you have a very good reason for that you only use this key in server side code as your function.

Flutter
So, In your flutter code (App) when execution a function you should remove the X-Appwrite-key header.

Function
In your function I think that you'll need to to change the line 34 in which you're using the deleteUserFunctionId constant to delete_user_API one

#

Is this make sense?

pure oxide
#

Yup makes sense didn't know the header part and I think you're right about changing the deleteUserFunctionId to delete_user_API

#

Going to try them out now

undone lantern
pure oxide
#

Just curious but if you know any other way of sending strings to a function and calling a function let me know

undone lantern
#

Sure

#

You can add the flutter SDK in your pubspec

dependencies:
  appwrite: ^9.0.0

If you're using the cloud then add this version

dependencies:
  appwrite: ^8.1.0
pure oxide
#

Awesome I saw that in the documentation but I didn't know how to get the data from it in the function

undone lantern
pure oxide
#

So if I execute the function as Future result = functions.createExecution(
functionId: '[FUNCTION_ID]', data: '[USER_ID]'
);

#

I'd get the data from res

#

In the function

undone lantern
#

Yes
But this way is in sync mode in you get your results in the then function

pure oxide
#

Okay gotcha. Do I have to do res.data or res.payload

undone lantern
#

You can run it also async (which is my preferred way) like so

try{
  final answer = await functions.createExecution(
    functionId: '[FUNCTION_ID]', data: '[USER_ID]'
  );
} catch (e){
  // handle error
}
pure oxide
#

I didn't know

undone lantern
pure oxide
#

Ohh

#

So I use res.data

undone lantern
pure oxide
#

No problemI'm probably confusing you. I know I can send data to the function but I don't know how to use that data for the Users.delete('[USER_ID]') when I use Future result = functions.createExecution(
functionId: '[FUNCTION_ID]', data: '[USER_ID]'
); you said it will be in the response and in the function I see req and res

undone lantern
#

Ohh

#

Got you

pure oxide
#

I thought maybe it would be res.data or maybe it's the same code I'm using now

undone lantern
#

you mean in the function code?

pure oxide
#

Yes

#

Does it need to change if I use the functions.createExecution()

undone lantern
#

No
It should work the same

pure oxide
#

Ohh okay awesome

undone lantern
#

functions.createExecution is a convinced wrapper around Appwrite REST API

pure oxide
#

Ahhh gotcha

pure oxide
#

Switching to createExecution () is helping me narrow down the issue this is what it tells me

undone lantern
#

Where you did you got this error?
In the function or in the App?

pure oxide
#

In the app

#

Maybe if I make the function execute access for all/user/guests it will work?

undone lantern
#

In your function remove the Guest option leave only the any and try again

pure oxide
#

Okay

pure oxide
#

Damn it's still not working

undone lantern
#

What now?

pure oxide
#

Same thing but when I do any for roles that should excute I don't get errors displayed but the execution fails and I can't delete

undone lantern
#

Good, No you can debug the function

#

What is the error you get there?

pure oxide
#

Maybe I shouldn't try making the function in dart

undone lantern
#

You can do it in any of the supporting languages Appwrite have

pure oxide
#

Doesn't show logs in the console

#

No errors just says failed

undone lantern
#

All those tabs are empty?

pure oxide
#

Yup all empty

undone lantern
#

That means it crashes before

pure oxide
#

Only thing I see is under errors and says operation timed out

undone lantern
#

Add
res.json in your catch

pure oxide
#

Internal curl error

undone lantern
pure oxide
#

Okay

#

What's up

undone lantern
#

That means the address is maybe worng

pure oxide
#

Ohhh

undone lantern
#

You've set all your variables like so?

pure oxide
#

Oh boy

#

I think i found the problem

#

Those are in the function?

undone lantern
#

Yes, you need to set them in the functions settings

pure oxide
#

Oh boy

#

I think that's the problem

#

Wow

undone lantern
#

Go ahead and set them, then access each one of them using the req.variables['variable_name']

pure oxide
#

Okay

#

Got it

#

Wow it worked and I finally got a completed instead of failed

#

But the user is still in the Auth tab

undone lantern
#

Cool

undone lantern
pure oxide
#

Nope all output is successful

undone lantern
#

Can you share the function code as text?

pure oxide
#

I can do pastebin

#

One sec

#

Here you go

#

Oh crap I found the error

#

I didn't specify the host

#

In the uri

undone lantern
#

Yes.

#

You need to replace those lines

#
      .setEndpoint(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
      .setProject(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
pure oxide
#

Wow

#

Yup

#

Atleast now I know a 100 ways not to make an appwrite cloud function

#

Thank you for being patient

#

Weird thing is now it's failing

#

I'm trying to do the function without async because I remember you telling me "Yes
But this way is in sync mode in you get your results in the then function"

#

Nope nvm needs to stay async

undone lantern
#

So what's falling now?

#

Is this your code?

import 'package:dart_appwrite/dart_appwrite.dart';


Future<void> start(final req, final res) async {
  final client = Client();


  client
      .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
      .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
      .setKey(req.variables['APPWRITE_FUNCTION_API_KEY']);

  final users = Users(client);

  try {
    final userId = req.payload;
    await users.delete(userId: userId);
    res.json({'message': 'User deleted'});
  } catch (e) {
    res.send('Error deleting user: $e', status: 500);
  }
}
undone lantern
pure oxide
#

Yes this is my code. It went back to saying an internal curl error Error Msg operation timed out

undone lantern
#

Try this

import 'package:dart_appwrite/dart_appwrite.dart';


Future<void> start(final req, final res) async {
  final client = Client();


  client
      .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
      .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
      .setKey(req.variables['APPWRITE_FUNCTION_API_KEY']);

  res.json(req.variables);
  /*final users = Users(client);

  try {
    final userId = req.payload;
    await users.delete(userId: userId);
    res.json({'message': 'User deleted'});
  } catch (e) {
    res.send('Error deleting user: $e', status: 500);
  }*/
}
#

So you can verify that all the variables sets

pure oxide
#

Okay

#

That was an awesome idea and I think I found the final problem 😀

#

For the endpoint this is what it looks like

#

Https://example.com/v1

#

Everything thing else looks fine

#

The brackets for the endpoint lol weird

#

Look

#

They do / instead of //

#

///

#

Let me take a picture

#

Discord messes it up

#

Not sure why the backslashes look like that

pure oxide
#

I don't think the endpoint is the problem. Not sure what I'm doing wrong

undone lantern
# pure oxide

Is this your endpoint
Appwrite.test.org or you're using hosts file?

#

If so add setSelfSigned() to your client

  client
      .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
      .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
      .setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
      .setSelfSigned();
#

Then try again

pure oxide
undone lantern
# pure oxide

Did you put your Appwrite URL in the APPWRITE_FUNCTION_ENDPOINT variable?

pure oxide
#

The setting it setSelfSigned(status: true) didn't fix it

#

I put the exact endpoint I get on the appwrite console

#

Should it be just my url?

undone lantern
# pure oxide

So here you've adjusted the value to show appwrite.test.org?

pure oxide
undone lantern
#

So you've set this address locally in your computer?

pure oxide
#

Nope it's a domain

undone lantern
#

This is your domain?

#

Of your Appwrite?

pure oxide
#

Yup

lunar moon
pure oxide
#

Ohhh

#

No that's my own domain. Appwrite cloud looks awesome

undone lantern
#

You own the domain test.org?

pure oxide
#

No I just put test

#

For an example

lunar moon
# pure oxide I'm

what's your code and how long does the execution run for before the error occurs?

undone lantern
# pure oxide For an example

So you'll need to put your working Appwrite URL + v1 putting appwrite.test.org make the function point to this url which return nothing and which makes the curl timeout

pure oxide
#

I'll count how long it takes before throwing the curl error and failing

lunar moon
lunar moon
pure oxide
#

Okay

#

15s

#

Trying that now

undone lantern
#

Is your Appwrite is local or on a remote host?

pure oxide
#

Thanks for being patient with me guys

#

Remote host byawitz

undone lantern
#

Okay
So let's assume your Appwrite is https://aw.domain.com

Then set this https://aw.domain.com/v1 to APPWRITE_FUNCTION_ENDPOINT
And try again

pure oxide
undone lantern
#

You still get timeout?
And you can verify that all the APPWRITE_FUNCTION_ENDPOINT contains your server domain?

pure oxide
#

Yup i can verify because you helped me res.json(req.variables)

pure oxide
lunar moon
pure oxide
#

Okay will do and let you guys know in 2 hours

lunar moon
pure oxide
#

Nope

#

Picking up my kid

pure oxide
#

It still fails with the same error when I increase the timeout in the function to 2 minutes. When I do res.json(res.variables) everything prints correctly on the appwrite console logs. Not sure what's wrong. Maybe I should try the function in nodejs and not dart?

undone lantern
#

Let's try from the beginning.

#

Can you share your Appwrite url, either here or in DM?

lunar moon
pure oxide
#

One was 3s and the other was 119ms

undone lantern
#

What was the result of the 119ms one?

pure oxide
#

Same error

#

Internal curl error

#

I don't think anyone else is having these issues maybe I need to docker-compose up -d -force -recreate

undone lantern
#

it's wierd

#

I think we just missing something.

pure oxide
#

Yup probably something super simple I'm messing up

lunar moon
pure oxide
#

I can't share the exact appwrite url. I can show how I have it formatted. Sorry for not being able to share that

undone lantern
pure oxide
lunar moon
pure oxide
#

I removed status:500

lunar moon
undone lantern
#

And you sure that this variable APPWRITE_FUNCTION_ENDPOINT contains the same URL you have here? in your project settings?

pure oxide
#

Yes I made sure it's the same as url in the project settings

#

It does something weird tho that I've never seen a url do let me show you

#

See the weird backspaces

undone lantern
#

But why you have the value test.org in there?

pure oxide
#

Yup

#

That's just to not show my real url

undone lantern
#

Ohh, so you have replace it in inspect element?

pure oxide
#

Yes

undone lantern
#

Clever 👍

lunar moon
undone lantern
#

And you can verify that everything in order when you click the eye button in the function settings page?

pure oxide
#

Yup even is right

#

I don't have SG_TEMPLATE_ID or SG_API_KEY

#

Everything is write

undone lantern
#

Yes that's okay
That's project of mine

pure oxide
#

Oh okay

lunar moon
pure oxide
#

Okay

#

How do I do that

lunar moon
pure oxide
#

Okay

#

It failed again

undone lantern
#

Just to know what version of Appwrite you're running on?
You can find it either in the bottom of your console or by going to your appwrite domain and adding versions at the end as such appwrite.domain.com/versions

lunar moon
pure oxide
#

1.34

#

Is the version

#

Ran for 18 secs

lunar moon
# pure oxide It failed again

I ran this exact code and it was successful and gave me a response:

import 'package:dart_appwrite/dart_appwrite.dart';

Future<void> start(final req, final res) async {
  try {
    final client = Client();

    client
        .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
        .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
        .setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
        .setSelfSigned();

    //res.json(req.variables);
    final users = Users(client);

    final userId =
        req.payload; // Assuming the userId is passed as the request payload
    await users.delete(userId: userId);
    res.json({'message': 'User deleted'});
  } catch (e) {
    res.send('Error deleting user: $e');
  }
}

so if you have this...it shouldn't fail...

#

can you share a screenshot of your deployments, executions, and settings (variables and timeout) pages

pure oxide
#

Awesome glad it's just me. Here are all the screenshots

#

Didn't send one for the response logs because it's the same as the logs tab

#

I do have a real-time subscription running on the page that I'm trying to run the delete

#

Not sure if that could be it but at this point I'm pretty sure it's something simple I missed

#

Sorry for all the trouble guys

undone lantern
#

Quite the opposite when finding the solution it can help in the future

pure oxide
#

And how many real-time subscriptions is too much. Right now I'm using 2

undone lantern
#

Can you share also the settings screen.

pure oxide
#

Okay

#

And thank you

#

Here you go

lunar moon
#

so if you comment out the await.users.delete(), it'll execute successfully?

lunar moon
pure oxide
#

I think I found the issue guys

#

I added dart to the function runtimes but not to the function envs

undone lantern
#

It's okay

pure oxide
#

Could that be it?

undone lantern
#

The last one is deprecated

pure oxide
#

Dammit

undone lantern
#

Yeah, sorry

#

The problem is only between the function and the server it tries to connect.

pure oxide
#

Damn

undone lantern
#

Can you try what Steven suggested to run it without the user line
like so

import 'package:dart_appwrite/dart_appwrite.dart';

Future<void> start(final req, final res) async {
  try {
    final client = Client();

    client
        .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
        .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
        .setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
        .setSelfSigned();

    //res.json(req.variables);
    final users = Users(client);

    final userId = req.payload; // Assuming the userId is passed as the request payload
    // await users.delete(userId: userId);
    res.json({'message': 'User deleted'});
  } catch (e) {
    res.send('Error deleting user: $e');
  }
}
pure oxide
#

Okay

#

Yup the code Steven sent works for me too Byawitz

#

Issue is when I try to call the Users.delete()

#

Will try to call it

lunar moon
lunar moon
undone lantern
#

I think we can add timeout to the await function and then dart should throw an error, maybe this will help debug the problem
Try this:

...
  await users.delete(userId: userId).timeout(const Duration(seconds: 1));
...
lunar moon
undone lantern
#

Can you share a picture of the output of the command

docker ps -a
pure oxide
#

I moved the delete button to a different page that doest use real-time to make sure that wasnt what was causing it

#

I'll try the timeout you told me Byawitz

#

Yeah Steven you're right by I think the Users.delete() is not working for some reason otherwise event works

#

Eveything

#

I'll try to run that command Byawitz on my server

lunar moon
pure oxide
#

Okay

#

How can I do that

undone lantern
lunar moon
pure oxide
#

I'm guessing most people have a way easier time deleting a user than this

#

Deployed his solution now and gonna try it

pure oxide
#

It completes

#

But future didn't

#

Completed in the duration time 1s

#

And Steven I'm not deploying it on the console but when I do I get the same error

lunar moon
pure oxide
#

Okay

#

Running that now

#

It looks paused not sure if that's normal

#

Had to cancel it

#

Doesn't show anything

lunar moon
pure oxide
#

It's not paused anymore I finally got an output. It says " curl: (28) Failed to connect to appwrite.myurl.org port 443: Connection timed out

#

I guess curl is where my problems are?

undone lantern
#

I think you should check the Firewall as Steven suggested

#

In what company is your server in?

lunar moon
#

or what operating system is your appwrite instance running on?

lunar moon
pure oxide
#

Damn

#

Atleast I now know I didn't do anything wrong

#

I was feeling kinda down for not making it work

#

Maybe unrelated but when I deploy my function it doesn't give me the correct time it created the deployment

#

I usually deploy multiple times for it to show the right time

pure oxide
#

On the appwrite console when I do a new deployment the time created says I did it 20 mins ago instead of right now do I usually redeploy and the the time created is the current time

lunar moon
pure oxide
#

Yes

#

And it's probably unrelated

lunar moon
# pure oxide Yes

when you deploy, you should see something show up in the inactive table as it's building:

pure oxide
#

You guys are awesome for helping

#

Yup that one I have no issues with

lunar moon
#

Then, the updated at timestamp in the active section should update. and the build logs should show you the logs for the active deployment:

pure oxide
#

Ahhh gotcha

#

I was always reading the inactive

#

But for the server test you told me to run I used the right deployment id

lunar moon
#

so what operating system is your appwrite instance running on?

pure oxide
#

It's on Ubuntu 20.04 x64

lunar moon
pure oxide
#

Ok

#

Here to go

#

One says deny

lunar moon
# pure oxide

good. this confirms you have a firewall enabled. just for testing, you can disable it to see if it makes a difference. disable with:

sudo ufw disable
pure oxide
#

Okay will do right now

lunar moon
#

then you can try executing your function or that docker exec curl command

pure oxide
#

I can later add an exception if this fixes it?

#

Cool

#

WOW it worked

#

Unbelievable

#

I would've never fixed that alone

#

You guys are awesome

#

How can I add firewall exceptions and what exceptions

#

I have no idea where to start

#

Thank you guys @undone lantern and @lunar moon

lunar moon
pure oxide
#

I feel like a pro at functions now

#

Okay

#

Will do that now

lunar moon
# pure oxide WOW it worked

this would have been 100000x easier if that function returned the error...which it really should have. i've run into this case before but the function always returned an error so we knew the request was being blocked

pure oxide
#

Wow I guess my firewall was maybe even blocking the error from being return

lunar moon
pure oxide
#

Done

#

Maybe

lunar moon
pure oxide
#

Let me make a new user and try

#

And I have a couple questions to ask that aren't related but I just wanna get to know appwrite more

lunar moon
pure oxide
#

Okay will do

#

They aren't bugs

#

Don't worry

#

But I'll make separate support posts

#

The function failed with the exceptions I added

lunar moon
pure oxide
#

Worked when I disabled the firewall again

#

Oops

#

How do I delete a port

#

I think I found out

#

Sudo udw delete

#

Followed by the number

#

Added the right port number

#

Yup works

#

Amd thank you guys

#

And

lunar moon
pure oxide
#

💯

#

No the thanks is all too you guys

#

Love your product and your help