Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rename all files to kebab case #32

Merged
merged 3 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/label-pr-based-on-paths.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
settings: '8c564b', // Brown
dependency: '2ca02c', // Green
db: '9467bd', // Purple
middleware: 'ff7f0e', // Orange for middleware
test: 'd62728', // Red
};

Expand Down Expand Up @@ -59,6 +60,9 @@ jobs:
if (file.filename.startsWith('db/')) {
labels.add('db');
}
if (file.filename.startsWith('src/middleware')) {
labels.add('middleware');
}
});

if (labels.size > 0) {
Expand Down
47 changes: 26 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [x] 用户登录功能(返回 JWT -通过Cookie)🔑
- [x] 自动生成的 OpenAPI Schema 和可交互的 Reference 📚
- [x] 用户登陆状态验证及自动刷新(通过Cookie和authMiddleware)🔄
- [x] 404 处理及全局错误处理(JSON)🚫
- [ ] 基础权限验证(基于 JWT 的路由保护)🔐
- [ ] 密码重置功能 🔄 (in future)
- [ ] 双因素身份验证(2FA)🔒 (in future)
Expand All @@ -43,26 +44,31 @@
```plaintext
.
├── db
│ └── schema.sql # 数据库初始化脚本
│ └── schema.sql # 数据库初始化脚本
├── src
│ ├── auth
│ │ ├── login.ts # 登录逻辑
│ │ ├── register.ts # 注册逻辑
│ │ ├── reset.ts # 密码重置(开发中)
│ │ └── verify.ts # 2FA 验证(开发中)
│ ├── index.ts # 主入口文件,初始化 Hono 应用
│ └── utils
│ │ ├── hash.ts # 密码加密工具
│ │ └── authToken.ts # JWT生成、验证及自动刷新
│ └── middleware
│ │ └── authMiddleware.ts # 检测 Cookie 的登陆状态
│ │ ├── login.ts # 登录逻辑
│ │ ├── register.ts # 注册逻辑
│ │ ├── reset.ts # 密码重置(开发中)
│ │ ├── status.ts # 用户状态检测
│ │ └── verify.ts # 2FA 验证(开发中)
│ ├── index.ts # 主入口文件,初始化 Hono 应用
│ ├── middleware
│ │ ├── auth-middleware.ts # 检测 Cookie 的登录状态
│ │ ├── not-found.ts # 404 处理
│ │ └── on-error.ts # 全局错误处理
│ ├── utils
│ │ ├── auth-token.ts # JWT 生成、验证及自动刷新
│ │ ├── hash.ts # 密码加密工具
│ │ ├── password-validator.ts # 密码验证
│ │ └── username-validator.ts # 用户名验证
│ └── lib
│ ├── db # 数据库操作
│ └── helper # JSON 构建和错误响应处理
├── wrangler.toml # Wrangler 配置文件
├── package.json # 项目依赖和脚本
├── example.env # 环境变量示例文件
└── README.md # 项目说明文档
│ ├── db # 数据库操作
│ └── helper # Zod JSON 构建和错误响应处理
├── wrangler.toml # Wrangler 配置文件
├── package.json # 项目依赖和脚本
├── example.env # 环境变量示例文件
└── README.md # 项目说明文档
```

## 🚀 快速开始
Expand All @@ -77,8 +83,8 @@

2. 配置环境变量:

- 复制 `example.env` 并重命名为 `.env`
- 设置 JWT 密钥和其他必要配置
- 复制 `example.dev.vars` 并重命名为 `.dev.vars`
- 设置 JWT 密钥、有效时常和其他必要配置

3. 使用 Wrangler 在本地初始化 D1 数据库:

Expand All @@ -94,9 +100,8 @@

## 📚 未来发展计划

- 实现 Refresh Token 机制,改善用户体验
- 加入双因素身份验证(2FA),提高账户安全性
- 完善错误处理和日志记录功能
- 完善日志记录功能
- 提供详细的 API 文档,方便集成与二次开发

---
Expand Down
47 changes: 26 additions & 21 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Designed for small applications and personal projects, this system provides a se
- [x] User login (returns JWT via Cookie) 🔑
- [x] Auto-generated OpenAPI Schema and Interactive Reference 📚
- [x] User login status verification with auto-refresh (via Cookie and authMiddleware) 🔄
- [x] 404 and global error handling (JSON) 🚫
- [ ] Basic authorization (JWT-protected routes) 🔐
- [ ] Password reset feature 🔄 (in future)
- [ ] Two-factor authentication (2FA) 🔒 (in future)
Expand All @@ -43,26 +44,31 @@ Designed for small applications and personal projects, this system provides a se
```plaintext
.
├── db
│ └── schema.sql # Database initialization script
│ └── schema.sql # Database initialization script
├── src
│ ├── auth
│ │ ├── login.ts # Login logic
│ │ ├── register.ts # Registration logic
│ │ ├── reset.ts # Password reset (in development)
│ │ └── verify.ts # 2FA verification (in development)
│ ├── index.ts # Main entry, initializes Hono app
│ └── utils
│ │ ├── hash.ts # Password hashing utilities
│ │ └── authToken.ts # JWT generate, validate, and refresh
│ └── middleware
│ │ └── authMiddleware.ts # Check user auth status through Cookie
│ │ ├── login.ts # Login logic
│ │ ├── register.ts # Registration logic
│ │ ├── reset.ts # Password reset (in development)
│ │ ├── status.ts # User status check
│ │ └── verify.ts # 2FA verification (in development)
│ ├── index.ts # Main entry file, initializing Hono application
│ ├── middleware
│ │ ├── auth-middleware.ts # Check login status via Cookie
│ │ ├── not-found.ts # 404 handling
│ │ └── on-error.ts # Global error handling
│ ├── utils
│ │ ├── auth-token.ts # JWT generation, validation, and auto-refresh
│ │ ├── hash.ts # Password hashing utility
│ │ ├── password-validator.ts # Password validation
│ │ └── username-validator.ts # Username validation
│ └── lib
│ ├── db # Database query
│ └── helper # Data structure builder
├── wrangler.toml # Wrangler configuration file
├── package.json # Project dependencies and scripts
├── example.env # Environment variable sample file
└── README.md # Project documentation
│ ├── db # Database operations
│ └── helper # Zod JSON building and error response handling
├── wrangler.toml # Wrangler configuration file
├── package.json # Project dependencies and scripts
├── example.env # Environment variable sample file
└── README.md # Project documentation
```

## 🚀 Quick Start
Expand All @@ -77,8 +83,8 @@ Designed for small applications and personal projects, this system provides a se

2. Set up environment variables:

- Copy `example.env` and rename it to `.env`
- Set the JWT secret and other necessary configurations
- Copy `example.dev.vars` and rename it to `.dev.vars`
- Set the JWT secret, expire time, and other necessary configurations

3. Initialize the D1 database locally with Wrangler:

Expand All @@ -94,9 +100,8 @@ Designed for small applications and personal projects, this system provides a se

## 📚 Future Plans

- Implement a refresh token mechanism to improve user experience
- Add two-factor authentication (2FA) for enhanced account security
- Improve error handling and logging
- Implement logging
- Provide comprehensive API documentation for easy integration and development

---
Expand Down
4 changes: 2 additions & 2 deletions src/auth/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'

// Mock dependencies
import { getUserByUsername } from '@/lib/db'
import { generateAuthTokenAndSetCookie } from '@/utils/authToken'
import { generateAuthTokenAndSetCookie } from '@/utils/auth-token'
import { verifyPassword } from '@/utils/hash'

// Mock implementations
vi.mock('@/lib/db')
vi.mock('@/utils/hash')
vi.mock('@/utils/authToken')
vi.mock('@/utils/auth-token')

// Mock database and environment
const mockDB = {
Expand Down
2 changes: 1 addition & 1 deletion src/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
jsonContentRequired,
jsonMessageContent,
} from '@/lib/helper'
import { generateAuthTokenAndSetCookie } from '@/utils/authToken'
import { generateAuthTokenAndSetCookie } from '@/utils/auth-token'
import { verifyPassword } from '@/utils/hash'
import { createRoute, z } from '@hono/zod-openapi'
import type { Context } from 'hono'
Expand Down
12 changes: 6 additions & 6 deletions src/auth/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import type { Context } from 'hono'
import { beforeEach, describe, expect, it, vi } from 'vitest'

// Mock dependencies
import { generateAuthTokenAndSetCookie } from '@/utils/authToken'
import { generateAuthTokenAndSetCookie } from '@/utils/auth-token'
import { hashPassword } from '@/utils/hash'
import { passwordValidator } from '@/utils/passwordValidator'
import { usernameValidator } from '@/utils/usernameValidator'
import { passwordValidator } from '@/utils/password-validator'
import { usernameValidator } from '@/utils/username-validator'

// Mock implementations
vi.mock('@/utils/hash')
vi.mock('@/utils/authToken')
vi.mock('@/utils/passwordValidator')
vi.mock('@/utils/usernameValidator')
vi.mock('@/utils/auth-token')
vi.mock('@/utils/password-validator')
vi.mock('@/utils/username-validator')

// Mock database
const mockDB = {
Expand Down
6 changes: 3 additions & 3 deletions src/auth/register.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createNewUser, getUserByUserId } from '@/lib/db'
import { errorResponse, jsonContentRequired } from '@/lib/helper'
import { generateAuthTokenAndSetCookie } from '@/utils/authToken'
import { generateAuthTokenAndSetCookie } from '@/utils/auth-token'
import { hashPassword } from '@/utils/hash'
import { passwordValidator } from '@/utils/passwordValidator'
import { usernameValidator } from '@/utils/usernameValidator'
import { passwordValidator } from '@/utils/password-validator'
import { usernameValidator } from '@/utils/username-validator'
import { createRoute, z } from '@hono/zod-openapi'
import type { Context } from 'hono'

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { loginHandler, loginRoute } from '@/auth/login'
import { registerHandler, registerRoute } from '@/auth/register'
import { authStatusHandler, authStatusRoute } from '@/auth/status'
import { errorHook } from '@/lib/helper'
import { authMiddleware } from '@/middleware/authMiddleware'
import { authMiddleware } from '@/middleware/auth-middleware'
import { notFound } from '@/middleware/not-found'
import { onError } from '@/middleware/on-error'
import { OpenAPIHono } from '@hono/zod-openapi'
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/lib/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { getPasswordRoleByUserRoleId } from './getPasswordRole'
export { getUserByUserId, getUserByUsername } from './getUser'
export { createNewUser } from './setUser'
export { getPasswordRoleByUserRoleId } from './get-password-role'
export { getUserByUserId, getUserByUsername } from './get-user'
export { createNewUser } from './set-user'
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from '@hono/zod-openapi'
import { jsonContent } from './jsonContent'
import { jsonContent } from './json-content'

export const errorResponse = (message: string) => {
return jsonContent(
Expand Down
10 changes: 5 additions & 5 deletions src/lib/helper/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { errorHook } from './errorHook'
export { errorResponse } from './errorResponse'
export { jsonContent } from './jsonContent'
export { jsonContentRequired } from './jsonContentRequired'
export { jsonMessageContent } from './jsonMessageContent'
export { errorHook } from './error-hook'
export { errorResponse } from './error-response'
export { jsonContent } from './json-content'
export { jsonContentRequired } from './json-content-required'
export { jsonMessageContent } from './json-message-content'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { jsonContent } from './jsonContent'
import { jsonContent } from './json-content'
import type { ZodSchema } from './types'

export const jsonContentRequired = <T extends ZodSchema>(
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from '@hono/zod-openapi'
import { jsonContent } from './jsonContent'
import { jsonContent } from './json-content'

export const jsonMessageContent = (message: string) => {
const messageSchema = z.object({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { validateAuthToken } from '@/utils/authToken'
import { validateAuthToken } from '@/utils/auth-token'
import type { Context, Next } from 'hono'
import { createMiddleware } from 'hono/factory'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getUserByUserId } from '@/lib/db'
import {
generateAuthTokenAndSetCookie,
validateAuthToken,
} from '@/utils/authToken'
} from '@/utils/auth-token'
import type { Context } from 'hono'
import { getSignedCookie, setSignedCookie } from 'hono/cookie'
import { sign, verify } from 'hono/jwt'
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { passwordValidator } from '@/utils/passwordValidator'
import { passwordValidator } from '@/utils/password-validator'
import type { Context } from 'hono'
import { describe, expect, it, vi } from 'vitest'

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { usernameValidator } from '@/utils/usernameValidator'
import { usernameValidator } from '@/utils/username-validator'
import type { Context } from 'hono'
import { describe, expect, it, vi } from 'vitest'

Expand Down
File renamed without changes.
Loading