How to get user data when signing in with onSuccess?

I want to track when a user has signed in and who has signed in for analytics. My analytics tool works only on the client side. That's why I need to use the onSuccess callback like this:

// This is inside my client component

const handleSignInWithGoogle = async () => {
    await authClient.signIn.social(
      {
        provider: "google",
        callbackURL: redirectTo,
      },
      {
        onSuccess: async (ctx) => {
          // handle event tracking
        },
      },
    );
  };


Is there any way to get the current user inside the function? or is there a better option? The CTX context only returns the following:

{
  "data": {
    "url": "******",
    "redirect": true
  },
  "response": {},
  "request": {
    "baseURL": "http://localhost:3000/api/auth",
    "credentials": "include",
    "method": "POST",
    "plugins": [
      {
        "id": "redirect",
        "name": "Redirect",
        "hooks": {}
      },
      {
        "id": "apply-schema",
        "name": "Apply Schema",
        "version": "1.0.0"
      }
    ],
    "body": "{\"provider\":\"google\",\"callbackURL\":\"/dashboard\"}",
    "url": "http://localhost:3000/api/auth/sign-in/social",
    "headers": {},
    "signal": {}
  }
}
Solution
You can just call authClient.getSesion inside the onSuccess callback to grab user info, then pass it to your analytics APIs.
Was this page helpful?