Search code examples
authenticationmeteormeteor-accountsmeetup

Modifying user.profile using Accounts.onCreateUser for loginWithMeetup


When a user creates an account using the the Meetup login package, I want to get a few more values such as the user's full name and URL to their profile picture then store them in user.profile. Currently, I'm checking if the service is "meetup", performing the following GET request and trying to store it in user.profile.

if (service == "meetup") {
        var accessToken = user.services.meetup.accessToken;

        var request = Meteor.http.get('https://api.meetup.com/2/profiles',{
            params : {
                access_token : accessToken
            },
            headers: {"User-Agent": "Meteor/1.0"}
        });

        if(result.error){
            throw result.error;
        }

        profile = _.pick(request.results,
            'name',
            'photo_url'
        );

        user.profile = profile;

        return user;

    }

However, I'm getting an error when I try to create an account. Note that I am able to create an account if I were to remove the code under if (service == "meetup") albeit without the fields I need so I know the problem is in here. How can I obtain the user's full name and profile picture URL and store them under user.profile?

Many thanks in advance :)


Solution

  • OP here. Thank you for the other answers! I did manage to get the code to work. I tried a few different things so I didn't isolate what was causing the problem but I'm sure parts of it were that I was using Meteor.http.get instead of HTTP.get (as @sbking and @fuzzybabybunny suggested) and that I was using /2/profile/ instead of /2/member in the request (Thank you @mlc).

    In the end, I decided to use an API key from an empty Meetup account I created instead of an access token like before.

    Here is the code:

    if (service == "meetup") {
            var meetupId = user.services.meetup.id;
            var target = 'https://api.meetup.com/2/member/' + meetupId + '?key=' + MEETUP_API_KEY + '&signed=true';
    
            var result = HTTP.get(target, {
                params: {
                    format: 'json'
                }
            });
    
            var userProperties = result.data;
    
            options.profile = {
                'name': userProperties.name,
                'link': userProperties.link,
                'bio': userProperties.bio,
                'picture': userProperties.photo.photo_link,
                'id': meetupId
            };
    
            user.profile = options.profile;
    
            return user;
    
        }
    

    The above code doesn't validate the returned data before storing it, which it will in its final form.

    I hope this helps anyone with a similar question.