mirror of
https://github.com/woodchen-ink/Q58Connect.git
synced 2025-07-18 14:01:55 +08:00
86 lines
2.5 KiB
Markdown
86 lines
2.5 KiB
Markdown
# Discourse Connect
|
||
|
||
这是一个基于Next.js, 实现了使用 Discourse SSO (Single Sign-On) 用户系统的 OAuth 认证功能。
|
||
|
||
前端UI使用shadcn/ui.
|
||
|
||
> shadcn安装组件的命令,举例: npx shadcn@latest add button
|
||
|
||
## 项目概述
|
||
|
||
本项目提供了一个 OAuth 认证系统,允许其他应用程序使用 Discourse 论坛的用户账号进行身份验证。这样可以让用户使用他们已有的 Discourse 账号登录到您的应用程序,无需创建新的账号。
|
||
|
||
目前Discourse论坛是Q58论坛.
|
||
Q58论坛网址: https://q58.club
|
||
本项目部署网址: https://connect.q58.club
|
||
|
||
主要特性:
|
||
|
||
- 基于 Discourse SSO 的用户认证
|
||
- OAuth 2.0 协议支持
|
||
- 使用 Next.js 框架构建,提供良好的性能和开发体验
|
||
|
||
本项目部署在vercel, 数据库使用Neon.
|
||
|
||
## 需要检查的几点
|
||
|
||
1. 直接登录本系统
|
||
2. 未登录本系统, 未登录q58论坛, 检查: 用户在接入应用中登录, 然后登录本系统, 然后登录q58论坛, 正常一直回调到用户应用
|
||
3. 未登录本系统, 登录了q58论坛, 检查: 用户在接入应用中登录, 然后登录本系统, 正常回调到用户应用
|
||
|
||
## 用户接入本系统oauth2.0认证的方式:
|
||
|
||
1. 发起授权请求
|
||
将用户重定向到授权页面
|
||
|
||
const authUrl = 'https://connect.q58.club/oauth/authorize?' +
|
||
new URLSearchParams({
|
||
response_type: 'code', // 必填,固定值
|
||
client_id: 'your_client_id', // 必填,您的应用ID
|
||
redirect_uri: 'https://your-app.com/callback',
|
||
state: 'random_state', // 建议提供,防CSRF攻击
|
||
scope: 'read_profile' // 可选,默认read_profile
|
||
});
|
||
|
||
window.location.href = authUrl;
|
||
|
||
2. 处理授权回调
|
||
在回调地址处理授权结果
|
||
|
||
// 获取访问令牌
|
||
const response = await fetch('https://connect.q58.club/api/oauth/access_token', {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/x-www-form-urlencoded'
|
||
},
|
||
body: new URLSearchParams({
|
||
code: '授权码', // 回调参数中的code
|
||
redirect_uri: 'https://your-app.com/callback'
|
||
})
|
||
});
|
||
|
||
const { access_token, expires_in } = await response.json();
|
||
|
||
3. 获取用户信息
|
||
使用访问令牌获取用户数据
|
||
|
||
const userInfo = await fetch('https://connect.q58.club/api/oauth/user', {
|
||
headers: {
|
||
'Authorization': `Bearer ${access_token}`
|
||
}
|
||
}).then(res => res.json());
|
||
|
||
// 返回数据示例:
|
||
{
|
||
"id": "user_xxx",
|
||
"email": "user@example.com",
|
||
"username": "username",
|
||
"name": "用户昵称",
|
||
"avatar_url": "https://...",
|
||
"groups": ["group1", "group2"]
|
||
}
|
||
|
||
## 许可证
|
||
|
||
本项目采用 MIT 许可证。详情请见 [LICENSE](LICENSE) 文件。
|