feat(auth): support local username login mode

This commit is contained in:
mac
2026-07-16 16:19:25 +08:00
parent d5c7cab8bb
commit cafa8bc840
10 changed files with 304 additions and 19 deletions
+42 -3
View File
@@ -3,7 +3,7 @@ import { getToken } from '@/utils/auth'
export interface LoginRequest {
username: string
password: string
password?: string
}
export interface UserInfo {
@@ -20,9 +20,48 @@ export interface LoginResponse {
user: UserInfo
}
export interface AuthConfig {
sso_enabled: boolean
}
export const authApi = {
login(_data: LoginRequest): Promise<ApiResponse<LoginResponse>> {
return Promise.reject(new Error('password login is disabled'))
async getConfig(): Promise<ApiResponse<AuthConfig>> {
const response = await fetch('/auth/api/v1/config', {
headers: {
Accept: 'application/json',
},
})
const data = await response.json().catch(() => ({}))
if (!response.ok) {
throw new Error(data.error || `HTTP ${response.status}`)
}
return {
code: 0,
message: 'success',
data: {
sso_enabled: data.sso_enabled !== false,
},
}
},
async login(data: LoginRequest): Promise<ApiResponse<LoginResponse>> {
const response = await fetch('/auth/api/v1/login', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ username: data.username }),
})
const result = await response.json().catch(() => ({}))
if (!response.ok) {
throw new Error(result.error || `HTTP ${response.status}`)
}
return {
code: 0,
message: 'success',
data: result,
}
},
async logout(): Promise<ApiResponse<null>> {
+10 -1
View File
@@ -61,7 +61,16 @@ request.interceptors.response.use(
if (status === 401) {
removeToken()
redirectToSSO()
fetch('/auth/api/v1/config', { headers: { Accept: 'application/json' } })
.then((response) => response.json())
.then((data) => {
if (data.sso_enabled === false) {
window.location.assign('/login')
} else {
redirectToSSO()
}
})
.catch(() => redirectToSSO())
ElMessage.error(backendMessage || '未授权,请重新登录')
} else {
const message = backendMessage || httpMessageMap[status] || `请求失败 (${status})`