Back

How to retrieve name and email after sign in with Google

  • 1
  • Flutter
  • Functions
  • Accounts
Mosh Ontong
28 Aug, 2023, 00:57
TypeScript
 final res = await account.createOAuth2Session(
        provider: AuthProvider.google.name,
        scopes: ['profile', 'email'],
      );

I have this code to create session with that provider but I want to have name or email return, but in my case it returns null. So why do I need return values after create session? It because I need to trigger a function

TypeScript
      await functions.createExecution(
        functionId: newUserFunctionId,
        data: jsonEncode(data),
        xasync: true,
      );

Since the appwrite still have issue on event trigger when creating a new account with google provider

TL;DR
The user is trying to retrieve the name and email after signing in with Google. They are getting an error message saying that the 'name' attribute is missing. The solution suggested is to call the account.get() function on the client side to get the user's email and name. Another suggestion is to execute the function on session creation and check if it is an OAuth session and an account was just created. They can use the account.createOAuth2Session() function with the 'profile' and 'email' scopes to get the user's information. There seems to be an issue with the event trigger when creating a new account with the Google provider
Drake
28 Aug, 2023, 00:59

If your goal is to do something with a function when a user is created, I recommend executing the function on session creation as well. Then, you can check if this is an oauth session and if an account was just created

Drake
28 Aug, 2023, 01:00

Otherwise, the user should be passed to the function that the user executes. You can use the users API to get the user and that should have the email and name

Drake
28 Aug, 2023, 01:00

The last option is to call account.get() client side.

Mosh Ontong
28 Aug, 2023, 01:01

alright thanks steve

Mosh Ontong
28 Aug, 2023, 01:09
TypeScript
{$id: 64ebf33b0a2b366370c2, $createdAt: 2023-08-28T01:07:07.069+00:00, userId: 64ebf33ac92690731d4a, expire: 2024-08-27 01:07:07.041, provider: google, providerUid: 101926932913220533651, providerAccessToken: ya29.a0AfB_byAcBR1WhRteyjfI9gIDZKqaY5IofjFQElwwQgD-yG2WyGXBUk87Lf5uRoMl5jYqnAoFjkDvsM7fqhGTt91XUotpU51uczwQMyCU3zIWk2AN38qCnz5EXTwnSzXnLJhrjrXhWQRQs1MYEIa5U3bBCeVAqWnEYuLUy-bDaCgYKARoSARISFQHsvYls0vuU9cOPARVTM_cvpTotNw0175, providerAccessTokenExpiry: 2023-08-28T02:07:06.041+00:00, providerRefreshToken: , ip: 158.62.81.232, osCode: AND, osName: Android, osVersion: 10, clientType: browser, clientCode: CM, clientName: Chrome Mobile, clientVersion: 116.0, clientEngine: Blink, clientEngineVersion: 116.0.0.0, deviceName: smartphone, deviceBrand: , deviceModel: , countryCode: ph, countryName: , current: false}
Invalid document structure: Missing required attribute "name"

I just do the execte when session creation, I just try to print the payload

TypeScript
 final payload = jsonDecode(req.variables['APPWRITE_FUNCTION_EVENT_DATA']);
  print(payload);

There is no email or name of the user

Drake
28 Aug, 2023, 01:11

Right...you would need to fetch the user

Mosh Ontong
28 Aug, 2023, 01:11

Ohhh I seee

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