diff --git a/src/stores/user.ts b/src/stores/user.ts index 9173405..5cb9677 100644 --- a/src/stores/user.ts +++ b/src/stores/user.ts @@ -1,7 +1,5 @@ import { defineStore } from 'pinia' -import axios from 'axios' -import router from '@/router' - +import axios, {AxiosResponse, AxiosError} from 'axios' export const useUser = defineStore('userStore', { state: () => { @@ -13,23 +11,30 @@ export const useUser = defineStore('userStore', { }, actions: { - async login(username: string, password: string) { + async login(username: string, password: string) : Promise { - axios.post('http://localhost:3333/api/v1/login', { - username: username, - password: password - }).then((response) => { - console.log(response) + try { + const response: AxiosResponse<{ + user: string, + role: string, + Message: string + }> = await axios.post('http://localhost:3333/api/v1/login', { + username: username, + password: password + }) + + console.log(response.data) this.isLoggedIn = true - // this.user = response?.data?.user - // this.role = response?.data?.role - - router.push({name: 'Home'}) - }).catch((error) => { - console.log(error.response) - }) - + this.user = response.data.user + this.role = response.data.role + return true + } catch(err) { + if (axios.isAxiosError(err)){ + console.log(err.response) + } + return false + } } }