Hi Luke,
Welcome to RW!
First off, this is entirely possible with redwood, because we implemented custom JWT auth for our website, just without the username/password bit, instead using github for that portion of it. You sound like you know what you’re doing, so it should be no problem for you :), just need a little patience to understand the auth process.
Before we continue… I’ll forward on the same advice I got initially, maybe consider Auth0 unless you have a really good reason not to, because it’ll definitely be less maintenance. But incase you still want to, here’s a summary:
1. Create a GraphQL handler for your login form, according to Rob’s post above
This will let you post a username and password, check with your user table on the DB, and return a JWT token in the response. At this point you can save it in localStorage, or however you’d like to.
2. Start with the standard syntax for enabling auth in your app
https://redwoodjs.com/docs/authentication. For now pick one of the options, just to roll with it, before you write your custom handlers.
There’s a few little insights here that’s really important, in the JSX code there’s:
a) AuthProvider
- this is the component that will let the FE know whether you’re logged in or not. You’ll want to write a custom auth provider later, but for now let’s keep investigating.
b) In the JSX, notice how it says type="netlify"
or type="auth0"
. This is important because every graphql request it sends will have two headers for auth: auth-provider
which is this type, and Authorization
which is going to be your token. You need to set the type to “custom” or whatever you want so that in the api
you can handle parsing/verifying the token yourself instead of the built in providers
c) Notice the client={auth0}
prop? This is your authClient, whose job is to expose functions for logging in, logout, and getting your token from localStorage (among other things). You’ll need to write a custom auth client here.
There’s an old post I originally wrote here Custom github JWT Auth with Redwood Auth. Part 2 in the first post is where you can see simple implementations of both the AuthProvider
and a custom authClient
3. Wrtie your custom validator on the api side
You write this in src/lib/auth.js
in the getCurrentUser
function. The purpose of this is to validate the JWT that the graphql handler receives, and to return the user’s details so it can be used in services later.
Example in post 11 of the thread above
I hope this helps you get started!