Support Forums

How to pass the Authorization Bearer Token to the identity widget?


I’m having issues of finding an example of how to pass Authorization Bearer token to my netfliy-identity-widget when the user logs in.

Reason I want to do that is to read the user from the clientContext in my identity-login function

Hi @vorci,

Could you elaborate on what you mean? You don’t have to pass any Bearer Token to the widget. I think you mean, you wish to get the bearer token from the widget. If that’s the case, you don’t need Authorization header could do it something like:

exports.handler = async event => {
  // rest of the stuff

event.body.user will automatically consist of the User object. If you want to do something with the Auth token, you’d have to do something like:

netlifyIdentity.on('login', user => {
  fetch('/.netlify/functions/postLogin/, {
    headers: {
      Authorization: 'Bearer ' + user.token.access_token,
  }).then(response => {
    if (respose.ok) {
      return response.json()
    } else {
      throw response.statusText
  }).then(data => {
  }).catch(error => {

If you submit the data this way, then in your custom serverless function, you could do something like:

exports.handler = async (event, context) => {
  const {identity, user} = context.clientContext;
  // Do stuff and return a response...

Hi, thanks for the reply!

What I mean is found on following link :

The user object is present if the function request has an Authorization: Bearer <token> header with a valid JWT from the Identity instance. In this case the object will contain the decoded claims.

Right now when I try to log the clientContext with identity-login function it has no user property

export function handler(event, context, callback) {

    const { user } = context.clientContext;

    return callback(null, {
        statusCode: 200,
        body: JSON.stringify({status:"OK"})

And the log will be something like

  custom: {
    netlify: 'xxx'
  identity: {
    url: 'xxx',
    token: 'xxx'

I assume the reason is what is found on the link I’ve attached above

Yes, as I said, if you wish to pass the Authorization header, you’d have to create a different function which you can manually trigger with the login callback of the widget.

1 Like

Thank you ! That clarifies things.

So when we want to do something with user propertiy post login the convention is to create a custom callback and use that instead of identity-login?

Well, you do get the user object in identity-login function too. That’s what I’ve shown in my first example (event.body.user). You also get the identity admin token in that function, so you could use your GoTrueJS Admin methods on the user even in that.

I don’t see a reason why you’d need to create a custom function, but yes, a custom function would be needed only if you need the Bearer token specifically. If you can work without it, the identity-login function does include the user object on which you can work.

Understood , thanks!
I think there’s just one small correction its event.user not event.body.user.

Hi @vorci,

Are you sure? I really think it’s event.body.user. That’s how I got it working and it even says so in the docs:

The payload is in the body of the event.

Yeah event.body is undefined, event.user is not.
I’ve tried with both