Could not access user from context inside netlify function!

I am trying to get my user from the clientContext inside my netlify function. But that returns null, I have read the documentation, but it has come no good. Can you share with me a code snippet what exactly I have to pass as headers and authorization parameters when I make a fetch call to my netlify function as an API from the site pages! Stuck for more than a week, hoping a in depth help from your side. Thanks!

Considering you’re using Netlify Identity Widget, in the client side, you can do:

const user = netlifyIdentity.currentUser()
fetch('/.netify/functions/<function-name>/', {
  headers: {
    Authorization: `Bearer ${user.token.access_token}`
  }
})

Then you can access the user in the function.


I did this.
But no help.
What’s wrong in here!

Do you have a repo I could check? By the looks of this code, it seems correct, but if you still say it’s not working, it’d need further investigation.

I’d be happy to have any simple test repo without any confidential data in it to check.

Feel free to check this one!
https://github.com/dasgupta002/netlify

Hi @dasgupta002,

Thanks for the repo. It seems like it’s working to me. Here’s what I got in my functions’ console:

INFO   {
  callbackWaitsForEmptyEventLoop: [Getter/Setter],
  succeed: [Function (anonymous)],
  fail: [Function (anonymous)],
  done: [Function (anonymous)],
  functionVersion: '$LATEST',
  functionName: '164d0769d4ca9c18ce26562dca3f9e408070c2d1c2b884621e91527e1724e77d',
  memoryLimitInMB: '1024',
  logGroupName: '/aws/lambda/164d0769d4ca9c18ce26562dca3f9e408070c2d1c2b884621e91527e1724e77d',
  logStreamName: '2021/08/03/[$LATEST]d3dabe643c01435bb985e7b0ae5821d0',
  clientContext: {
    custom: {
      netlify: 'eyJpZGVudGl0eSI6eyJ1cmwiOiJodHRwczovL2Fkb3JpbmctYWxiYXR0YW5pLWEyODkwNi5uZXRsaWZ5LmFwcC8ubmV0bGlmeS9pZGVudGl0eSIsInRva2VuIjoiZXlKaGJHY2lPaUpJVXpJMU5pSXNJblI1Y0NJNklrcFhWQ0o5LmV5SmxlSEFpT2pFMk1qYzVOekkxTVRRc0luTjFZaUk2SWpBaWZRLlJGSDVwRDlPUGZUa2lSb3RscXdRekJmbU1JWlF0QWFOVGNkYlVnX2N6WWMifSwidXNlciI6eyJhcHBfbWV0YWRhdGEiOnsicHJvdmlkZXIiOiJlbWFpbCJ9LCJlbWFpbCI6InRlc3RAdGVzdC5jb20iLCJleHAiOjE2Mjc5NzU5NjgsInN1YiI6Ijc3NWE1MzQ0LTBjNmQtNGRjNi05YWM3LWQyZDY0NmUwNzFlYyIsInVzZXJfbWV0YWRhdGEiOnsiZnVsbF9uYW1lIjoiVGVzdCBVc2VyIn19LCJzaXRlX3VybCI6Imh0dHBzOi8vYWRvcmluZy1hbGJhdHRhbmktYTI4OTA2Lm5ldGxpZnkuYXBwIn0='
    },
    identity: {
      url: 'https://adoring-albattani-a28906.netlify.app/.netlify/identity',
      token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2Mjc5NzI1MTQsInN1YiI6IjAifQ.RFH5pD9OPfTkiRotlqwQzBfmMIZQtAaNTcdbUg_czYc'
    },
    user: {
      app_metadata: [Object],
      email: 'test@test.com',
      exp: 1627975968,
      sub: '775a5344-0c6d-4dc6-9ac7-d2d646e071ec',
      user_metadata: [Object]
    }
  },
  identity: undefined,
  invokedFunctionArn: 'arn:aws:lambda:us-east-1:375708248955:function:164d0769d4ca9c18ce26562dca3f9e408070c2d1c2b884621e91527e1724e77d',
  awsRequestId: '2ee47065-afe2-433f-b88d-3280e25b5ae4',
  getRemainingTimeInMillis: [Function: getRemainingTimeInMillis]
}

I had logged this: console.log(context). As you can see, I am getting context.clientContext.user perfectly fine.

I modified some small things in your code (like changed the function URL from absolute to relative so that I can use the function on my website rather than yours), here’s my repo:

https://github.com/Hrishikesh-K/clientContext


My function logs!

That’s strange. Could you capture and submit your HAR file recordings?

https://drive.google.com/drive/folders/1vMqmVCJVqTSoW1t3vaRVVBKShdWXegXs?usp=sharing

Check this one!

Hi @dasgupta002,

This is getting tougher to crack. The Auth token is being perfectly sent and it’s a valid one as decoded on: https://jwt.io.

Could you try publishing my repo as a different website to see if something has gone wrong with your Identity instance?

Your code runs fine. No issues.
Can you tell me what did I mess up in my code?

Hi @dasgupta002,

I don’t think there’s anything wrong with your code as I haven’t made any (significant) changes. Maybe your Identity instance or the user is corrupted for some reason. I’ve never seen this happen, but that’s my best guess at the moment.

Is this happening only with a specific user? If yes, could you try deleting and re-creating that user?

If not, you can try deleting your Identity instance and recreating it (this will delete all users and data).

Finally, if that doesn’t help either, you’d have to create a new website and setup the user(s) in that.

Fingers crossed, I am trying to recreate my identity instance to be sure. Thanks for the help!

Sure, let us know if/what works from that or if it’s something else altogether that gets your job done.

I’m having a similar issue. Anyone else experiencing this?

Bumping my issue: clientContext.user is missing in from context.clientContex regular netlify function

Hey there, @zricethezav :wave:

Thanks for reaching out. We ask that folks keep questions that are related in one thread. We will respond as soon as we are able to in the thread that you have linked above. Thanks for your patience.

1 Like