Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Bug Report: Trigger a function users.*.create when a user is created with phone #7101

Open
2 tasks done
silentmado opened this issue Nov 8, 2023 · 9 comments
Open
2 tasks done
Assignees
Labels
bug Something isn't working product / auth Fixes and upgrades for the Appwrite Auth / Users / Teams services.

Comments

@silentmado
Copy link

馃憻 Reproduction steps

  1. create a function and set triggers on users.*.create
  2. create a user using phone

馃憤 Expected behavior

function should be executed

馃憥 Actual Behavior

function is not executed

馃幉 Appwrite version

Version 1.4.x

馃捇 Operating system

Linux

馃П Your Environment

appwrite 1.4.3

馃憖 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

馃彚 Have you read the Code of Conduct?

@silentmado silentmado added the bug Something isn't working label Nov 8, 2023
@loks0n
Copy link
Member

loks0n commented Nov 9, 2023

Phone session does not trigger a user.*.create event, it triggers users.*.sessions.*.create event.

@mdmohsin7
Copy link

mdmohsin7 commented Nov 10, 2023

Phone session does not trigger a user.*.create event, it triggers users.*.sessions.*.create event.

It still doesn't get triggered for me. Tried on both Appwrite Cloud and Self Hosted instance (1.4.9). For both I have added the users.*.sessions.*.create event. Tried with just the users.* event as well, still isn't triggered.

@loks0n
Copy link
Member

loks0n commented Nov 10, 2023

It still doesn't get triggered for me. Tried on both Appwrite Cloud and Self Hosted instance (1.4.9). For both I have added the users.*.sessions.*.create event. Tried with just the users.* event as well, still isn't triggered.

Has the user called the Create phone session (confirmation) endpoint? This is when the session is created and the event should be triggered

@mdmohsin7
Copy link

Has the user called the Create phone session (confirmation) endpoint? This is when the session is created and the event should be triggered

Yes I am calling the create phone session endpoint.
To make sure I'm doing it right, I am attaching the code snippet below in Dart:

final client = Client()
    .setEndpoint('endpoint')
    .setProject('some_id');

final account = Account(client);
final sessionToken = await account.createPhoneSession(
    userId: ID.unique(),
    phone: '+14255550123'
);

@loks0n
Copy link
Member

loks0n commented Nov 10, 2023

Yes I am calling the create phone session endpoint. To make sure I'm doing it right, I am attaching the code snippet below in Dart:

final client = Client()
    .setEndpoint('endpoint')
    .setProject('some_id');

final account = Account(client);
final sessionToken = await account.createPhoneSession(
    userId: ID.unique(),
    phone: '+14255550123'
);

The session is not created until the user receives the code, and uses it to call account.updatePhoneSession. Please see https://appwrite.io/docs/references/cloud/client-web/account#updatePhoneSession

@mdmohsin7
Copy link

The session is not created until the user receives the code, and uses it to call account.updatePhoneSession. Please see https://appwrite.io/docs/references/cloud/client-web/account#updatePhoneSession

Oh okay got it.
I just now came across this thread on Discord where Steven has mentioned that no event gets triggered for the createPhoneSession endpoint.
I actually want to get that code (which is sent to the user). Is there any way to get that code before actually the user enters it?

@mdmohsin7
Copy link

I went through the codebase and I saw that the generated code is stored in a collection named "tokens".

        $secret = Auth::codeGenerator();
        $expire = DateTime::formatTz(DateTime::addSeconds(new \DateTime(), Auth::TOKEN_EXPIRATION_PHONE));

        $token = new Document([
            '$id' => ID::unique(),
            'userId' => $user->getId(),
            'userInternalId' => $user->getInternalId(),
            'type' => Auth::TOKEN_TYPE_PHONE,
            'secret' => Auth::hash($secret),
            'expire' => $expire,
            'userAgent' => $request->getUserAgent('UNKNOWN'),
            'ip' => $request->getIP(),
        ]);

        Authorization::setRole(Role::user($user->getId())->toString());

        $token = $dbForProject->createDocument('tokens', $token
            ->setAttribute('$permissions', [
                Permission::read(Role::user($user->getId())),
                Permission::update(Role::user($user->getId())),
                Permission::delete(Role::user($user->getId())),
            ]));

I tried adding the event databases.*.collections.tokens.documents.* and databases.*.collections.tokens, but none of them seems to cause the function to be triggered. Is there a way to access the tokens collection?

@silentmado
Copy link
Author

@loks0n why it doesn't create a user.*.create event while we have really a user created in appwrite, doesn't it make sense to have that event ?
I have noticed that the session creation takes 2 steps:

  1. first step where the code is sent to mobile phone number: and here we have a user being created in appwrite (not verified but created) and then we should have user.*.create event triggered. not ?
  2. second step Create phone session (confirmation): and here we have a session being created and then we should have users.*.sessions.*.create triggered.

@stnguyen90
Copy link
Contributor

This is a long-standing issue we have with our event system in which only 1 event is triggered per endpoint. We plan to rewrite the event system to fix this problem.

Related issues:

@stnguyen90 stnguyen90 added the product / auth Fixes and upgrades for the Appwrite Auth / Users / Teams services. label Nov 13, 2023
@stnguyen90 stnguyen90 self-assigned this Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working product / auth Fixes and upgrades for the Appwrite Auth / Users / Teams services.
Projects
None yet
Development

No branches or pull requests

4 participants