Comments (9)
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:
Line 25 in ff39b87
But the problem is how this array is managed:
Line 31 in ff39b87
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.
Line 62 in ff39b87
from fastify-auth.
You are right:
Line 62 in ff39b87
We accept only functions as input and not
Promise
.
Would you like to work on a PR to add this feature?
from fastify-auth.
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.
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.
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.
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.
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.
@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.
That is indeed true! PR are always welcomed!
from fastify-auth.
Related Issues (20)
- default imports do not work with TypeScript HOT 5
- Ability to customize the decorator name HOT 3
- Fastify-auth throws and error after an asynchronous call is completed "TypeError: that.done is not a function" HOT 2
- ChainAlert: npm package release (1.2.0) has no matching tag in this repo
- Use new @fastify org dependencies
- Configure default relation HOT 2
- No plugin export in ESM HOT 6
- NPM package update HOT 4
- No overload matches for fastify plugin HOT 1
- Unable to use a Typescript generic for the Params property off of the request HOT 5
- Improvements for composite auth HOT 4
- Typescript bug when used in conjunction with "fastify-type-provider-zod".
- Typescript bug when used in conjunction with "fastify-type-provider-zod". HOT 4
- Type definitions as defined in the route schema get removed when using fastify auth HOT 2
- Extended Composite Auth in #217 is broken HOT 10
- statusCode dirty with relation or
- fastify.auth(functions: fastifyAuth.AuthFunction[]) is missing second parameter in type declaration HOT 3
- New parameter: run all HOT 1
- Provide a way to skip an auth handler HOT 10
- Coverage 100%
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
ð Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google â€ïž Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify-auth.