accounts/docs/usage/examples.md
2026-01-26 22:21:11 +08:00

86 lines
2.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 常见使用场景
> 示例默认假设 `auth.enable=false`,并使用会话 token 进行认证。
## 注册(邮件验证关闭)
```bash
curl -X POST http://localhost:8080/api/auth/register \
-H 'Content-Type: application/json' \
-d '{"name":"demo","email":"demo@example.com","password":"Passw0rd!"}'
```
## 邮件验证码注册SMTP 已启用)
```bash
# 发送验证码
curl -X POST http://localhost:8080/api/auth/register/send \
-H 'Content-Type: application/json' \
-d '{"email":"demo@example.com"}'
# 验证并完成注册
curl -X POST http://localhost:8080/api/auth/register/verify \
-H 'Content-Type: application/json' \
-d '{"email":"demo@example.com","code":"123456"}'
```
## 登录并获取会话
```bash
curl -X POST http://localhost:8080/api/auth/login \
-H 'Content-Type: application/json' \
-d '{"identifier":"demo@example.com","password":"Passw0rd!"}'
```
返回示例(字段可能包含 `mfaToken`
```json
{
"message": "login successful",
"token": "<session-token>",
"expiresAt": "<timestamp>",
"user": {"id":"..."}
}
```
## 会话查询
```bash
curl -H 'Authorization: Bearer <session-token>' \
http://localhost:8080/api/auth/session
```
## MFA 绑定
```bash
# 申请 TOTP secret
curl -X POST http://localhost:8080/api/auth/mfa/totp/provision \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <session-token>' \
-d '{}'
# 验证 TOTP
curl -X POST http://localhost:8080/api/auth/mfa/totp/verify \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <session-token>' \
-d '{"mfaToken":"<mfa-token>","totpCode":"123456"}'
```
## 订阅 Upsert
```bash
curl -X POST http://localhost:8080/api/auth/subscriptions \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <session-token>' \
-d '{"externalId":"sub_001","provider":"stripe","kind":"subscription","status":"active"}'
```
## 管理设置更新
```bash
curl -X POST http://localhost:8080/api/auth/admin/settings \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <session-token>' \
-d '{"version":1,"matrix":{"billing":{"admin":true,"operator":false}}}'
```