feat: add ctx.auth

This commit is contained in:
chenos 2025-06-25 14:49:36 +08:00
parent e43191df17
commit d6d9e213dc
2 changed files with 16 additions and 3 deletions

View File

@ -10,7 +10,7 @@
import React, { createContext, useContext, useMemo } from 'react';
import { useACLRoleContext } from '../acl';
import { ReturnTypeOfUseRequest, useAPIClient, useRequest } from '../api-client';
import { useAppSpin } from '../application';
import { useApp, useAppSpin } from '../application';
import { useCompile } from '../schema-component';
export const CurrentUserContext = createContext<ReturnTypeOfUseRequest>(null);
@ -44,6 +44,7 @@ export const useCurrentRoles = () => {
export const CurrentUserProvider = (props) => {
const api = useAPIClient();
const app = useApp();
const result = useRequest<any>(() =>
api
.request({
@ -51,7 +52,12 @@ export const CurrentUserProvider = (props) => {
skipNotify: true,
skipAuth: true,
})
.then((res) => res?.data),
.then((res) => {
app.flowEngine.setContext({
user: res?.data?.data,
});
return res?.data;
}),
);
const { render } = useAppSpin();

View File

@ -7,7 +7,7 @@
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { BlockModel } from '@nocobase/client';
import { APIClient, BlockModel } from '@nocobase/client';
import {
APIResource,
BaseRecordResource,
@ -263,6 +263,7 @@ ctx.element.innerHTML = \`
const flowEngine = flowContext.globals.flowEngine as FlowEngine;
const request = flowContext.globals.api.request.bind(flowContext.globals.api);
const api = flowContext.app.apiClient as APIClient;
// Create a safe execution context for the code (as async function)
// Wrap user code in an async function
@ -300,6 +301,12 @@ ctx.element.innerHTML = \`
React,
ReactDOM,
flowEngine,
auth: {
role: api.auth.role,
locale: api.auth.locale,
token: api.auth.token,
user: flowContext.globals.user,
},
Components: {
antd,
},