irom
irom

Reputation: 3596

node active directory authenticate and group membership

Here I'm using nodejs module activedirectory and the problem is that async function ad.getGroupMembershipForUser returns groupname later than I return it from ad.authenticate. Not sure how to write callback here..

ad.authenticate(username, password, function (err, isAuthenticated) {
            if (err) return done(err, null);
            if (isAuthenticated) {
                var groupname='';
                ad.getGroupMembershipForUser(username, function(err, groups) {
                    if (err) {
                        console.log('ERROR: ' +JSON.stringify(err));
                        return;
                    }
                    if (! groups) return done(null, false);
                    else {
                        groups.forEach(function (group) {
                            if (config.group.indexOf(group.cn)>=0)
                            groupname=group.cn;

                        });
                    }
                });
                console.log('return',groupname); // output is 'return' !!!
                if (groupname) return done(null, {
                    username: username
                })
            }
            else {
                return done(null, false);
            }
        });

Upvotes: 0

Views: 1044

Answers (1)

Justin
Justin

Reputation: 885

Put the return into the callback:

ad.authenticate(username, password, function (err, isAuthenticated) {
        if (err) return done(err, null);
        if (isAuthenticated) {
            var groupname='';
            ad.getGroupMembershipForUser(username, function(err, groups) {
                if (err) {
                    console.log('ERROR: ' +JSON.stringify(err));
                    return;
                }
                if (! groups) return done(null, false);
                else {
                    groups.forEach(function (group) {
                        if (config.group.indexOf(group.cn)>=0)
                        groupname=group.cn;

                    });
                }

               console.log('return',groupname); // output is 'return' !!!
               if (groupname) return done(null, {
                 username: username
               })

           });

        }
        else {
            return done(null, false);
        }
    });

Upvotes: 1

Related Questions