feat: complete auth flow - email confirmation, login state, route guards

This commit is contained in:
2026-05-21 00:21:28 +08:00
parent c49ac7a42e
commit d4b07fba58
6 changed files with 258 additions and 78 deletions
+25 -4
View File
@@ -19,12 +19,24 @@ export const useAuthStore = defineStore('auth', () => {
.select('*')
.eq('id', user.value.id)
.single()
profile.value = data
if (data) {
profile.value = data as Profile
}
}
async function login(email: string, password: string) {
const { error } = await supabase.auth.signInWithPassword({ email, password })
if (error) throw error
const { data, error } = await supabase.auth.signInWithPassword({ email, password })
if (error) {
if (error.message.includes('Invalid login credentials')) {
throw new Error('邮箱或密码错误')
}
if (error.message.includes('Email not confirmed')) {
throw new Error('邮箱未确认,请先查收确认邮件')
}
throw error
}
user.value = data.user
await fetchProfile()
}
async function register(email: string, password: string, username: string) {
@@ -33,9 +45,18 @@ export const useAuthStore = defineStore('auth', () => {
password,
options: {
data: { username },
emailRedirectTo: `${window.location.origin}/auth/confirm`,
},
})
if (error) throw error
if (error) {
if (error.message.includes('already registered')) {
throw new Error('该邮箱已被注册')
}
if (error.message.includes('Password')) {
throw new Error('密码强度不足,请使用至少6位密码')
}
throw error
}
}
async function logout() {