Skip to content

v-hono/v-hono-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hono_auth

Authentication and authorization library for v-hono-core framework.

Features

  • JWT (JSON Web Token) authentication with HS256/384/512 algorithms
  • Bearer token authentication
  • User session management
  • Role-based permission system
  • Menu/permission tree building

Installation

v install --git https://github.com/v-hono/v-hono-core
v install --git https://github.com/v-hono/v-hono-auth

Usage

JWT Authentication

import hono
import hono_auth
import hono_middleware

fn main() {
    mut app := hono.Hono.new()
    secret := 'your-secret-key'

    // Protect routes with JWT middleware
    app.use('/api/*', hono_auth.jwt_middleware(hono_auth.JwtOptions{
        secret: secret
        alg: .hs256
    }))

    app.get('/api/profile', fn (mut c hono.Context) http.Response {
        payload := hono_auth.get_jwt_payload(c) or {
            return c.json('{"error":"No JWT payload"}')
        }
        return c.json('{"user":"${payload.sub}"}')
    })

    app.listen(':3000')
}

Bearer Token Authentication

import hono
import hono_auth

fn main() {
    mut app := hono.Hono.new()

    app.use('/api/*', hono_auth.bearer_auth(hono_auth.BearerAuthOptions{
        token: 'your-api-token'
    }))

    app.get('/api/data', fn (mut c hono.Context) http.Response {
        return c.json('{"data":"protected"}')
    })

    app.listen(':3000')
}

Dependencies

  • hono - Core framework
  • hono_middleware - Cookie support for JWT

License

MIT

About

Authentication and authorization module for v-hono, featuring JWT and Bearer Auth.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors