Back

[SOLVED] Google OAuth (at least) doesn't triggered "users.*.create" event

  • 0
  • Users
  • Accounts
  • Functions
AndrewIAm
20 Mar, 2023, 13:43

Trying to save user's data on OAuth session create fails due to users.*.create doesn't trigger on creating new user in Auth section of Appwrite. No error just execution doesn't run at all.

TL;DR
The issue is that the "users.*.create" event is not triggering when creating a new user in the Auth section of Appwrite. The reason for this is a bug in the system. The team is aware of the issue and is working on making changes to the event system to fix it. In the meantime, a workaround is to trigger on session create instead. If you have set the "users.*.create" trigger in the server, make sure it is assigned to the function in the console and not erased when redeploying the functions. You can also add the trigger in the "appwrite.json" file.
Binyamin
20 Mar, 2023, 13:56

I'm trying to get your use-case Where did you set users.*.create for event triggering? Because if you set it only in the server then it will be erased every time you redeploy the functions

So

  1. Check that the trigger is indeed assigned to function in the console
  2. What I usauly do is to add the trigger inside the appwrite.json file as such
TypeScript
{
  "$id"       : "FunctionID",
  "name"      : "FunctionName",
  "runtime"   : "node-16.0",
  "path"      : "functions/FunctionName",
  "entrypoint": "src/index.js",
  "ignore"    : [
    "node_modules",
    ".npm"
  ],
  "execute"   : [
    "any"
  ],
  "events"    : [
    "users.*.create"
  ],
  "schedule"  : "",
  "timeout"   : 15
}
Drake
20 Mar, 2023, 14:04

So there's a bug: https://github.com/appwrite/appwrite/issues/2406

The reason this is challenging is our event system was built to only trigger one event per endpoint. Things get messy when we need to trigger multiple (for create oauth2, we need to trigger a session and user event). We've been talking about making changes to our event system, but the team has been really tied up with other things.

AndrewIAm
20 Mar, 2023, 18:29

I’ve created it in Appwrite web interface, in the Settings section of my function. I’ll try you suggestions

safwan
21 Mar, 2023, 03:16

So is there any event that createOAuth triggers?

Drake
21 Mar, 2023, 03:17

The workaround at the moment is to trigger on session create

safwan
21 Mar, 2023, 03:27

alright sounds good

AndrewIAm
21 Mar, 2023, 05:47

yes, but there is not user's data so I have to make additional request to Google

AndrewIAm
21 Mar, 2023, 05:50

It didn't help

AndrewIAm
21 Mar, 2023, 05:58

Getting user data on create session doesn't work for some reason, I got:

TypeScript
    --> starting at object with constructor 'ClientRequest'
    |     property 'socket' -> object with constructor 'TLSSocket'
    --- property '_httpMessage' closes the circle
    at JSON.stringify (<anonymous>)
    at /usr/local/src/server.js:31:32
    at Array.forEach (<anonymous>)
    at console.log.console.info.console.debug (/usr/local/src/server.js:29:31)
    at module.exports (/usr/code-start/index.js:74:21)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async /usr/local/src/server.js:68:13```

My function code (part of it):
```else if (payload.provider === 'google') {
            const response = await axios.get('https://people.googleapis.com/v1/people/me?personFields=emailAddresses,names,photos', {
                responseType: 'json',
                headers: {
                    'Authorization': `Bearer ${payload.providerAccessToken}`
                }
            })
                .catch((error) => {
                    console.log('GOOGLE DATA ERROR', error);
                    res.json(error, 500);
                });

            console.log('------->>>', response)
            if (response) {
                const userData = response.data;

                console.log('USER DATA G: ', userData)
                const primaryName = userData.names.find(name => name.metadata.primary) ?? {};
                const primaryEmail = userData.emailAddresses.find(email => email.metadata.primary) ?? {};

                userProviderData = {
                    name: primaryName.displayName || `${primaryName.givenName} ${primaryName.familyName}` || 'John Doe',
                    email: primaryEmail && primaryEmail.value.toLowerCase(),
                    avatar: userData.photos.length ? userData.photos[0].url : ''
                };
            }
    }```
Probably, I missed something?
Drake
21 Mar, 2023, 06:21

What do you mean? There's a user id so you can fetch the user and then you'll have the user data

Drake
21 Mar, 2023, 06:21

You can't console.log response

AndrewIAm
22 Mar, 2023, 06:22

Gotcha, thanks for the clue 🙂 missed it

AndrewIAm
22 Mar, 2023, 06:22

Why?)

Drake
22 Mar, 2023, 15:09

There's a circular reference. you can log some specific property, though.

AndrewIAm
23 Mar, 2023, 06:53

[SOLVED] Google OAuth (at least) doesn't triggered "users.*.create" event

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