I create this a Bun application with basic authentication using hono
. The application includes a public route ("/") and an authenticated route ("/auth/page") that requires a bearer token.
- Bun: A minimalistic web framework for Node.js.
- Hono: Middleware-based authentication for Bun.
- Node.js: A JavaScript runtime built on Chrome's V8 JavaScript engine.
Make sure you already installed Node.js.
-
Clone this repository:
git clone https://github.com/zoelabbb/bunauth.git && cd bunauth
-
Install dependencies:
npm install
The example includes a simple authentication mechanism using a bearer token. The token is defined in the token
variable within the index.ts
file. In a real-world scenario, you would replace this with a more secure and dynamic authentication mechanism.
Run the Bun application:
bun run src/index.ts
The application will be available at http://localhost:3000.
Open your browser or use a tool like curl / Postman / REST Client plugin VSCode
to access the public route:
curl http://localhost:3000
GET http://localhost:3000
You should receive a JSON response:
TTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Date: Fri, 01 Dec 2023 16:29:09 GMT
Content-Length: 124
{
"message": "Basic auth using Bun and Hono",
"author": "Alif Ryuu BR",
"github": "https://github.com/zoelabbb",
"version": "1.0.0"
}
To access the authenticated route, you need to include the bearer token in the request header.
curl -H "Authorization: Bearer iloveprogramming" http://localhost:3000/auth/page
You should receive a plain text response:
Auth page : Your token is valid
GET http://localhost:3000/auth/page
Authorization: Bearer iloveprogramming
You should receive a response:
HTTP/1.1 200 OK
content-type: text/plain;charset=utf-8
Date: Fri, 01 Dec 2023 16:41:15 GMT
Content-Length: 31
Auth page : Your token is valid
Feel free to customize the authentication mechanism, routes, or any other aspects of the application according to your requirements. Explore the hono
documentation for more advanced authentication options and features.
src/
: Contains the source code.index.ts
: The main application file.
node_modules/
: The directory where npm packages are installed.package.json
: Project configuration file.tsconfig.json
: TypeScript configuration file.
To install dependencies:
bun install
To run:
bun run index.ts
This project was created using bun init
in bun v1.0.14. Bun is a fast all-in-one JavaScript runtime.