Auth API

通用认证服务 - 支持多应用隔离的用户注册登录系统

📄 下载完整文档 (Markdown)
未登录

前端接入指南

1. 请求头配置

所有请求需携带:

Content-Type: application/json
x-app-id: 你的AppID
x-app-secret: 你的AppSecret
Authorization: Bearer token  // 仅注销账号需要

2. API 接口

接口方法参数
/api/v1/auth/registerPOSTemail, password(6-16位)
/api/v1/auth/loginPOSTemail, password
/api/v1/auth/forgot-passwordPOSTemail
/api/v1/auth/reset-passwordPOSTtoken, newPassword
/api/v1/auth/accountDELETE需Authorization头

3. 代码示例

// JavaScript / Web
const API = 'https://auth.shangkeai.com';
const headers = {
  'Content-Type': 'application/json',
  'x-app-id': 'YOUR_APP_ID',
  'x-app-secret': 'YOUR_APP_SECRET'
};

// 注册
const register = async (email, password) => {
  const res = await fetch(`${API}/api/v1/auth/register`, {
    method: 'POST', headers,
    body: JSON.stringify({ email, password })
  });
  return res.json(); // { message, user, token }
};

// 登录
const login = async (email, password) => {
  const res = await fetch(`${API}/api/v1/auth/login`, {
    method: 'POST', headers,
    body: JSON.stringify({ email, password })
  });
  return res.json(); // { message, user, token }
};

// 注销账号
const deleteAccount = async (token) => {
  const res = await fetch(`${API}/api/v1/auth/account`, {
    method: 'DELETE',
    headers: { ...headers, 'Authorization': `Bearer ${token}` }
  });
  return res.json();
};
// Flutter - pubspec.yaml 添加: http: ^1.1.0
import 'package:http/http.dart' as http;
import 'dart:convert';

class AuthService {
  static const _baseUrl = 'https://auth.shangkeai.com';
  static const _appId = 'YOUR_APP_ID';
  static const _appSecret = 'YOUR_APP_SECRET';

  static Map<String, String> get _headers => {
    'Content-Type': 'application/json',
    'x-app-id': _appId,
    'x-app-secret': _appSecret,
  };

  // 注册
  static Future<Map> register(String email, String password) async {
    final res = await http.post(
      Uri.parse('$_baseUrl/api/v1/auth/register'),
      headers: _headers,
      body: jsonEncode({'email': email, 'password': password}),
    );
    return jsonDecode(res.body);
  }

  // 登录
  static Future<Map> login(String email, String password) async {
    final res = await http.post(
      Uri.parse('$_baseUrl/api/v1/auth/login'),
      headers: _headers,
      body: jsonEncode({'email': email, 'password': password}),
    );
    return jsonDecode(res.body);
  }

  // 注销账号
  static Future<Map> deleteAccount(String token) async {
    final res = await http.delete(
      Uri.parse('$_baseUrl/api/v1/auth/account'),
      headers: {..._headers, 'Authorization': 'Bearer $token'},
    );
    return jsonDecode(res.body);
  }
}
# 注册
curl -X POST https://auth.shangkeai.com/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -H "x-app-id: YOUR_APP_ID" \
  -H "x-app-secret: YOUR_APP_SECRET" \
  -d '{"email":"user@example.com","password":"123456"}'

# 登录
curl -X POST https://auth.shangkeai.com/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -H "x-app-id: YOUR_APP_ID" \
  -H "x-app-secret: YOUR_APP_SECRET" \
  -d '{"email":"user@example.com","password":"123456"}'

# 注销账号
curl -X DELETE https://auth.shangkeai.com/api/v1/auth/account \
  -H "x-app-id: YOUR_APP_ID" \
  -H "x-app-secret: YOUR_APP_SECRET" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

4. 响应格式

场景响应
成功{"message": "...", "user": {"id","email"}, "token": "..."}
失败{"error": "错误信息"}
多应用隔离:每个 App 使用独立的 app_id,用户数据完全隔离。同一邮箱可在不同 App 分别注册。

短信验证码登录 手机号验证码

1. 请求头配置

所有请求需携带应用凭证:

