How do I access user data from identity-login?

My site: https://silly-parrot.netlify.app#

I have a function in identity-login.js

exports.handler = async function (event, context) {
    const { identity, user } = context.clientContext;
    return {
        statusCode: 200,
        body: JSON.stringify({
            message: 'Hello ???!'
        })
    }
};

I want to capture the user’s ID when they log in, but I have no idea how to go about it.

Is the function called whenever someone logs in?

Is this the code I need in my script to capture it?

 const response = await fetch(`/.netlify/functions/identity-login`).then(
                    response => response.json());

userName.innerText = JSON.stringify(response);

How should I call this code?

Can someone please help?

Yes, it’s automatically called.

The data exists in event.body.

You can’t. Identity or any event-based functions cannot be called externally. If you wish to call it yourself, you’d have to create a custom function (name it something else) and use Webhooks in identity section to automatically call those.

I have got around it by using

    var token = ''
    async function started() {
        logged_in = false
        netlifyIdentity.on('init', async user => {
            if(user) {
                token = user.token.access_token
                logged_in = true
            }
            console.log('a', logged_in, token)
        })

        netlifyIdentity.on('login', user => {
            if(user) {
                logged_in = true
                token = user.token.access_token
            }
            console.log('b', logged_in, token)
        })

        netlifyIdentity.on('logout', () => {
                token = ''
                logged_in = false
            console.log('c', logged_in, token)
        })

Is that what you meant?

So is the description of the function in the docs to show the data that we can access?

Did you want to access the user data in server-side or client-side. Because your current code seems to be useful only in client side.

At this stage I am happy with the client side. I wish to use the user’s id as a key to a database call eventually, but for now, this code serves my purposes

I’m not sure what else you need. This is my full page if that’s any help

<html>
    <head>
        <title>User auth</title>
        <script type="text/javascript" src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
    </head>
    <body>
        <div id="select-file">
            <p>Please login or sign up.</p>

            <div data-netlify-identity-menu></div>
    </div>
    <script>
        var token = ''
        async function started() {
            logged_in = false
            netlifyIdentity.on('init', async user => {
                if(user) {
                    token = user.token.access_token
                    logged_in = true
                }
                console.log('a', logged_in, token)
            })

            netlifyIdentity.on('login', user => {
                if(user) {
                    logged_in = true
                    token = user.token.access_token
                }
                console.log('b', logged_in, token)
            })

            netlifyIdentity.on('logout', () => {
                    token = ''
                    logged_in = false
                console.log('c', logged_in, token)
            })


        }
        started()
        </script>
    </body>
</html>