AINative Authentication Guide
Auth Methods
| Method | Use Case | Header |
|---|
| API Key | Server-side, agents, SDKs, MCP tools | INLINECODE0 |
| Bearer JWT |
User sessions, web apps |
Authorization: Bearer <token> |
| OAuth2 | Social login (LinkedIn, GitHub) | Standard OAuth2 flow |
API Key Auth (Simplest)
Get a key via npx zerodb init or from the dashboard.
CODEBLOCK0
CODEBLOCK1
Email/Password Registration & Login
CODEBLOCK2
JWT Usage
CODEBLOCK3
Token Refresh
CODEBLOCK4
Logout
CODEBLOCK5
OAuth2 Social Login
CODEBLOCK6
Next.js Middleware
CODEBLOCK7
Password Reset
CODEBLOCK8
Auth Endpoints
| Endpoint | Method | Description |
|---|
| INLINECODE3 | POST | Create account |
| INLINECODE4 |
POST | Email/password → JWT |
|
/api/v1/auth/logout | POST | Invalidate session |
|
/api/v1/auth/refresh | POST | Refresh access token |
|
/api/v1/users/me | GET | Current user profile |
|
/api/v1/auth/verify-email | POST | Verify email address |
|
/api/v1/auth/forgot-password | POST | Send reset email |
|
/api/v1/auth/reset-password | POST | Apply new password |
|
/api/v1/auth/linkedin/callback | POST | LinkedIn OAuth2 |
|
/api/v1/auth/github/callback | POST | GitHub OAuth2 |
Error Codes
| Status | Meaning |
|---|
| 401 | Invalid or missing token/key |
| 403 |
Valid auth, insufficient permissions |
| 409 | Email already registered |
References
- -
src/backend/app/api/v1/endpoints/auth.py — Auth endpoint implementation - INLINECODE14 — Next.js auth middleware
- INLINECODE15 — Full authentication guide
AINative 身份验证指南
身份验证方法
| 方法 | 使用场景 | 请求头 |
|---|
| API 密钥 | 服务端、代理、SDK、MCP 工具 | X-API-Key: ak_... |
| Bearer JWT |
用户会话、Web 应用 | Authorization: Bearer
|
| OAuth2 | 社交登录(LinkedIn、GitHub) | 标准 OAuth2 流程 |
API 密钥身份验证(最简单)
通过 npx zerodb init 或从控制台获取密钥。
python
import requests
response = requests.get(
https://api.ainative.studio/api/v1/public/credits/balance,
headers={X-API-Key: akyourkey}
)
typescript
const res = await fetch(https://api.ainative.studio/api/v1/public/credits/balance, {
headers: { X-API-Key: akyourkey }
});
邮箱/密码注册与登录
python
注册
resp = requests.post(
https://api.ainative.studio/api/v1/auth/register,
json={email: user@example.com, password: securepass, name: Alice}
)
token = resp.json()[access_token]
登录
resp = requests.post(
https://api.ainative.studio/api/v1/auth/login,
json={email: user@example.com, password: securepass}
)
accesstoken = resp.json()[accesstoken]
refreshtoken = resp.json()[refreshtoken]
JWT 使用
python
headers = {Authorization: fBearer {access_token}}
me = requests.get(https://api.ainative.studio/api/v1/users/me, headers=headers).json()
令牌刷新
python
resp = requests.post(
https://api.ainative.studio/api/v1/auth/refresh,
json={refreshtoken: refreshtoken}
)
newaccesstoken = resp.json()[access_token]
退出登录
python
requests.post(
https://api.ainative.studio/api/v1/auth/logout,
headers={Authorization: fBearer {access_token}}
)
OAuth2 社交登录
python
LinkedIn
resp = requests.post(
https://api.ainative.studio/api/v1/auth/linkedin/callback,
json={code: oauthcode, redirecturi: https://yourapp.com/callback}
)
GitHub
resp = requests.post(
https://api.ainative.studio/api/v1/auth/github/callback,
json={code: oauthcode, redirecturi: https://yourapp.com/callback}
)
token = resp.json()[access_token]
Next.js 中间件
typescript
// middleware.ts
import { createMiddleware } from @ainative/next-sdk/middleware;
export const middleware = createMiddleware({
apiKey: process.env.AINATIVEAPIKEY!,
protectedPaths: [/dashboard, /api/protected],
loginPath: /login,
});
密码重置
python
请求重置邮件
requests.post(https://api.ainative.studio/api/v1/auth/forgot-password,
json={email: user@example.com})
使用邮件中的令牌设置新密码
requests.post(https://api.ainative.studio/api/v1/auth/reset-password,
json={token: resettokenfromemail, newpassword: newpassword})
身份验证端点
| 端点 | 方法 | 描述 |
|---|
| /api/v1/auth/register | POST | 创建账户 |
| /api/v1/auth/login |
POST | 邮箱/密码 → JWT |
| /api/v1/auth/logout | POST | 使会话失效 |
| /api/v1/auth/refresh | POST | 刷新访问令牌 |
| /api/v1/users/me | GET | 当前用户信息 |
| /api/v1/auth/verify-email | POST | 验证邮箱地址 |
| /api/v1/auth/forgot-password | POST | 发送重置邮件 |
| /api/v1/auth/reset-password | POST | 应用新密码 |
| /api/v1/auth/linkedin/callback | POST | LinkedIn OAuth2 |
| /api/v1/auth/github/callback | POST | GitHub OAuth2 |
错误码
身份验证有效,但权限不足 |
| 409 | 邮箱已注册 |
参考
- - src/backend/app/api/v1/endpoints/auth.py — 身份验证端点实现
- packages/sdks/nextjs/src/middleware/ — Next.js 身份验证中间件
- docs/guides/AUTHENTICATION.md — 完整身份验证指南