新增oauth预配置介绍

This commit is contained in:
Pan Qiancheng 2025-10-29 09:16:50 +08:00
parent 841341fa2a
commit da09b9a9a9
1 changed files with 139 additions and 6 deletions

View File

@ -607,16 +607,149 @@ const tokenValue = await loginByOauth({
| 场景 | 已登录 | OAuth 账号已存在 | 操作 |
|-----|-------|---------------|------|
| 首次登录 | ❌ | ❌ | 自动注册(需配置 `autoRegister=true` |
| 老用户登录 | ❌ | ✅ | 直接登录 |
| 绑定新账号 | ✅ | ❌ | 创建绑定 |
| 重复绑定 | ✅ | ✅(自己) | 报错 |
| 绑定冲突 | ✅ | ✅(他人) | 报错 |
| 首次登录 | 否 | 否 | 自动注册(需配置 `autoRegister=true` |
| 老用户登录 | 否 | 是 | 直接登录 |
| 绑定新账号 | 是 | 否 | 创建绑定 |
| 重复绑定 | 是 | 是(自己) | 报错 |
| 绑定冲突 | 是 | 是(他人) | 报错 |
---
## 配置指南
### 零、预配置
- 必须在前端组件中使用oauth登录页组件
- /src/pages/frontend/login/oauth/authorize (示例如下)
- 页面在项目中的位置可以改动但是在配置时请确保该路径为oauth服务的认证点
index.ts:
```ts
export default OakComponent({
// Virtual Component
isList: false,
filters: [],
properties:{}
});
```
web.pc.tsx:
```tsx
import React from 'react';
import { EntityDict } from '@project/oak-app-domain';
import { RowWithActions, WebComponentProps } from 'oak-frontend-base';
import Styles from './styles.module.less';
import Auth from "oak-general-business/es/components/login/oauth/authorize"
const Authorize = (
props: WebComponentProps<
EntityDict,
keyof EntityDict,
false,
{
// virtual
}
>
) => {
return <Auth
oakPath={`#Authorize`}
/>;
};
export default Authorize;
```
- 必须在前端使用Oauth认证点组件
- /src/pages/frontend/oauth (示例如下)
- 配置时请确保该路径为客户端回调地址,不可改动
index.ts:
```ts
export default OakComponent({
// Virtual Component
isList: false,
filters: [],
properties:{}
});
```
web.pc.tsx:
```tsx
import React from 'react';
import { EntityDict } from '@project/oak-app-domain';
import { RowWithActions, WebComponentProps } from 'oak-frontend-base';
import Styles from './styles.module.less';
import OAuth from "oak-general-business/es/components/oauth"
const Oauth = (
props: WebComponentProps<
EntityDict,
keyof EntityDict,
false,
{
// virtual
}
>
) => {
const { oakFullpath } = props.data;
return <OAuth
oakPath={`#OAuth`}
></OAuth>
};
export default Oauth;
```
- 登录页必须实现下列跳转逻辑
- 这里的redirectUrl本质上是指向本地/oauth的带参数URL
```ts
export default OakComponent({
properties: {
redirect: "",
},
data: {
redirectUrl: "",
},
methods: {
onLoggedIn() {
const { redirectUrl } = this.state;
console.log('redirecting to', redirectUrl);
if (redirectUrl) {
this.features.navigator.navigateTo({ url: redirectUrl }, undefined, true);
}
},
loadSearchParams() {
const searchParams = new URLSearchParams(window.location.search);
const encoded = searchParams.get('redirect') || '';
const redirectUrl = encoded ? decodeURIComponent(atob(encoded)) : '';
console.log('Loaded redirect URL from search params:', redirectUrl);
this.setState({
redirectUrl: redirectUrl,
});
},
},
lifetimes: {
ready() {
this.loadSearchParams();
if (this.features.token.getUserId(true)) {
this.onLoggedIn();
}
},
}
});
```
### 一、配置 OAuth 提供方(让第三方应用接入)
#### 1. 创建 OAuth 应用
@ -653,7 +786,7 @@ await context.operate("oauthApplication", {
```javascript
// 构建授权 URL
const authUrl = new URL('https://your-oak-system.com/authorize');
const authUrl = new URL('https://your-oak-system.com/login/oauth/authorize');
authUrl.searchParams.set('response_type', 'code');
authUrl.searchParams.set('client_id', 'your_client_id');
authUrl.searchParams.set('redirect_uri', 'https://your-app.com/callback');