question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrapping a serverless function in `useRequireAuth` still allows you to execute the function without being authenticated

See original GitHub issue

We had a user on Discord who was wrapping his serverless function in useRequireAuth, but could still execute the function without any auth headers set. He was expecting that useRequireAuth would block any unauthenticated invocations. Which is totally understandable, as the documentation isn’t really clear here.

What useRequireAuth really does though, is it make all the other auth related functions available. Maybe it should really be called makeAuthAvailable()? Or withAuthAvailable() to copy React HOC naming conventions.

What you really need to do to block execution is

const myHandler = async (_event: APIGatewayEvent, _context: Context) => {
  requireAuth({ roles: 'admin' })

  return {
    statusCode: 200,
  }
}

export const handler = useRequireAuth({ handlerFn: myHandler, getCurrentUser })

So, wrap the handler in useRequireAuth() and then call requireAuth() first thing in your handler.

I do think the current implementation is the right one. We could block execution in useRequireAuth, but doing the current way is much more flexible. For example it allows the developer to have two execution paths through the handler - one for authenticated users and one for unauthenticated users.

@dthyresson Do you think we should just update the docs, or should we rename something? Or both? Or something else entirely?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
Tobbecommented, Apr 26, 2022

We decided on a call today to call the function withAuth

1reaction
Tobbecommented, Apr 21, 2022

@jtoar Thanks for your input. “Enriched” is used a bit in the tests for useRequireAuth. That’s where I got that from. But definitely open to other names.

https://github.com/redwoodjs/redwood/blob/34acbb5efe4f3d16447c5c08d6eea9c9d79fa6b0/packages/graphql-server/src/functions/__tests__/useRequireAuth.test.ts?rgh-link-date=2022-04-21T10%3A15%3A32Z#L141

Read more comments on GitHub >

github_iconTop Results From Across the Web

useRequireAuth React Hook - useHooks
Hooks are a feature in React that allow you use state and other React ... to view a page that should require them...
Read more >
Redwood v2.0.0 is now available! - Releases and Upgrade ...
Congratulations, you're on v2.0.0! But if you're deploying via baremetal or using auth in serverless functions, keep reading.
Read more >
React Hooks Best Practice #2 - Medium
Building your own Hooks lets you extract component logic into reusable functions. - Rules of Hooks. Only Call Hooks at the Top Level....
Read more >
Serverless Functions in Depth with AWS Amplify - YouTube
Twitch Live Stream - Learn how to build, deploy, update, delete & test serverless functions using AWS Amplify. We'll also learn how to...
Read more >
Protect Your App With Route Protection, Serverless Lambdas ...
In this video we create a React 18 app with Amplify with protected routes, and a custom authenticator for a Lambda function.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found