Core: Trying to integrate passport and req.connection is undefined

Created on 19 Sep 2016  路  11Comments  路  Source: adonisjs/core

screen shot 2016-09-19 at 7 58 03 pm

I have this piece of code currently in my server.js

'use strict'

/*
|--------------------------------------------------------------------------
| Http Server
|--------------------------------------------------------------------------
|
| Here we boot the HTTP Server by calling the exported method. A callback
| function is optionally passed which is executed, once the HTTP server
| is booted.
|
*/

const passport = require('passport')
// Configure passport to use Auth0
const strategy = require('./setup-passport')
// Session and cookies middlewares to keep user logged in
var cookieParser = require('cookie-parser')
var session = require('express-session')

const http = require('./bootstrap/http')
http(function () {
  const Event = use('Event')
  Event.fire('Http.start')
})

I have this file too set-up-passport.js

'use strict'

const passport = require('passport')
const Auth0Strategy = require('passport-auth0')

const strategy = new Auth0Strategy({
    domain:       'xxxxxx',
    clientID:     'xxxxx',
    clientSecret: 'xxxxxxx',
    callbackURL:  '/callback'
  }, function(accessToken, refreshToken, extraParams, profile, done) {
    // accessToken is the token to call Auth0 API (not needed in the most cases)
    // extraParams.id_token has the JSON Web Token
    // profile has all the information from the user
    return done(null, profile)
  });

passport.use(strategy)

// This is not a best practice, but we want to keep things simple for now
passport.serializeUser(function(user, done) {
  done(null, user)
});

passport.deserializeUser(function(user, done) {
  done(null, user)
});

module.exports = strategy

I feel there is something i am not doing right. If I was using express, there is an easy way to just enable "trust proxy" but here in AdonisJS, I changed the value of trustProxy to true in config/app.js but I still get confronted with that error.

If I can figure this out, then it will be easy for people to use third party authentication systems like Auth0 and the likes. @thetutlage please check this out. Thanks

All 11 comments

@unicodeveloper I don't understand the need of express-session and neither it has been used anywhere.

So, apart from that. I think the main issue is that request.connection keeps returning undefined. Is there a way i can set that value permanently?

AdonisJs request is a glued layer on top of Node.js request object. I believe you are trying to access the connection property on actual Node.js request object. Which is accessible using request.request.connection

Damn!!..Thank you for that. Aha, I can see the object now through request.request.connection. Now, these third party modules like passport-oauth2 try to access the request object directly making it return undefined. So do you think I need to create a middleware to reassign AdonisJS request like so:

request.connection = request.request.connection

or is there an easy way of making sure third party modules still access the Node.js request object directly?

@unicodeveloper So I believe passport itself is very much specific to Express.js. With AdonisJs I do not have any targets of making the HTTP layer compatible with Express.js. The lower level modules will still work fine with AdonisJs, since you can access the raw _request object_.

Also I am not sure how exactly passport works internally, and for now creating a middleware is the way to go.

Thank you @thetutlage . I really appreciate your time and effort in answering my questions. 馃殌

@unicodeveloper Sure! Closing the issue.

@unicodeveloper How do you connect passportjs with adonisjs?
Should i simply do auth.attempt() inside passportjs callback?

@unicodeveloper So I believe passport itself is very much specific to Express.js. With AdonisJs I do not have any targets of making the HTTP layer compatible with Express.js. The lower level modules will still work fine with AdonisJs, since you can access the raw _request object_.

Also I am not sure how exactly passport works internally, and for now creating a middleware is the way to go.

Where should the middleware be placed ?

is there any updateds about this point?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amrayoub picture amrayoub  路  4Comments

aligoren picture aligoren  路  4Comments

navdeepsingh picture navdeepsingh  路  3Comments

umaams picture umaams  路  3Comments

blendsoft picture blendsoft  路  3Comments