|
|
|
@ -1,6 +1,5 @@ |
|
|
|
<script setup lang="ts"> |
|
|
|
import type { FormError, FormSubmitEvent } from '@nuxt/ui' |
|
|
|
import { request, unwrapApiBody, type ApiResponse } from '../../utils/http/factory' |
|
|
|
import { DEFAULT_AUTHENTICATED_LANDING_PATH, normalizeSafeRedirect } from '../../utils/auth-routes' |
|
|
|
import { useAuthSession } from '../../composables/useAuthSession' |
|
|
|
|
|
|
|
@ -34,6 +33,7 @@ const resultMessage = ref('') |
|
|
|
const route = useRoute() |
|
|
|
const { refresh } = useAuthSession() |
|
|
|
const { allowRegister } = useGlobalConfig() |
|
|
|
const { fetchData, getApiErrorMessage } = useClientApi() |
|
|
|
|
|
|
|
const validate = (formState: LoginFormState): FormError[] => { |
|
|
|
const errors: FormError[] = [] |
|
|
|
@ -59,13 +59,11 @@ const onSubmit = async (_event: FormSubmitEvent<LoginFormState>) => { |
|
|
|
loading.value = true |
|
|
|
|
|
|
|
try { |
|
|
|
const res = unwrapApiBody(await request<ApiResponse<LoginResult>>('/api/auth/login', { |
|
|
|
const res = await fetchData<LoginResult>('/api/auth/login', { |
|
|
|
method: 'POST', |
|
|
|
body: { |
|
|
|
username: state.username, |
|
|
|
password: state.password, |
|
|
|
}, |
|
|
|
})) |
|
|
|
body: { username: state.username, password: state.password }, |
|
|
|
notify: false, |
|
|
|
}) |
|
|
|
|
|
|
|
await refresh(true) |
|
|
|
|
|
|
|
@ -77,12 +75,8 @@ const onSubmit = async (_event: FormSubmitEvent<LoginFormState>) => { |
|
|
|
const redirectTarget = normalizeSafeRedirect(redirectCandidate, DEFAULT_AUTHENTICATED_LANDING_PATH) |
|
|
|
await navigateTo(redirectTarget) |
|
|
|
} catch (error: unknown) { |
|
|
|
const message = typeof error === 'object' && error !== null && 'statusMessage' in error |
|
|
|
? String(error.statusMessage) |
|
|
|
: '登录失败,请稍后重试' |
|
|
|
|
|
|
|
resultType.value = 'error' |
|
|
|
resultMessage.value = message |
|
|
|
resultMessage.value = getApiErrorMessage(error) |
|
|
|
} finally { |
|
|
|
loading.value = false |
|
|
|
} |
|
|
|
|