Auth0 Home Blog Docs

Login form additional data in Rules

Hi All,

If you want to send additional data during login and want to retrieve and store it in your app/user metadata, here’s how you can do it using Rules.

Send additional data inside the logon form and retrieve it using a Rule like below:

Regards
Karthik

Rule

    function (user, context, callback) 
    {
        let rememberMe = false;
        if (context.hasOwnProperty('request'))
        {
        let _request = context.request;
        if (_request.hasOwnProperty('query'))
        {
            let _query = _request.query;
            if (_query.hasOwnProperty('remember_me'))
            {
                let rememberMeParam = _query.remember_me;
                if (typeof rememberMeParam === 'boolean' && rememberMeParam) 
                {
                    rememberMe = true;
                }
            }
        }

        user.user_metadata.rememberMe = rememberMe;

        return auth0.users.updateUserMetadata(user.user_id, user.user_metadata)
        .then(function () { callback(null, user, context); });
    }

    return callback(null, user, context);
}

Thanks a lot for sharing it with the community. As we cannot mark the first message in the thread a solution I will reference you solution here in my reply:

SOLUTION by @ksubramaniam

 function (user, context, callback) 
    {
        let rememberMe = false;
        if (context.hasOwnProperty('request'))
        {
        let _request = context.request;
        if (_request.hasOwnProperty('query'))
        {
            let _query = _request.query;
            if (_query.hasOwnProperty('remember_me'))
            {
                let rememberMeParam = _query.remember_me;
                if (typeof rememberMeParam === 'boolean' && rememberMeParam) 
                {
                    rememberMe = true;
                }
            }
        }

        user.user_metadata.rememberMe = rememberMe;

        return auth0.users.updateUserMetadata(user.user_id, user.user_metadata)
        .then(function () { callback(null, user, context); });
    }

    return callback(null, user, context);
}
1 Like