feat: 前端认证和工具模块
- 新增 useAuth.ts composable - 新增 tools/useTools.ts - 更新 Login.vue, Signup.vue Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { useRouter, useRoute } from 'vue-router'
|
||||
import { useAuth } from '@/composables/useAuth'
|
||||
|
||||
const router = useRouter()
|
||||
const route = useRoute()
|
||||
const { login } = useAuth()
|
||||
|
||||
// 表单数据
|
||||
const username = ref('')
|
||||
@@ -11,23 +14,38 @@ const rememberMe = ref(false)
|
||||
const isLoading = ref(false)
|
||||
const showPassword = ref(false)
|
||||
const errorMsg = ref('')
|
||||
const successMsg = ref('')
|
||||
|
||||
// 模拟登录验证
|
||||
const handleLogin = () => {
|
||||
// 检查是否从注册页跳转过来
|
||||
onMounted(() => {
|
||||
if (route.query.registered === 'true') {
|
||||
successMsg.value = 'Account created successfully! Please sign in.'
|
||||
}
|
||||
})
|
||||
|
||||
// 登录处理
|
||||
const handleLogin = async () => {
|
||||
errorMsg.value = ''
|
||||
|
||||
if (username.value !== 'admin' || password.value !== 'admin') {
|
||||
errorMsg.value = 'Invalid username or password'
|
||||
if (!username.value || !password.value) {
|
||||
errorMsg.value = 'Please enter username and password'
|
||||
return
|
||||
}
|
||||
|
||||
isLoading.value = true
|
||||
// 模拟登录
|
||||
setTimeout(() => {
|
||||
isLoading.value = false
|
||||
|
||||
try {
|
||||
await login({
|
||||
username: username.value,
|
||||
password: password.value,
|
||||
})
|
||||
// 登录成功,跳转到 Dashboard
|
||||
router.push('/dashboard')
|
||||
}, 1500)
|
||||
router.push('/chat')
|
||||
} catch (error: any) {
|
||||
errorMsg.value = error.message || 'Login failed, please check your credentials'
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -52,6 +70,12 @@ const handleLogin = () => {
|
||||
|
||||
<!-- 登录表单 -->
|
||||
<div class="bg-dark-700 rounded-2xl p-8 shadow-xl border border-dark-500/50">
|
||||
<!-- 成功提示 -->
|
||||
<div v-if="successMsg" class="mb-4 p-3 bg-green-500/10 border border-green-500/30 rounded-lg text-green-400 text-sm flex items-center gap-2">
|
||||
<i class="fa-solid fa-check-circle"></i>
|
||||
{{ successMsg }}
|
||||
</div>
|
||||
|
||||
<!-- 错误提示 -->
|
||||
<div v-if="errorMsg" class="mb-4 p-3 bg-red-500/10 border border-red-500/30 rounded-lg text-red-400 text-sm flex items-center gap-2">
|
||||
<i class="fa-solid fa-circle-exclamation"></i>
|
||||
|
||||
Reference in New Issue
Block a user