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

Issues Authenticating specific Routes #1

Closed
SimeonC opened this Issue Apr 15, 2014 · 2 comments

Comments

Projects
None yet
2 participants
@SimeonC

SimeonC commented Apr 15, 2014

Hi There. I've been trying to get this working in my project, I've got everything in the examples working, ie I can login via Auth0 and it redirects back to my callback page. My problem is that the authentication doesn't seem to work for routes - it always 302's to the login page.

passport = require 'passport'
Auth0Strategy = require 'passport-auth0'
security = require('./security')

strategy = new Auth0Strategy 
    domain:       security.auth0.domain
    clientID:     security.auth0.clientID
    clientSecret: security.auth0.clientSecret
    callbackURL:  '/callback'
, (accessToken, refreshToken, profile, done) ->
    console.log "Hit Auth0 strategy"
    done null, profile

passport.use strategy

# This is not a best practice, but we want to keep things simple for now
passport.serializeUser (user, done) -> done null, user

passport.deserializeUser (user, done) -> done null, user

app.configure ->
    app.use express.logger 'dev'
    app.set 'views', "#{__dirname}/views"
    app.engine 'jade', require('consolidate').jade
    app.set 'view engine', 'jade'

    app.use helmet.xframe(), helmet.iexss(), helmet.contentTypeOptions(), helmet.cacheControl()
    app.use express.json(), express.urlencoded()
    app.use express.methodOverride()
    app.use express.cookieParser()
    app.use express.session
        secret: security.cookieSecret
    app.use passport.initialize()
    app.use passport.session()

# Auth0 callback handler
app.get '/callback', passport.authenticate('auth0',
    failureRedirect: '/login'
), (req, res) ->
    if not req.user then throw new Error 'user null'
    console.log "hit callback"
    console.log req.user
    res.redirect "/first-login"

# This get when redirected to from the callback ALLWAYS 302's back to the login page...
app.get '/first-login', passport.authenticate('auth0'), (req, res) -> res.render 'first-login.jade'

Have I done something wrong? As far as I was aware this is the correct way of authenticating routes according to passport.js so I figured it was possibly an issue with passport-auth0.

@woloski

This comment has been minimized.

Show comment
Hide comment
@woloski

woloski Apr 16, 2014

Member

You should check req.isAuthenticated() as shown here

http://stackoverflow.com/questions/14188834/express-passport-where-is-the-documentation-for-ensureauthentication-isaut

On Tue, Apr 15, 2014 at 8:29 PM, SimeonC notifications@github.com wrote:

Hi There. I've been trying to get this working in my project, I've got everything in the examples working, ie I can login via Auth0 and it redirects back to my callback page. My problem is that the authentication doesn't seem to work for routes - it always 302's to the login page.

passport = require 'passport'
Auth0Strategy = require 'passport-auth0'
security = require('./security')
strategy = new Auth0Strategy 
  domain:       security.auth0.domain
  clientID:     security.auth0.clientID
  clientSecret: security.auth0.clientSecret
  callbackURL:  '/callback'
, (accessToken, refreshToken, profile, done) ->
  console.log "Hit Auth0 strategy"
  done null, profile
passport.use strategy
# This is not a best practice, but we want to keep things simple for now
passport.serializeUser (user, done) -> done null, user
passport.deserializeUser (user, done) -> done null, user
app.configure ->
  app.use express.logger 'dev'
  app.set 'views', "#{__dirname}/views"
  app.engine 'jade', require('consolidate').jade
  app.set 'view engine', 'jade'
  app.use helmet.xframe(), helmet.iexss(), helmet.contentTypeOptions(), helmet.cacheControl()
  app.use express.json(), express.urlencoded()
  app.use express.methodOverride()
  app.use express.cookieParser()
  app.use express.session
      secret: security.cookieSecret
  app.use passport.initialize()
  app.use passport.session()

# Auth0 callback handler
app.get '/callback', passport.authenticate('auth0',
  failureRedirect: '/login'
), (req, res) ->
  if not req.user then throw new Error 'user null'
  console.log "hit callback"
  console.log req.user
  res.redirect "/first-login"
# This get when redirected to from the callback ALLWAYS 302's back to the login page...
app.get '/first-login', passport.authenticate('auth0'), (req, res) -> res.render 'first-login.jade'

Have I done something wrong? As far as I was aware this is the correct way of authenticating routes according to passport.js so I figured it was possibly an issue with passport-auth0.

Reply to this email directly or view it on GitHub:
#1

Member

woloski commented Apr 16, 2014

You should check req.isAuthenticated() as shown here

http://stackoverflow.com/questions/14188834/express-passport-where-is-the-documentation-for-ensureauthentication-isaut

On Tue, Apr 15, 2014 at 8:29 PM, SimeonC notifications@github.com wrote:

Hi There. I've been trying to get this working in my project, I've got everything in the examples working, ie I can login via Auth0 and it redirects back to my callback page. My problem is that the authentication doesn't seem to work for routes - it always 302's to the login page.

passport = require 'passport'
Auth0Strategy = require 'passport-auth0'
security = require('./security')
strategy = new Auth0Strategy 
  domain:       security.auth0.domain
  clientID:     security.auth0.clientID
  clientSecret: security.auth0.clientSecret
  callbackURL:  '/callback'
, (accessToken, refreshToken, profile, done) ->
  console.log "Hit Auth0 strategy"
  done null, profile
passport.use strategy
# This is not a best practice, but we want to keep things simple for now
passport.serializeUser (user, done) -> done null, user
passport.deserializeUser (user, done) -> done null, user
app.configure ->
  app.use express.logger 'dev'
  app.set 'views', "#{__dirname}/views"
  app.engine 'jade', require('consolidate').jade
  app.set 'view engine', 'jade'
  app.use helmet.xframe(), helmet.iexss(), helmet.contentTypeOptions(), helmet.cacheControl()
  app.use express.json(), express.urlencoded()
  app.use express.methodOverride()
  app.use express.cookieParser()
  app.use express.session
      secret: security.cookieSecret
  app.use passport.initialize()
  app.use passport.session()

# Auth0 callback handler
app.get '/callback', passport.authenticate('auth0',
  failureRedirect: '/login'
), (req, res) ->
  if not req.user then throw new Error 'user null'
  console.log "hit callback"
  console.log req.user
  res.redirect "/first-login"
# This get when redirected to from the callback ALLWAYS 302's back to the login page...
app.get '/first-login', passport.authenticate('auth0'), (req, res) -> res.render 'first-login.jade'

Have I done something wrong? As far as I was aware this is the correct way of authenticating routes according to passport.js so I figured it was possibly an issue with passport-auth0.

Reply to this email directly or view it on GitHub:
#1

@SimeonC

This comment has been minimized.

Show comment
Hide comment
@SimeonC

SimeonC Apr 16, 2014

Thanks, that was exactly what I was looking for - why none of the blogs/notes or my google searches turned this up mystifies me completely!

For others who find this, to fix the issue I changed my last line to:

authFunc = (req, res, next) -> if req.isAuthenticated() then next() else res.redirect '/login'
app.get '/first-login', authFunc, (req, res) -> res.render '/first-login'

SimeonC commented Apr 16, 2014

Thanks, that was exactly what I was looking for - why none of the blogs/notes or my google searches turned this up mystifies me completely!

For others who find this, to fix the issue I changed my last line to:

authFunc = (req, res, next) -> if req.isAuthenticated() then next() else res.redirect '/login'
app.get '/first-login', authFunc, (req, res) -> res.render '/first-login'

@SimeonC SimeonC closed this Apr 16, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment