Skip to content
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
2 changes: 2 additions & 0 deletions .github/workflows/basic-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
run: |
cd apps/web
pnpm install --frozen-lockfile
env:
DATABASE_URL: 'postgresql://test:test@localhost:5432/test'

- name: 🔍 检查 Web 应用
run: |
Expand Down
191 changes: 174 additions & 17 deletions apps/web/.env.example
Original file line number Diff line number Diff line change
@@ -1,17 +1,174 @@
# NextAuth 配置
AUTH_SECRET="your-auth-secret-here" # 使用 `npx auth secret` 生成
NEXTAUTH_URL="http://localhost:8800"

# GitHub OAuth 配置
# 1. 访问 https://github.com/settings/applications/new
# 2. 创建新的 OAuth App
# 3. Authorization callback URL: http://localhost:8800/api/auth/callback/github
GITHUB_CLIENT_ID="your-github-client-id"
GITHUB_CLIENT_SECRET="your-github-client-secret"

# 应用配置
NEXT_PUBLIC_DOMAIN=localhost:8800
NEXT_PUBLIC_NODE_ENV=development

# API 配置
NEXT_PUBLIC_API_URL="http://localhost:8890" # 后端 API 地址
# Better-Auth 环境变量配置

## 必需的环境变量

### Better-Auth 认证

```bash
# Better-Auth 密钥(必需)
# 用于加密 session 和签名 tokens
# 生成方法: npx @better-auth/cli secret 或 openssl rand -base64 32
BETTER_AUTH_SECRET=your-secret-key-here

# 数据库连接(必需)
DATABASE_URL=postgresql://user:password@localhost:5432/database_name
```

### GitHub OAuth

```bash
# GitHub OAuth 应用配置(必需)
# 从 https://github.com/settings/developers 获取
GITHUB_CLIENT_ID=your_github_client_id
GITHUB_CLIENT_SECRET=your_github_client_secret
```

---

## 可选的环境变量

### Better-Auth 配置

```bash
# Better-Auth 基础 URL(可选,默认 http://localhost:8800)
# 生产环境需要设置为实际域名
BETTER_AUTH_URL=https://your-domain.com

# 客户端可见的 Better-Auth URL(可选)
# 通常与 BETTER_AUTH_URL 相同
NEXT_PUBLIC_BETTER_AUTH_URL=https://your-domain.com
```

---

## 开发环境配置示例

创建 `apps/web/.env.local` 文件:

```bash
# .env.local - 不要提交到版本控制

# Better-Auth
BETTER_AUTH_SECRET=dev-secret-change-in-production
BETTER_AUTH_URL=http://localhost:8800
NEXT_PUBLIC_BETTER_AUTH_URL=http://localhost:8800

# 数据库
DATABASE_URL=postgresql://user:password@localhost:5432/telos

# GitHub OAuth
GITHUB_CLIENT_ID=your_github_client_id
GITHUB_CLIENT_SECRET=your_github_client_secret
```

---

## 生产环境配置示例

### 1. 生成生产密钥

```bash
npx @better-auth/cli secret
# 或
openssl rand -base64 32
```

### 2. 设置环境变量

```bash
# Better-Auth
BETTER_AUTH_SECRET=<生成的随机密钥>
BETTER_AUTH_URL=https://your-domain.com
NEXT_PUBLIC_BETTER_AUTH_URL=https://your-domain.com

# 数据库(使用生产数据库)
DATABASE_URL=postgresql://prod_user:prod_password@prod-db:5432/telos

# GitHub OAuth(使用生产 OAuth 应用)
GITHUB_CLIENT_ID=<生产环境的 Client ID>
GITHUB_CLIENT_SECRET=<生产环境的 Client Secret>
```

---

## GitHub OAuth 应用配置

### 本地开发

1. 访问 https://github.com/settings/developers
2. 点击 "New OAuth App"
3. 配置如下:
- **Application name**: Telos (Development)
- **Homepage URL**: `http://localhost:8800`
- **Authorization callback URL**: `http://localhost:8800/api/auth/callback/github`
4. 创建后获取 `Client ID` 和 `Client Secret`

### 生产环境

1. 同上,创建新的 OAuth App
2. 配置:
- **Homepage URL**: `https://your-domain.com`
- **Authorization callback URL**: `https://your-domain.com/api/auth/callback/github`
- **Enable Device Flow**: 可选

---

## 数据库配置

### PostgreSQL 连接字符串格式

```bash
# 标准格式
postgresql://username:password@host:port/database

# 示例
postgresql://telos_user:telos_pass@localhost:5432/telos

# 使用 socket(如果支持)
postgresql://username:password@/database?host=/var/run/postgresql
```

### Docker PostgreSQL

如果使用 Docker 运行 PostgreSQL:

```yaml
# docker-compose.yml
services:
postgres:
image: postgres:14
environment:
POSTGRES_USER: telos_user
POSTGRES_PASSWORD: telos_pass
POSTGRES_DB: telos
ports:
- "5432:5432"

# 对应的 DATABASE_URL
DATABASE_URL=postgresql://telos_user:telos_pass@localhost:5432/telos
```

---

## 安全注意事项

1. **永远不要将 `.env.local` 提交到版本控制**
```bash
# .gitignore
.env.local
.env.*.local
```

2. **生产环境的 secret 必须使用强随机密钥**
```bash
# 生成 32 字节的随机密钥
openssl rand -base64 32
```

3. **GitHub Client Secret 要妥善保管**
- 不要在前端代码中使用
- 不要在日志中打印

4. **使用不同的 OAuth 应用区分开发和生产环境**
- 开发环境使用 localhost 回调
- 生产环境使用正式域名回调
Loading
Loading