JWT Auth by @renaldyakb using Hono App
This my a few examples project to route some endpoint using my own middleware using #verfify
JWT from cookie store, I'm using Bun runtime & Supabase Database
POST /api/v1/auth/signup
POST /api/v1/auth/signin
DELETE /api/v1/auth/signout
// protected route
GET /api/v1/protect/user
It's a good idea before starting, install some dependencies first, because I use bun I will run it in the terminal in my directory folder with the command:
bun install
and set environment variables :
SUPABASE_URL=<your_supabase_url>
SUPABASE_KEY=<your_supabase_>
JWT_SECRET=secretkey123
-
is to make new user to
request body (JSON) :
{
"username" : "yourUsername",
"password" : "yourPassword",
"email" : "yourEmail@example.com",
}
-
to enter the application (basically), and save a
cookie
in the browser with the nameauth_token
request body (JSON)
{
"username" : "yourUsername",
"password" : "yourPassword",
}
-
To sign out is actually very simple, I just delete
cookies
in the browser with a function like the following
import { deleteCookie } from "hono/cookie";
deleteCookie(c, "auth_token");
-
The most crucial part is that I only authenticate using the JWT token which is already in the
cookie
with the key"auth_token"
I am very happy to be in the hono community and I hope I can also improve or improvise my example project, if there is input from all of you I would be very grateful, cheers 🥂