Content-Type: application/json
x-app-id: 你的AppID
x-app-secret: 你的AppSecret
Authorization: Bearer token  // 仅注销账号需要

2. 已注册应用

应用名称App ID
元智播app-yuanzhibo
它从哪里来app-whereishefrom
历史之声app-history-voice
趣学AI化学实验室app-ai-funchem

3. API 接口

接口方法参数说明
/api/v1/auth/send-code POST phone (11位手机号) 发送6位验证码,有效期5分钟
/api/v1/auth/login POST phone, code (6位验证码) 验证码登录,自动注册新用户
/api/v1/auth/account DELETE 需Authorization头 永久删除账号
/health GET 健康检查(无需认证)

4. 发送验证码

向手机号发送6位数字验证码,同一手机号60秒内只能发送一次。

// 发送验证码
const sendCode = async (phone) => {
  const res = await fetch('https://auth.shangkeai.com/api/v1/auth/send-code', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'x-app-id': 'app-yuanzhibo',
      'x-app-secret': 'secret_yuanzhibo_2024_metacast'
    },
    body: JSON.stringify({ phone })
  });
  return res.json();
  // 成功: { "message": "验证码已发送" }
  // 失败: { "error": "手机号格式不正确" }
};

// 验证码登录(自动注册)
const loginWithCode = async (phone, code) => {
  const res = await fetch('https://auth.shangkeai.com/api/v1/auth/login', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'x-app-id': 'app-yuanzhibo',
      'x-app-secret': 'secret_yuanzhibo_2024_metacast'
    },
    body: JSON.stringify({ phone, code })
  });
  return res.json();
  // 成功: { "message": "登录成功", "user": { "id": 1, "phone": "138****5678" }, "token": "eyJ...", "isNewUser": false }
};
# 发送验证码
curl -X POST https://auth.shangkeai.com/api/v1/auth/send-code \
  -H "Content-Type: application/json" \
  -H "x-app-id: app-yuanzhibo" \
  -H "x-app-secret: secret_yuanzhibo_2024_metacast" \
  -d '{"phone": "13812345678"}'

# 验证码登录
curl -X POST https://auth.shangkeai.com/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -H "x-app-id: app-yuanzhibo" \
  -H "x-app-secret: secret_yuanzhibo_2024_metacast" \
  -d '{"phone": "13812345678", "code": "123456"}'

# 注销账号
curl -X DELETE https://auth.shangkeai.com/api/v1/auth/account \
  -H "x-app-id: app-yuanzhibo" \
  -H "x-app-secret: secret_yuanzhibo_2024_metacast" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

# 健康检查
curl https://auth.shangkeai.com/health

5. 响应格式

发送验证码成功

{ "message": "验证码已发送" }

登录成功

{
  "message": "登录成功",  // 或 "注册成功"(新用户)
  "user": {
    "id": 1,
    "phone": "13812345678"
  },
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "isNewUser": false  // true 表示新注册用户
}

错误响应

状态码错误信息说明
400手机号格式不正确手机号不符合11位格式
400发送过于频繁,请60秒后再试同一手机号60秒内重复请求
400请先获取验证码该手机号未请求过验证码
400验证码错误验证码不匹配
400验证码已过期,请重新获取验证码超过5分钟有效期
401Missing app credentials缺少 x-app-id 或 x-app-secret
401Invalid app credentials应用凭证无效
401No token provided注销账号时未提供Token
401Invalid tokenToken无效或已过期

6. 速率限制

限制类型限制值说明
API请求频率100次/15分钟单IP所有API接口总请求数
验证码发送1次/60秒同一手机号验证码发送间隔
验证码有效期5分钟验证码过期后需重新获取
Token有效期7天JWT Token过期后需重新登录
响应Header:每次请求响应会包含 X-RateLimit-LimitX-RateLimit-RemainingX-RateLimit-Reset 头信息,用于监控请求配额。
多应用隔离:每个 App 使用独立的 app_id,用户数据完全隔离。同一手机号可在不同 App 分别注册。

用户注册

用户登录

忘记密码

重置密码

注销账号

警告:此操作不可逆!