Netlify Identity for paid subscriptions

Hi,

That’s a good idea @parkeragee. @RobertBroersma, as an extension of Parker’s suggestion, you can have users signup for free and add custom data to the app_metadata via netlify functions later when they successfully make a purchase. You can update a user’s metadata in functions by doing something like this:

    import fetch from "node-fetch";

    exports.handler = async (event, context) => {
    const { identity, user } = context.clientContext;
    // you need to pass in the user ID, which you can do by passing in the users jwt
    // or by just adding it as a parameter to your request.
    // Another way is to make a request to search for the user by e-mail but that's slower
    // const userID = 
    const usersUrl = `${identity.url}/admin/users/${userID}`;
    const adminAuthHeader = "Bearer " + identity.token;

    const attributes = {
    app_metadata: {
    roles: ['test', 'user', 'member'],
    my_user_info: "this is user info that the user can't change from the UI",
    },
};

    try {
        return fetch(usersUrl, {
        method: "PUT",
        headers: { Authorization: adminAuthHeader },
        body: JSON.stringify(attributes)
        })
        .then(response => {
            return response.json();
        })
        .then(data => {
            console.log("Updated the user");
            console.log(JSON.stringify({ data }));
            return { statusCode: 204 };
        })
        .catch(e => {...};
        });
    } catch (e) {
        return e;
    }
    };

Note that I haven’t actually tested that function, but it should do what you want it to do.

2 Likes