Back

Security: Running a cloud Function

  • 0
  • Functions
  • Web
  • Cloud
yalondpsi
4 Oct, 2023, 15:48

I made a cloud function that's get a user id and deletes that user in a case when the registration did not go well.

Is it safe to run that cloud function from the client, even without user login first? What is the best practice regarding security?

TL;DR
The user is asking about passing username to a cloud function for a registering user. The response suggests creating a separate function for logged in users and implementing the username logic within that function. The user also asks about listening to an event when a user's session is expired, and the response suggests setting up a webhook or function. In terms of security, the user asks for recommendations on best practices for securing a web app and the response suggests checking out a chapter on self-hosted Appwrite security.
Binyamin
4 Oct, 2023, 15:56

How would you know the user if the user isn't logged in?

yalondpsi
4 Oct, 2023, 16:05

You are right πŸ™‚ - so if the user registering is already logged in? the account.create is making the user logged in?

Binyamin
4 Oct, 2023, 16:07

No, It won't be logged in But in that case you can create a function and make it triggered based on the user-creation event.

So

  1. User registering using account.create
  2. Your function is being triggered and run on the spot to check if the account is "okay"
  3. If not the user is being deleted/blocked

The advantage of that method is that Appwrite will call that function behind the scenes and no-one else can access it from the out-world.

yalondpsi
4 Oct, 2023, 16:10

Yes, I understand your point. Ok. I will try to implement that. Is there any articles / books / YT / Courses that you recommend about best practices in securing a web app?

Binyamin
4 Oct, 2023, 16:10

From what aspect?

Binyamin
4 Oct, 2023, 16:11

In this chapter of something I've written you can find out about self-hosted Appwrite security

yalondpsi
4 Oct, 2023, 16:12

Ok thank you very much for the valuable info

yalondpsi
4 Oct, 2023, 16:14

General rules of thumb regarding security when building a web app I'm using appwrite cloud

Binyamin
4 Oct, 2023, 16:14

I can't think of a general like that right now

yalondpsi
4 Oct, 2023, 16:15

Ok - thanks

yalondpsi
4 Oct, 2023, 16:18

BTW, is it possible to listen to event when a user's session is expired? I tried to register that event without getting any inputs from the system. Should I write a cloud function for that?

Binyamin
4 Oct, 2023, 16:20

You can see here the users.*.sessions.*.delete event

Binyamin
4 Oct, 2023, 16:20

And yes in order to use this event you'll need either set a webhook or function.

yalondpsi
4 Oct, 2023, 16:21

Thank you very much!

yalondpsi
4 Oct, 2023, 17:02

@Binyamin One more question please: I need to add a username to a registering user (the username is entered by the user).

So, a cloud function will be triggered when an new user is registering by email/password or with google (for example). Is it possible to pass that cloud function which is (automatically got executed) the username it suppose to write to the DB?

Binyamin
4 Oct, 2023, 17:24

The cloud function for now can be triggered only for email password creation method.

As for the user name. You'll need to create another function (or use the existing one with some route) and let that function to be executed only for logged in users And within that function you can write the username logic

yalondpsi
4 Oct, 2023, 17:26

@Binyamin So, to pass the username I need to execute "manually" the function and pass to it the username?

Binyamin
4 Oct, 2023, 17:26

Correct

yalondpsi
4 Oct, 2023, 17:27

@Binyamin and when the support for OAuth registration events will be - do you have an estimation ?

Binyamin
4 Oct, 2023, 17:28

I don't think in the near future, but will need core <:appwrite:1156975611466764298> member confirmation

yalondpsi
4 Oct, 2023, 17:29

@Binyamin Ok, thank you, again !

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