Context.identity null in localhost

Hi there

I’m using Netlify CLI (netlidy dev) to run my local environment.

I’m using GitHub - netlify/gotrue-js: JavaScript client library for GoTrue for user management and I’m trying to run an admin method to get a list of all the users. I’m using identity within context to do so.

In production, it works just fine, but in localhost it doesn’t - identity returns null.

How do I fix this?

Paul

Hiya, sorry you are having trouble with your build.

This Support Guide contains a ton of useful debugging tips that can likely help you solve your problem :slight_smile: Additionally, this Support Guide houses all of our resources for debugging build and deploy issues.

We also recommend trying to search the forums with the build error you encountered - it’s likely your question was already asked by someone else!

If you are still having problems, please provide as much information as you can on what you have already tried, what your build settings are, your package.json, etc. Thanks!

Unfortunately this guide doesn’t address the issue I have so my question remains.

As we cannot see what you are building locally, are you able to provide a reproduction for us?

I’m simply calling a notify function and console.log the context: you can see that identify is null. This is on localhost. When it’s live on production, identity is there and filled in.

exports.handler = async function (event, context) {
  console.log(JSON.stringify(context, null, 2))
  return {
    statusCode: 200,
    body: 'Success'
  }
}

Screenshot 2022-10-30 at 17.56.42

Feel free to follow:

So this isn’t possible at the moment?

It is, if you set a custom JWT secret in your site settings (requires a Business plan as of today). Then, you can set the same JWT secret in netlify.toml and it would work. This is only needed if you wish to test CLI using the same production Identity instance. If you simply want to test the flow, you can set a random JWT secret and that should work as long as you generate your own JWTs with that signature.

I got it to work to get the token in a local environment BUT when I call admin functions of the Gotrue API like listusers (${identity.url}/admin/users?per_page=500), I get a 401 error. That same codebase is working in prod to list all users.

Not sure what to do next.

Hiya @phb71

Can you tell me: are you making that call from a Netlify function?

As far as I know, you’d only have administrative permissions to use GoTrue API calls like this from inside a Netlify Lambda function running on the same site (versus you doing something like a direct curl of the URL you mention - https://sitename.com/.netlify/identity/admin/users from your own machine).

This doc describes that a bit: Functions and Identity | Netlify Docs