diff --git a/src/main.ts b/src/main.ts index 5cb1eb5..ed7eb62 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,6 +11,11 @@ app.use(pinia) app.use(router) const authStore = useAuthStore() -authStore.initialize().then(() => { + +if (window.location.pathname === '/auth/confirm') { app.mount('#app') -}) +} else { + authStore.initialize().then(() => { + app.mount('#app') + }) +} diff --git a/src/router/index.ts b/src/router/index.ts index 1d5247d..68b33bf 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -19,6 +19,11 @@ const router = createRouter({ name: 'register', component: () => import('@/views/auth/RegisterView.vue'), }, + { + path: '/auth/confirm', + name: 'auth-confirm', + component: () => import('@/views/auth/AuthConfirmView.vue'), + }, { path: '/upload', name: 'upload', diff --git a/src/stores/auth.ts b/src/stores/auth.ts index 94074b2..186a1ee 100644 --- a/src/stores/auth.ts +++ b/src/stores/auth.ts @@ -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() { diff --git a/src/views/auth/AuthConfirmView.vue b/src/views/auth/AuthConfirmView.vue new file mode 100644 index 0000000..13eef39 --- /dev/null +++ b/src/views/auth/AuthConfirmView.vue @@ -0,0 +1,106 @@ + + + diff --git a/src/views/auth/LoginView.vue b/src/views/auth/LoginView.vue index f09632a..2b30125 100644 --- a/src/views/auth/LoginView.vue +++ b/src/views/auth/LoginView.vue @@ -20,7 +20,7 @@ async function handleLogin() { const redirect = (route.query.redirect as string) || '/' router.push(redirect) } catch (e: unknown) { - error.value = e instanceof Error ? e.message : '登录失败' + error.value = e instanceof Error ? e.message : '登录失败,请稍后重试' } finally { loading.value = false } @@ -30,7 +30,11 @@ async function handleLogin() {