Netlify CLI - dev: How to test identity functions events correctly?

Hello! I setup netlify CLI and started a dev environment for developing. I want to test identity function events. On login, I want to add a metadata to a user. But, after walking in circles through the docs and github, I couldn’t test it out the function properly.

This is my identity-login.ts function:

import { Handler } from '@netlify/functions'

const handler: Handler = (_event, _context) => {
  const identity = _context.clientContext?.identity
  const user = _context.clientContext?.user

  console.log('Identity: ', identity)
  console.log('User: ', user)

  return {
    statusCode: 200,
    body: JSON.stringify({ user_metadata: { unique_secret: '123' } }),

export { handler }

I run the netlify dev in a terminal:

Then, in another terminal I call: netlify functions:invoke --name identity-login --identity

But the result is:

Both the identity and user that are supposed to be set are undefined. Am I doing something wrong? I thought that netlify would mock some data for these type of events. How can I test identity? I wish to set the metadata and then check if the data is there, so I can move on with testing other logic around it. So I need some test-user account.

context contains Identity related data only when called with the Authorization header.

identity-login function contains the Identity-related data in the event body.

Looks like you’re merging both of those concepts. Also, I don’t think you can successfully invoke event -driven functions by the functions:invoke command. You probably need to trigger the event for that function from the front end.

1 Like

Hmm… But when I login in the localhost frontend, the identity-login.ts function doesn’t trigger. Nothing is logged out in the terminal, neither I get any feedback for the response.

Hey @rafael-lua :wave:

The problem is probably that event-driven functions expect the data in event.body , but normal Identity-based functions (that are called with the Authorization header), have this data in context.clientContext . So, you’ll have to change the code to this:

const user = JSON.parse(_event.body).user

And invoke the function like:

netlify functions:invoke identity-login --identity --payload "{\"event\": \"login\", \"user\": {\"sub\": \"foo\"}}"

With the Authorization header technique, I believe the CLI would use a dummy user, but not in event-based case.

Could you give that a try and let us know if it works for you?


Thank you!

That does help me understand better my situation and how the functions workflow work.

It seems I was also missing the “async”, which caused some issue with the live function.