Closed olance closed 3 years ago
@olance You are so correct that:
I completely agree with:
As a Redwood user, I don't think I should know about the global context, the authentication context and how to populate the current user data into the global context.
I'd like to see the behavior be much more like the web hook validation / verification:
const authContext = await getAuthenticationContext({ event, context })
await getCurrentUser( authContext[0], authContext[1], authContext[2] ),
Perhaps some thing like verifyEvent
but checks for the auth-provider
header and checks the token/cookie?
See: https://redwoodjs.com/docs/webhooks#how-to-receive-and-verify-an-incoming-webhook
@olance Maybe verifyAuth(role: ['admin'])
? And raise Auth/Forbidden error if not, and return result of getCurrentUser if verified?
Would that work?
@dthyresson sorry I hadn't seen your own issue! Maybe we should merge them?
However, I wonder why you'd go with a different function than requireAuth
?
I'm pretty sure there should be a way to make it work seamlessly within non-GraphQL serverless functions, it's just a matter of correctly populating the context.
Also I looked at the Webhook docs and I feel there's room for a much easier/developer friendlier approach 🙂
Scratch that, I thought function decorators were a thing in JS/TS :(
I'll try to write up a proposal!
@olance I'm also attempting to use requireAuth()
in a custom serverless function and saw how the context
was only created in the grapqhql function.
With the code you provided above, I'm able to use the updated global context if I "disable context isolation".
I did that in a wrapper function to ensure that I restore the environment variable, but I don't know if there will be any other effects from that settings.
I am coming up with a few ideas -- but, just out of curiosity @olance and @doowb -- if your function needs Redwood user auth, why use a serverless custom function over a GraphQL service? Is there something specific a function can do? Or is the function being used outside the web side?
Yeah in my case, I was planning on using this function from outside of the web side. From a Chrome/Firefox extension to be precise :)
I chose to change my approach here and indeed use a regular GraphQL service, mainly for the sake of not having to wait for changes in Redwood to be honest though. I guess in the end it shall work, and I don't think there would be an issue with sending GraphQL requests from an extension.
However, I don't think it invalidates this issue ^^
I though that might be one of the use cases.
And I agree that just using GraphQL auth doesn’t make this issue go away.
Going to plan out a few options on Monday to handle a few scenarios. Thanks.
I created a custom function and I need to check that it is called by an authenticated user. I naively added a
requireAuth
call at the top of my function's handler, but realized it doesn't work.What I have done
functions/extractContent/extractContent.js
:export const handler = async (event, context) => { requireAuth() ... }
authorization: Bearer
auth-provider: firebase
A few
console.log
later I could see that this time, my Firebase token was being read and decoded correctly, andcurrentUser
was set on the global context... however it seems there's something a bit fishy going on with the context, ascontext
does contain acurrentUser
key with correct values, butcontext.currentUser
returnsundefined
😐A few things here:
where
createFunctionHandler
would setup the appropriate context reading/populating, and then call the actual handler once it's done?context
name? In the plugins, I see there's aextendContext
function provided, but it does not exist as a exported function anyway AFAICT.