passport-gitlab2 icon indicating copy to clipboard operation
passport-gitlab2 copied to clipboard

Function after passport.use not working

Open Bertieio opened this issue 8 years ago • 3 comments

I am unable to get the

function(accessToken, refreshToken, profile, cb) {
       console.log(profile);
   }

to fire when authenticating

passport.use(new gitlabAuth({
        clientID: info.gitlabID,
        clientSecret: info.gitlabSecret,
        callbackURL: "http://bertie.io:"+opts.port+"/auth/gitlab/callback",
        baseURL: "http://git.bertie.io"
    },function(accessToken, refreshToken, profile, cb) {
        console.log(profile);
    }
));

Full code

var passport = require("passport");
var gitlabAuth = require("passport-gitlab2");
 var info = require("./info.js");
var express = require("express");
var morgan = require('morgan');

var opts = {};
opts.port = 3000;
var app = express();
app.use(morgan('common'));
var admin = express.Router();
app.use(require('serve-static')(__dirname + '/../../public'));
app.use(require('express-session')({
    secret: 'keyboard cat',
    resave: true,
    saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());

console.log("Server running on port: " + opts.port);

passport.use(new gitlabAuth({
        clientID: info.gitlabID,
        clientSecret: info.gitlabSecret,
        callbackURL: "http://bertie.io:"+opts.port+"/auth/gitlab/callback",
        baseURL: "http://git.bertie.io"
    },function(accessToken, refreshToken, profile, cb) {
        console.log(profile);
    }
));

app.get('/', function(req, res) {
    res.send('Hello World')
})

app.get('/auth/gitlab', passport.authenticate('gitlab', {scope: ['api']}));


function gitLabTest(res) {
  var gitlab = require('gitlab')({
  url:   'http://git.bertie.io',
  token: info.pvTest
});

// Listing users
gitlab.issues.all(function(issues) {
for(var i = 0; i < issues.length; i++){
  if (issues[i].author.id = 2) {
    console.log("#" + issues[i].id + ": " + issues[i].project_id + ", " + issues[i].author.name);
  }
}
//   for (var i = 0; i < users.length; i++) {
//     console.log("#" + users[i].id + ": " + users[i].email + ", " + users[i].name + ", " + users[i].created_at);
//   }
// });
//
// // Listing projects
// gitlab.projects.all(function(projects) {
//   for (var i = 0; i < projects.length; i++) {
//     console.log("#" + projects[i].id + ": " + projects[i].name + ", path: " + projects[i].path + ", default_branch: " + projects[i].default_branch + ", private: " + projects[i]["private"] + ", owner: " + projects[i].owner.name + " (" + projects[i].owner.email + "), date: " + projects[i].created_at);
//   }
 });
}



  app.get('/auth/gitlab/callback', function(req, res) {
    gitLabTest(res);
});

app.listen(3000)


//Connection

Bertieio avatar Feb 17 '17 11:02 Bertieio

Hi @Bertieio

You're code looks good from what I can tell so far. I also couldn't reproduce this issue (of course with custom clientId, clientSecret and baseUrl).

Do you might have any more details about the error:

  • What happens to the user? Does he see any errors in the browser? Is the OAuth2 round-trip successful (incl. callback)?
  • Are there any other errors / exceptions?
  • Are you executing your script behind a firewall or proxy?

The callback should actually be invoked, as soon as the callback URL is triggered.

Cheers

fh1ch avatar Feb 19 '17 09:02 fh1ch

Thanks for the reply @fh1ch

  • Everything else works as it should
  • There are no other errors I can see.
  • Its running on a VPS There shouldnt be anything stopping it, port 3000 is open.

Bertieio avatar Feb 19 '17 22:02 Bertieio

EDIT: Just saw the closed issue #9 and double checked my version, I'm using passport-gitlab2 4.0.0 so this issue might still remain (or it's my implementation)

I have the same issue (more or less)

my passport.use function:

passport.use(
    new gitlabStrategy({
        clientID: keys.gitlab.clientID,
        clientSecret: keys.gitlab.clientSecret,
        callbackURL: "/auth/gitlab/redirect",
        baseURL: "https://redacted.domain.com/"
    }, function(accessToken, refreshToken, profile, cb) {
        //callback, seems to be broken
        console.log(profile);
  })
);

here are my routes:

// Authenticate with gitlab
router.get("/gitlab", passport.authenticate('gitlab', {
    scope: ['openid']
}));

// callback redirect from gitlab authentication
// WARNING: the passport.authenticate() as middleware crashes with InternalOAuthError: Failed to fetch user profile
router.get('/gitlab/redirect', passport.authenticate('gitlab'), function (req, res) {
    res.send('you reached the gitlab callback route') //demo
});

It works as long as i don't use passport.authenticate('gitlab') on my callback route.

Here is the error I get both in browser and in terminal when it fails:

InternalOAuthError: Failed to fetch user profile
    at /home/SpaceLenore/Documents/git/myProject/node_modules/passport-gitlab2/lib/strategy.js:82:19
    at passBackControl (/home/SpaceLenore/Documents/git/myProject/node_modules/oauth/lib/oauth2.js:132:9)
    at IncomingMessage.<anonymous> (/home/SpaceLenore/Documents/git/myProject/node_modules/oauth/lib/oauth2.js:157:7)
    at IncomingMessage.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1092:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)

Server running on my localhost and no issue while going back and fourth between gitlab has been detected. It works without the callback to passport as middleware.

Hopes this information helps resolve the issue, best regards SpaceLenore

SpaceLenore avatar Sep 14 '18 21:09 SpaceLenore