快速开始

5 分钟内将 W-ID 集成到你的应用中。

方式 A: 使用 SDK (推荐)

1. 引入 SDK

<script src="https://auth.vibe.wang/sdk/w-id.js"></script>

2. 初始化

const auth = WID.init({
  clientId: 'YOUR_CLIENT_ID',
  redirectUri: window.location.origin + '/callback'
});

// 跳转登录 (自动 PKCE)
auth.login();

// 在回调页处理
auth.handleCallback().then(tokens => {
  console.log('Logged in!', tokens);
});

3. 获取用户信息

const user = await auth.getUser();
console.log(user.email, user.name, user.tier);
💡 PKCE 自动生成
SDK 内置 S256 PKCE 支持,无需手动处理 code_verifier,安全且简单。

方式 B: 手动 OAuth

如不使用 SDK,可直接调用 OAuth 端点:

# 1. 跳转授权
https://auth.vibe.wang/oauth/authorize?
  client_id=YOUR_ID&
  redirect_uri=YOUR_CALLBACK&
  response_type=code&
  scope=openid%20email%20profile&
  state=RANDOM_STATE

# 2. 用 code 换 token
# Confidential Client (服务端):
POST https://auth.vibe.wang/oauth/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&
code=AUTH_CODE&
redirect_uri=YOUR_CALLBACK&
client_id=YOUR_ID&
client_secret=YOUR_SECRET

# Public Client (SPA/SDK — 使用 PKCE):
POST https://auth.vibe.wang/oauth/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&
code=AUTH_CODE&
redirect_uri=YOUR_CALLBACK&
client_id=YOUR_ID&
code_verifier=YOUR_PKCE_VERIFIER

# 3. 获取用户信息
GET https://auth.vibe.wang/oauth/userinfo
Authorization: Bearer ACCESS_TOKEN

完整 Quick Start 模板

复制下面的 HTML 文件,替换 YOUR_CLIENT_ID,用浏览器打开即可测试登录:

<!DOCTYPE html>
<html>
<head>
  <title>My App — W-ID Demo</title>
  <script src="https://auth.vibe.wang/sdk/w-id.js"></script>
  <style>
    body { font-family: system-ui; max-width: 480px; margin: 4rem auto; text-align: center; }
    button { padding: 0.75rem 2rem; font-size: 1rem; border-radius: 8px; border: none; cursor: pointer; }
    .login-btn { background: #10a37f; color: white; }
    .logout-btn { background: #ef4444; color: white; display: none; }
    .profile { display: none; margin: 2rem 0; }
  </style>
</head>
<body>
  <h1>My App</h1>

  <button class="login-btn" onclick="auth.login()">Sign in with W-ID</button>
  <button class="logout-btn" onclick="auth.logout()">Sign Out</button>

  <div class="profile">
    <p>Welcome, <strong id="user-name"></strong>!</p>
    <p>Email: <span id="user-email"></span></p>
  </div>

  <script>
    var auth = WID.init({
      clientId: 'YOUR_CLIENT_ID',
      redirectUri: window.location.href.split('?')[0]
    });

    // Handle callback if returning from login
    if (window.location.search.includes('code=')) {
      auth.handleCallback().then(showUser);
    } else if (auth.isAuthenticated()) {
      auth.getUser().then(showUser);
    }

    function showUser(user) {
      document.querySelector('.login-btn').style.display = 'none';
      document.querySelector('.logout-btn').style.display = 'inline-block';
      document.querySelector('.profile').style.display = 'block';
      document.getElementById('user-name').textContent = user.name || user.email;
      document.getElementById('user-email').textContent = user.email;
    }
  </script>
</body>
</html>
💡 测试提示
在 Admin → Clients 创建 Client 时,将 Redirect URI 设为 http://localhost:8080/,然后使用 python3 -m http.server 8080 或任意本地服务器来测试。

OAuth Discovery

服务端元数据自动发现端点(RFC 8414):

https://auth.vibe.wang/.well-known/openid-configuration
⚠️ 协议说明
W-ID 是 OAuth 2.0 Authorization Server,不是完整 OIDC Provider。不发行 id_token,无 JWKS 端点。请通过 /oauth/userinfo 获取用户信息。