zoelabbb / bunauth

BunAuth is a simple project that combines the power of Bun, a modern JavaScript runtime, and Hono, a minimalistic web framework for Node.js. This project serves as an illustrative example of implementing basic authentication using the Bearer Token method.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bun Authentication Basic

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.

Technologies Used

  • 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.

Installation

Make sure you already installed Node.js.

  1. Clone this repository:

    git clone https://github.com/zoelabbb/bunauth.git && cd bunauth
  2. Install dependencies:

    npm install

Usage

Configuration

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.

Running the Application

Run the Bun application:

bun run src/index.ts

The application will be available at http://localhost:3000.

Testing Authentication

Public Route ("/")

Open your browser or use a tool like curl / Postman / REST Client plugin VSCode to access the public route:

Using Curl

curl http://localhost:3000

Using plugin REST Client VSCode

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"
}

Authenticated Route ("/auth/page")

To access the authenticated route, you need to include the bearer token in the request header.

Use the following curl command as an example:

curl -H "Authorization: Bearer iloveprogramming" http://localhost:3000/auth/page

You should receive a plain text response:

Auth page : Your token is valid

Using REST Client VSCode

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

Customization

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.

Directory Structure

  • 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.

bunauth

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.

About

BunAuth is a simple project that combines the power of Bun, a modern JavaScript runtime, and Hono, a minimalistic web framework for Node.js. This project serves as an illustrative example of implementing basic authentication using the Bearer Token method.


Languages

Language:TypeScript 100.0%