Add Vue frontend application
This commit is contained in:
44
frontend/src/stores/auth.ts
Normal file
44
frontend/src/stores/auth.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import api from '@/api'
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const token = ref<string | null>(localStorage.getItem('access_token'))
|
||||
const user = ref<{ id: string; email: string; full_name?: string } | null>(null)
|
||||
|
||||
const isAuthenticated = computed(() => !!token.value)
|
||||
|
||||
async function login(email: string, password: string) {
|
||||
const formData = new FormData()
|
||||
formData.append('username', email)
|
||||
formData.append('password', password)
|
||||
const response = await api.post('/api/auth/login', formData, {
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
})
|
||||
token.value = response.data.access_token
|
||||
localStorage.setItem('access_token', response.data.access_token)
|
||||
await fetchUser()
|
||||
}
|
||||
|
||||
async function fetchUser() {
|
||||
if (!token.value) return
|
||||
try {
|
||||
const response = await api.get('/api/auth/me')
|
||||
user.value = response.data
|
||||
} catch {
|
||||
logout()
|
||||
}
|
||||
}
|
||||
|
||||
function logout() {
|
||||
token.value = null
|
||||
user.value = null
|
||||
localStorage.removeItem('access_token')
|
||||
}
|
||||
|
||||
if (token.value) {
|
||||
fetchUser()
|
||||
}
|
||||
|
||||
return { token, user, isAuthenticated, login, logout, fetchUser }
|
||||
})
|
||||
47
frontend/src/stores/conversation.ts
Normal file
47
frontend/src/stores/conversation.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import type { Message, Conversation } from '@/api/conversation'
|
||||
|
||||
export const useConversationStore = defineStore('conversation', () => {
|
||||
const conversations = ref<Conversation[]>([])
|
||||
const currentConversationId = ref<string | null>(null)
|
||||
const messages = ref<Message[]>([])
|
||||
const isLoading = ref(false)
|
||||
|
||||
function setConversations(data: Conversation[]) {
|
||||
conversations.value = data
|
||||
}
|
||||
|
||||
function setCurrentConversation(id: string) {
|
||||
currentConversationId.value = id
|
||||
}
|
||||
|
||||
function addMessage(msg: Message) {
|
||||
messages.value.push(msg)
|
||||
}
|
||||
|
||||
function setMessages(data: Message[]) {
|
||||
messages.value = data
|
||||
}
|
||||
|
||||
function addConversation(conv: Conversation) {
|
||||
conversations.value.unshift(conv)
|
||||
}
|
||||
|
||||
function removeConversation(id: string) {
|
||||
conversations.value = conversations.value.filter((c) => c.id !== id)
|
||||
}
|
||||
|
||||
return {
|
||||
conversations,
|
||||
currentConversationId,
|
||||
messages,
|
||||
isLoading,
|
||||
setConversations,
|
||||
setCurrentConversation,
|
||||
addMessage,
|
||||
setMessages,
|
||||
addConversation,
|
||||
removeConversation,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user