Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passport and express #105

Closed
zabojad opened this issue Jan 19, 2016 · 1 comment
Closed

Passport and express #105

zabojad opened this issue Jan 19, 2016 · 1 comment

Comments

@zabojad
Copy link

zabojad commented Jan 19, 2016

Hi !

I'm trying to attach a passport strategy to express routes.

In js, it would look like this:

File auth.js:

var passport = require('passport');
var BasicStrategy = require('passport-http').BasicStrategy;
var User = require('../models/user');

passport.use(new BasicStrategy(
  function(username, password, callback) {
    User.findOne({ username: username }, function (err, user) {
      if (err) { return callback(err); }

      // No user found with that username
      if (!user) { return callback(null, false); }

      // Make sure the password is correct
      user.verifyPassword(password, function(err, isMatch) {
        if (err) { return callback(err); }

        // Password did not match
        if (!isMatch) { return callback(null, false); }

        // Success
        return callback(null, user);
      });
    });
  }
));

exports.isAuthenticated = passport.authenticate('basic', { session : false });

Main.js:

var passport = require('passport');
var authController = require('./controllers/auth');

mongoose.connect('mongodb://localhost:27017/beerlocker');

var app = express();

app.use(bodyParser.urlencoded({
  extended: true
}));

app.use(passport.initialize());

var router = express.Router();

router.route('/beers')
  .post(authController.isAuthenticated, beerController.postBeers)
  .get(authController.isAuthenticated, beerController.getBeers);

Here is my try to do something similar with haxe:

    expressSrv = new js.npm.Express();

    function isAuthenticated(req : Request, res : Response) : Void {
trace('isAuthenticated triggered');
        js.npm.Passport.authenticate('basic', { session : false });
    }

    function initExpressRoutes() : Void {

        var r = new Router();

        r.route('/beers')

            .post(isAuthenticated, postBeerAction)

            .get(isAuthenticated, getBeersAction);

        js.npm.Passport.use(new js.npm.passport.BasicStrategy(

            function(email, password, cb) {

                    trace('basic strategy triggered!');

                    app.userDao.md.findOne({ email: email }, 

                        function (err, user) {

                                if (err != null) {

                                    cb(err);
                                    return;
                                }

                                if (user == null) {

                                    cb(null, false);
                                    return;
                                }

                                user.verifyPassword(password, 

                                    function(err, isMatch) {

                                            if (err != null) {

                                                cb(err);
                                                return;
                                            }

                                            if (!isMatch) {

                                                cb(null, false);
                                                return;
                                            }

                                            cb(null, user);
                                        });
                            });
                }
            ));

        r.use(js.npm.Passport.initialize());
        r.use(BodyParser.json());

        expressSrv.use(r);
    }

And it might not be the right way to do it as calls hangs on the isAuthenticated method...

What am I doing wrong ?

@zabojad
Copy link
Author

zabojad commented Jan 19, 2016

I've found my mistake ^^... Again, I'll document it later...

@zabojad zabojad closed this as completed Jan 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant