fix: support production platform login response
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -4,6 +4,35 @@ import { useState } from 'react'
|
||||
import { request } from '../../lib/request'
|
||||
import { setPlatformToken } from '../../lib/auth'
|
||||
|
||||
function extractLoginToken(payload: unknown): string | null {
|
||||
if (!payload || typeof payload !== 'object') {
|
||||
return null
|
||||
}
|
||||
|
||||
const directToken =
|
||||
'token' in payload && typeof payload.token === 'string'
|
||||
? payload.token
|
||||
: null
|
||||
|
||||
if (directToken) {
|
||||
return directToken
|
||||
}
|
||||
|
||||
if (
|
||||
!('code' in payload) ||
|
||||
payload.code !== 0 ||
|
||||
!('data' in payload) ||
|
||||
!payload.data ||
|
||||
typeof payload.data !== 'object'
|
||||
) {
|
||||
return null
|
||||
}
|
||||
|
||||
return 'token' in payload.data && typeof payload.data.token === 'string'
|
||||
? payload.data.token
|
||||
: null
|
||||
}
|
||||
|
||||
export default function LoginPage() {
|
||||
const navigate = useNavigate()
|
||||
const [loading, setLoading] = useState(false)
|
||||
@@ -16,8 +45,10 @@ export default function LoginPage() {
|
||||
body: JSON.stringify(values),
|
||||
})
|
||||
|
||||
if (result.code === 0 && result.data) {
|
||||
setPlatformToken(result.data.token)
|
||||
const token = extractLoginToken(result)
|
||||
|
||||
if (token) {
|
||||
setPlatformToken(token)
|
||||
message.success('登录成功')
|
||||
navigate('/projects')
|
||||
} else {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { BrowserRouter } from 'react-router-dom'
|
||||
import LoginPage from '../features/auth/LoginPage'
|
||||
import { request } from '../lib/request'
|
||||
|
||||
// Mock the request module
|
||||
vi.mock('../lib/request', () => ({
|
||||
@@ -46,4 +47,31 @@ describe('LoginPage', () => {
|
||||
expect(mockNavigate).toHaveBeenCalledWith('/projects')
|
||||
})
|
||||
})
|
||||
|
||||
it('accepts the production login payload shape with direct token and user', async () => {
|
||||
vi.mocked(request).mockResolvedValueOnce({
|
||||
token: 'production-token',
|
||||
user: {
|
||||
id: 1,
|
||||
username: '13513801736',
|
||||
role: 'platform_admin',
|
||||
},
|
||||
} as never)
|
||||
|
||||
const user = userEvent.setup()
|
||||
render(
|
||||
<BrowserRouter>
|
||||
<LoginPage />
|
||||
</BrowserRouter>
|
||||
)
|
||||
|
||||
await user.type(screen.getByLabelText('用户名'), '13513801736')
|
||||
await user.type(screen.getByLabelText('密码'), 'Jx88Zhang')
|
||||
await user.click(screen.getByRole('button', { name: '登录后台' }))
|
||||
|
||||
await waitFor(() => {
|
||||
expect(localStorage.getItem('platform_token')).toBe('production-token')
|
||||
expect(mockNavigate).toHaveBeenCalledWith('/projects')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user