AI-News/frontend/app/composables/usePasswordReset.js
2025-12-04 10:04:21 +08:00

47 lines
1.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// app/composables/usePasswordReset.js
import { useApi } from './useApi'
import { useFlash } from './useFlash'
export function usePasswordReset() {
const api = useApi()
const flash = useFlash()
const sending = useState('pwreset:sending', () => false)
const resetting = useState('pwreset:resetting', () => false)
async function sendResetEmail(email) {
if (sending.value) return
sending.value = true
try {
await api.post('/auth/password/forgot', { email })
// 后端无论邮箱是否存在都返回 ok:true这里统一提示
flash.success('如果该邮箱存在,我们已发送重置邮件,请查收。')
} catch (e) {
flash.error(e?.data?.detail || e?.message || '发送失败')
} finally {
sending.value = false
}
}
async function submitNewPassword({ token, password, confirmPassword }) {
if (resetting.value) return
resetting.value = true
try {
await api.post('/auth/password/reset', {
token,
password,
confirm_password: confirmPassword,
})
flash.success('密码已重置,请使用新密码登录。')
return true
} catch (e) {
flash.error(e?.data?.detail || e?.message || '重置失败')
return false
} finally {
resetting.value = false
}
}
return { sending, resetting, sendResetEmail, submitNewPassword }
}