GithubHelp home page GithubHelp logo

Comments (9)

darkgl0w avatar darkgl0w commented on May 10, 2024 1

Hello.

@julie-ng can you try with this piece of code :

const PORT = process.env.port || 3000

const fastify = require('fastify')({ logger: true })
const fastifyPassport = require('fastify-passport')
const fastifySecureSession = require('fastify-secure-session')

const { Strategy: GitHubStrategy } = require('passport-github')
const strategyOpts = {
  clientID: process.env.GITHUB_CLIENT_ID,
  clientSecret: process.env.GITHUB_CLIENT_SECRET,
  callbackURL: 'http://127.0.0.1:3000/auth/github/callback'
}

const notForProductionKey = 'no-encryption-key-for-local-dev-min-32-bytes'
fastify.register(fastifySecureSession, { key: notForProductionKey })
fastify.register(fastifyPassport.initialize())
fastify.register(fastifyPassport.secureSession())

fastifyPassport.use('github', new GitHubStrategy(strategyOpts, function (accessToken, refreshToken, profile, cb) {
  console.log('**** Is this ever called??? *******') // <-- NEVER called in Fastify ⁉️
  console.log(profile) // <-- for testing only
  return cb(null, profile)
}
))


/**
 * Routes
 */

fastify.get('/', function (request, reply) {
  reply.send({
    hello: 'world',
    protected: false
  })
})

fastify.get('/protected', { onRequest: fastifyPassport.authenticate('github', { authInfo: false })}, (request, reply) => {
  reply.send({
    hello: 'protected',
    protected: true
  })
})

fastify.get('/auth/github/callback', { onRequest: fastifyPassport.authenticate('github', { authInfo: false })}, (request, reply) => {
  // reply.redirect('/')
  reply.send('called back')
})


/**
 * Run the Server
 */
fastify.listen(PORT, function (err, address) {
  if (err) {
    fastify.log.error(err)
    process.exit(1)
  }
})

It seems that the GitHub auth callback url must call fastifyPassport.authenticate() method too.

NB: I changed the hook from preValidation to onRequest in order to fail fast in the request lifecycle, but it should work as intended even with the preValidation hook. ^^

from fastify-passport.

airhorns avatar airhorns commented on May 10, 2024 1

Ah shoot right -- the only way that the passport handler is going to be able to get the user information is if it participates in the request where the user information comes back which is that callback URL! I missed that reading the example code. I feel like that must also be the case for express as well, no? Gonna close presuming yes but if not we can take a deeper look. Thanks @darkgl0w !

from fastify-passport.

airhorns avatar airhorns commented on May 10, 2024

Thanks for the reproduction! Offhand I am not sure why exactly this is happening, but, in my project we're using passport-github2 which is a maintained fork of passport-github, and it seems to work ok with fastify-passport! Want to try that one out?

from fastify-passport.

julie-ng avatar julie-ng commented on May 10, 2024

@airhorns I will try that and let you know.

I also just noticed I chose the original because according to passportjs, it was last updated 3 months ago…
Screen Shot 2022-02-17 at 15 39 40

but npm says actually 6 years ago
Screen Shot 2022-02-17 at 15 41 20

🤦‍♀️

from fastify-passport.

julie-ng avatar julie-ng commented on May 10, 2024

@airhorns I have the same problem, when swapping passport-github for passport-github2 🤷‍♀️

from fastify-passport.

julie-ng avatar julie-ng commented on May 10, 2024

Thanks @darkgl0w! That seems to work. Is there a reason why the callback needs it too? Is there something I missed in the documentation?

from fastify-passport.

darkgl0w avatar darkgl0w commented on May 10, 2024

No you didn't miss anything. It is the plugin documentation that needs some love. I have spotted some issues that are worth a PR when looking at this issue. 😝

In this particular case the plugin code itself is well commented. 😸

from fastify-passport.

darkgl0w avatar darkgl0w commented on May 10, 2024

@airhorns yes it is the case for express as well. I will submit a PR to add a note about this.

from fastify-passport.

julie-ng avatar julie-ng commented on May 10, 2024

No you didn't miss anything. It is the plugin documentation that needs some love. I have spotted some issues that are worth a PR when looking at this issue. 😝

In this particular case the plugin code itself is well commented. 😸

Yeah, documentation can really use from love. I find it detailed - but not in a good way that's easy to sort out, esp. since

  • { authInfo@ false } is never explained
  • And lines like this are just 😵‍💫 - so many params… and if I wanted to skip authInfo, could I? Or do I need an empty object? 🤔
    fastify.get(
      '/',
      { preValidation: fastifyPassport.authenticate('test', { authInfo: false }) },
      async (request, reply, err, user, info, status) => {

I'll try to figure out some of this, but it's a struggle right now.

If it's well documented in the code, you should consider using JSDoc to publish a docs site. And I'll shamelessly plug my own template here ;-) https://www.npmjs.com/package/tidy-jsdoc

Closing issue for now since original problem described is now solved.

from fastify-passport.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.