简体   繁体   中英

how to get soundcloud username with passport.js

I'm working on a nodejs project. I'm using passport.js for my authentication, it works perfectly, but i'm in trouble when I try to get the username of some people using soundcloud.

here is my the part of my code I use to store the profile informations in my database :

function findOrCreateUser (accessToken, refreshToken, profile, done) {
    User.findOne({ oauthID: profile.id }, function(err, user) {
        if(err) console.log(err); 
        if (!err && user != null) done(null, user);
        else {
            var user = new User( {
                oauthID :   profile.id,
                name :      profile.displayName,
                created :   Date.now(),
                pwd :       'no-pwd',
                mail :      '',
                path :      ''
            });
            // temporary path using mongo _id
            user.path = user._id; 
            console.log( "familyName " + profile.familyName  ); // undefined
            console.log( "givenName " + profile.givenName  ); // undefined
            console.log( "middleName " + profile.middleName  ); // undefined
            console.log("displayName "+ profile.displayName  ); // empty if the user didn't fill the field 

            if (profile.displayName == '') user.name = "Jacky Chan";
            // if email exists...(i cant access it on soundcloud but i don't really need it 
            if(typeof(profile.emails) != 'undefined') user.mail = profile.emails[0].value;

            user.save(function(err) {
                if(err) {
                    console.log(err);
                }
                else {
                    console.log("saving user " + user.name );
                    done(null, user);
                };
            });
        };
    });
}

OK so everything is working fine here, profile.displayName return a String according to passport.js documentation . The problem is that this String is obtained with the First Name and the Last Name of the user that are not required, so it's often empty. the only required field is the username.

this picture illustrates it: soundcloud用户设置

So my question is :

how to get the username using passport.js ?

any help very appreciated! :)

Solved!! I had to modify the passeport-soundcloud middleware At line 88 I replaced "full_name" by "username" referring to soundcloud API documentation

:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM