diff --git a/packages/api/src/migrations/init.ts b/packages/api/src/migrations/init.ts index 459d92e45..1b78e4604 100644 --- a/packages/api/src/migrations/init.ts +++ b/packages/api/src/migrations/init.ts @@ -108,39 +108,17 @@ import * as uiSchema from './ui-schema'; extname: '.png', mimetype: 'image/png', url: 'https://nocobase.oss-cn-beijing.aliyuncs.com/682e5ad037dd02a0fe4800a3e91c283b.png', - created_at: '2021-08-16T06:31:53.214Z', - updated_at: '2021-08-16T06:31:53.214Z', storage_id: 2, }, }); } + + const User = database.getModel('users'); + const user = await User.create({ + email: process.env.ADMIN_EMAIL, + password: process.env.ADMIN_PASSWORD, + }); + await database.close(); })(); -const a = { - data: { - id: 1, - title: 'NocoBase', - created_at: '2021-08-16T05:37:50.751Z', - updated_at: '2021-08-16T06:31:55.069Z', - logo_id: 3, - created_by_id: null, - updated_by_id: null, - logo: { - id: 3, - title: 'logo-white (1)', - filename: '682e5ad037dd02a0fe4800a3e91c283b.png', - extname: '.png', - size: null, - mimetype: 'image/png', - path: '', - meta: {}, - url: 'https://nocobase.oss-cn-beijing.aliyuncs.com/682e5ad037dd02a0fe4800a3e91c283b.png', - created_at: '2021-08-16T06:31:53.214Z', - updated_at: '2021-08-16T06:31:53.214Z', - created_by_id: null, - updated_by_id: null, - storage_id: 2, - }, - }, -}; diff --git a/packages/app/src/pages/index.tsx b/packages/app/src/pages/index.tsx index 9415212fe..be4e3c722 100644 --- a/packages/app/src/pages/index.tsx +++ b/packages/app/src/pages/index.tsx @@ -17,6 +17,15 @@ const request = extend({ timeout: 30000, }); +request.use(async (ctx, next) => { + const { headers } = ctx.req.options as any; + const token = localStorage.getItem('NOCOBASE_TOKEN'); + if (token) { + headers['Authorization'] = `Bearer ${token}`; + } + await next(); +}); + const RouteSwitch = createRouteSwitch({ components: { AdminLayout, diff --git a/packages/client/src/components/admin-layout/Auth.tsx b/packages/client/src/components/admin-layout/Auth.tsx new file mode 100644 index 000000000..a76e65d38 --- /dev/null +++ b/packages/client/src/components/admin-layout/Auth.tsx @@ -0,0 +1,24 @@ +import { useRequest } from 'ahooks'; +import { Spin } from 'antd'; +import React, { createContext } from 'react'; +import { Redirect } from 'react-router'; +import { useLocation } from 'react-router-dom'; + +export const AuthContext = createContext(null); + +export function AuthProvider(props) { + const service = useRequest('users:check', { + formatResult: (result) => result?.data, + }); + const location = useLocation(); + const { pathname, search } = location; + let redirect = `?redirect=${pathname}${search}`; + if (service.error) { + return ; + } + return ( + + {service.loading ? : props.children} + + ); +} diff --git a/packages/client/src/components/admin-layout/UserInfo.tsx b/packages/client/src/components/admin-layout/UserInfo.tsx index a94302bef..8ada7c08f 100644 --- a/packages/client/src/components/admin-layout/UserInfo.tsx +++ b/packages/client/src/components/admin-layout/UserInfo.tsx @@ -13,7 +13,7 @@ export const UserInfo = () => { { - await request('/users:logout'); + await request('users:logout'); localStorage.removeItem('NOCOBASE_TOKEN'); history.push('/login'); }} diff --git a/packages/client/src/components/admin-layout/index.tsx b/packages/client/src/components/admin-layout/index.tsx index c08d57936..ed7b02a6a 100644 --- a/packages/client/src/components/admin-layout/index.tsx +++ b/packages/client/src/components/admin-layout/index.tsx @@ -38,6 +38,7 @@ import { Permissions } from './Permissions'; import { More } from './More'; import { UserInfo } from './UserInfo'; import { SiteTitle, SystemSettingsProvider } from './SiteTitle'; +import { AuthProvider } from './Auth'; function DesignableToggle() { const { designable, setDesignable } = useDesignableSwitchContext(); @@ -177,19 +178,21 @@ export function AdminLayout({ route, ...others }: any) { console.log('current?.title', current, current?.title, defaultSelectedKeys); return ( - - - - - - - - - + + + + + + + + + + + ); } diff --git a/packages/client/src/components/route-schema-renderer/index.tsx b/packages/client/src/components/route-schema-renderer/index.tsx index 1e6d37e21..e5bea6df3 100644 --- a/packages/client/src/components/route-schema-renderer/index.tsx +++ b/packages/client/src/components/route-schema-renderer/index.tsx @@ -2,7 +2,7 @@ import React, { useContext, useEffect, useState } from 'react'; import { Spin } from 'antd'; import { Helmet } from 'react-helmet'; import { useRequest } from 'ahooks'; -import { SchemaRenderer } from '../../schemas'; +import { request, SchemaRenderer } from '../../schemas'; import { useForm } from '@formily/react'; import { useHistory } from 'react-router-dom'; @@ -16,8 +16,13 @@ export function useLogin() { return { async run() { await form.submit(); + const { data } = await request('users:login', { + method: 'post', + data: form.values, + }); history.push('/admin'); - console.log(form.values); + localStorage.setItem('NOCOBASE_TOKEN', data?.data?.token); + console.log('NOCOBASE_TOKEN', data?.data?.token); }, }; } diff --git a/packages/client/src/demos/demo1.tsx b/packages/client/src/demos/demo1.tsx index 1b072e29b..2a9811918 100644 --- a/packages/client/src/demos/demo1.tsx +++ b/packages/client/src/demos/demo1.tsx @@ -19,6 +19,15 @@ const request = extend({ timeout: 30000, }); +request.use(async (ctx, next) => { + const { headers } = ctx.req.options as any; + const token = localStorage.getItem('NOCOBASE_TOKEN'); + if (token) { + headers['Authorization'] = `Bearer ${token}`; + } + await next(); +}); + console.log('process.env.API_URL', process.env.API_URL); // console.log = () => {} diff --git a/packages/client/src/schemas/index.tsx b/packages/client/src/schemas/index.tsx index 46b7dcbb4..d7ddaa523 100644 --- a/packages/client/src/schemas/index.tsx +++ b/packages/client/src/schemas/index.tsx @@ -25,6 +25,16 @@ export const request = extend({ prefix: process.env.API_URL, timeout: 30000, }); + +request.use(async (ctx, next) => { + const { headers } = ctx.req.options as any; + const token = localStorage.getItem('NOCOBASE_TOKEN'); + if (token) { + headers['Authorization'] = `Bearer ${token}`; + } + await next(); +}); + console.log('process.env.API_URL', process.env.API_URL); export async function createOrUpdateCollection(data: any) { diff --git a/packages/client/src/schemas/kanban/style.less b/packages/client/src/schemas/kanban/style.less index 830116201..6b8e7f5e0 100644 --- a/packages/client/src/schemas/kanban/style.less +++ b/packages/client/src/schemas/kanban/style.less @@ -89,4 +89,5 @@ .nb-kanban-column-header { margin-bottom: 12px; line-height: 32px; + padding: 0 12px; }