Basic auth using Netlify Functions + environment variable

Has anyone tried to create a simple way of authentication using Netlify Functions checking a submitted password against one saved in an environment variable?

The logic is roughly like this:

  1. show user an input field to submit password
  2. use a function to get that password to “server” side
  3. check that password against an environment variable
  4. if password matches, redirect to index page
  5. if not, reject access to site

Any ideas how to go about this, please?

Not gonna lie - I could use netlify built-in auth, but I don’t need a $15/mo plan for this single site that is being used once in a blue moon.

Thank you

Hello @josefrichter, I’m not sure that would work since you wouldn’t be able to prevent someone from just accessing a page directly if they know the URL without using one of the three ways to prevent access to a page: basic-auth, site password and role-based redirects.

If you have some data in your function itself, you could return that data as json if you authenticate the request, but that’s probably as much as you would be able to do, I think.