feat: scaffold infrastructure - router, pinia, tailwind, amap, auth store, page placeholders
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import { supabase } from '@/lib/supabase'
|
||||
import type { User } from '@supabase/supabase-js'
|
||||
import type { Profile } from '@/types/database'
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const user = ref<User | null>(null)
|
||||
const profile = ref<Profile | null>(null)
|
||||
const loading = ref(true)
|
||||
|
||||
const isLoggedIn = computed(() => !!user.value)
|
||||
const isAdmin = computed(() => profile.value?.role === 'admin')
|
||||
|
||||
async function fetchProfile() {
|
||||
if (!user.value) return
|
||||
const { data } = await supabase
|
||||
.from('profiles')
|
||||
.select('*')
|
||||
.eq('id', user.value.id)
|
||||
.single()
|
||||
profile.value = data
|
||||
}
|
||||
|
||||
async function login(email: string, password: string) {
|
||||
const { error } = await supabase.auth.signInWithPassword({ email, password })
|
||||
if (error) throw error
|
||||
}
|
||||
|
||||
async function register(email: string, password: string, username: string) {
|
||||
const { error } = await supabase.auth.signUp({
|
||||
email,
|
||||
password,
|
||||
options: {
|
||||
data: { username },
|
||||
},
|
||||
})
|
||||
if (error) throw error
|
||||
}
|
||||
|
||||
async function logout() {
|
||||
const { error } = await supabase.auth.signOut()
|
||||
if (error) throw error
|
||||
user.value = null
|
||||
profile.value = null
|
||||
}
|
||||
|
||||
async function initialize() {
|
||||
const { data: { session } } = await supabase.auth.getSession()
|
||||
user.value = session?.user ?? null
|
||||
if (user.value) await fetchProfile()
|
||||
loading.value = false
|
||||
|
||||
supabase.auth.onAuthStateChange((_event, session) => {
|
||||
user.value = session?.user ?? null
|
||||
if (user.value) fetchProfile()
|
||||
else profile.value = null
|
||||
})
|
||||
}
|
||||
|
||||
return { user, profile, loading, isLoggedIn, isAdmin, login, register, logout, initialize }
|
||||
})
|
||||
Reference in New Issue
Block a user