Coder Social home page Coder Social logo

Comments (9)

Eomm avatar Eomm commented on May 31, 2024 1

I don't think it is a matter of preHandler or hooks because the target is the input array functions.

This plugin returns a function that could be attached to any hooks in fastify:

function _auth (request, reply, done) {

But the problem is how this array is managed:

obj.functions = this.functions

So I think you need to find if the array element is a function or promise and then if it is a function nothing change, but if it is a promise you need to wait for it an then call the that.onAuth to continue the workflow.

func(that.request, that.reply, that.onAuth)

from fastify-auth.

Eomm avatar Eomm commented on May 31, 2024

You are right:

func(that.request, that.reply, that.onAuth)

We accept only functions as input and not Promise.

Would you like to work on a PR to add this feature?

from fastify-auth.

PavelPolyakov avatar PavelPolyakov commented on May 31, 2024

Sure, let's see what I can do. I believe such mechanism is already present in fastify, if you know on top of your head where to find it - please share. Otherwise I'll find it during next week work days.

from fastify-auth.

SkeLLLa avatar SkeLLLa commented on May 31, 2024

Could you please add some docs/details to this PR? Because I was able to use async functions before it successfully. And after it I'm getting FST_ERR_REP_ALREADY_SENT error, because route handler code called twice.

Example code I've used before as auth function:

const verifyFn = async (req, res, done) => {
  // do something then call done
  done(err); // if there's an error
  done(); // if there's no error
}

and everything worked fine.

As far as I understood it just check if auth function returns Promise, and if it does, then it allows access if no errors were thrown. However it omits checking if done is provided.

from fastify-auth.

PavelPolyakov avatar PavelPolyakov commented on May 31, 2024

hi @SkeLLLa,

I believe the key issue is, that now it should be either async or callback. If your auth function is async, then it's treated differently:
https://github.com/fastify/fastify-auth/blob/master/fastify-auth.js#L64

from fastify-auth.

SkeLLLa avatar SkeLLLa commented on May 31, 2024

Yes, I understand. But that info should be described in readme or docs. Now we have:

fastify
.decorate('verifyJWTandLevel', function (request, reply, done) {
    // your validation logic
    done() // pass an error if the authentication fails
  })

The most obvious way to do something async is just:

.decorate('verifyJWTandLevel', async function (request, reply, done) {
    // your async stuff
    done() // pass an error if the authentication fails
  })

And this PR adds logic that contradicts current documentation and current typings https://github.com/fastify/fastify-auth/blob/master/fastify-auth.d.ts#L24 (as you can see done callback is not optional) and AuthFunction type is the function that returns void, not Promise.

from fastify-auth.

mcollina avatar mcollina commented on May 31, 2024

Typings is an afterfact. This library is not written in typescript, and the JS is the source of truth.

The API should either be:

.decorate('verifyJWTandLevel', async function (request, reply) {
    // your async stuff
  })

or

.decorate('verifyJWTandLevel', function (request, reply, done) {
    // your validation logic
    done() // pass an error if the authentication fails
  })

The typescript definition should be updated.

from fastify-auth.

SkeLLLa avatar SkeLLLa commented on May 31, 2024

@mcollina The JS source of truth is good, but I think it will be better if that truth come to readme as well :)

from fastify-auth.

mcollina avatar mcollina commented on May 31, 2024

That is indeed true! PR are always welcomed!

from fastify-auth.

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.