API 接入文档

基于WordPress REST API的认证服务,支持JWT Token身份验证

Base URL: https://demo.kina.ink/wp-json/my-auth/v1/

端点总览

方法 路径 功能 认证
POST /register 用户注册 无需
POST /login 用户登录 无需
POST /token/validate 验证Token 无需
GET /me 获取当前用户 Bearer Token

1. 用户注册

POST /register

请求参数

参数类型必填说明
usernamestring用户名,只能包含字母、数字、下划线和连字符
emailstring有效的邮箱地址
passwordstring密码,至少6位

请求示例

cURL
curl -X POST "https://demo.kina.ink/wp-json/my-auth/v1/register" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "johndoe",
    "email": "john@example.com",
    "password": "123456"
  }'
JavaScript / Fetch
fetch('https://demo.kina.ink/wp-json/my-auth/v1/register', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    username: 'johndoe',
    email: 'john@example.com',
    password: '123456'
  })
})
.then(response => response.json())
.then(data => console.log(data));

成功响应 (201)

{
  "success": true,
  "data": {
    "user_id": 42,
    "message": "注册成功"
  }
}

错误响应

{
  "success": false,
  "code": "username_exists",
  "message": "该用户名已被注册"
}

2. 用户登录

POST /login

请求参数

参数类型必填说明
usernamestring用户名或邮箱
passwordstring密码

请求示例

cURL
curl -X POST "https://demo.kina.ink/wp-json/my-auth/v1/login" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "johndoe",
    "password": "123456"
  }'
JavaScript / Fetch
fetch('https://demo.kina.ink/wp-json/my-auth/v1/login', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    username: 'johndoe',
    password: '123456'
  })
})
.then(response => response.json())
.then(data => {
  if (data.success) {
    localStorage.setItem('token', data.data.token);
  }
});

成功响应 (200)

{
  "success": true,
  "data": {
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
    "expires": 1751299200,
    "user": {
      "id": 42,
      "username": "johndoe",
      "email": "john@example.com",
      "role": "subscriber",
      "display_name": "johndoe"
    }
  }
}

3. 验证Token

POST /token/validate

请求参数

参数类型必填说明
tokenstringJWT Token字符串

请求示例

cURL
curl -X POST "https://demo.kina.ink/wp-json/my-auth/v1/token/validate" \
  -H "Content-Type: application/json" \
  -d '{
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..."
  }'
JavaScript / Fetch
fetch('https://demo.kina.ink/wp-json/my-auth/v1/token/validate', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...'
  })
})
.then(response => response.json())
.then(data => console.log(data));

成功响应 (200)

{
  "success": true,
  "data": {
    "valid": true,
    "expires": 1751299200,
    "expires_in": 604800,
    "user": {
      "id": 42,
      "username": "johndoe",
      "email": "john@example.com"
    }
  }
}

4. 获取当前用户

GET /me

请求头

头信息说明
AuthorizationBearer <token>登录时获取的JWT Token

请求示例

cURL
curl -X GET "https://demo.kina.ink/wp-json/my-auth/v1/me" \
  -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..."
JavaScript / Fetch
fetch('https://demo.kina.ink/wp-json/my-auth/v1/me', {
  method: 'GET',
  headers: {
    'Authorization': 'Bearer ' + localStorage.getItem('token')
  }
})
.then(response => response.json())
.then(data => console.log(data));

成功响应 (200)

{
  "success": true,
  "data": {
    "id": 42,
    "username": "johndoe",
    "email": "john@example.com",
    "display_name": "johndoe",
    "role": "subscriber",
    "registered": "2026-07-06 14:00:00"
  }
}

错误码对照表

错误码HTTP状态码说明
username_exists409用户名已被注册
email_exists409邮箱已被注册
invalid_credentials401用户名或密码错误
token_expired401Token已过期
token_invalid401Token签名无效
no_token401未提供认证Token
user_not_found404用户不存在
jwt_not_configured500JWT密钥未配置
register_failed500注册失败
token_generation_failed500Token生成失败