added typed response to login-api-call

This commit is contained in:
Sockenklaus
2021-10-20 10:45:27 +02:00
parent 60618a5eee
commit 5017be6d42

View File

@@ -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<boolean> {
axios.post('http://localhost:3333/api/v1/login', {
try {
const response: AxiosResponse<{
user: string,
role: string,
Message: string
}> = await axios.post('http://localhost:3333/api/v1/login', {
username: username,
password: password
}).then((response) => {
console.log(response)
this.isLoggedIn = true
// this.user = response?.data?.user
// this.role = response?.data?.role
router.push({name: 'Home'})
}).catch((error) => {
console.log(error.response)
})
console.log(response.data)
this.isLoggedIn = true
this.user = response.data.user
this.role = response.data.role
return true
} catch(err) {
if (axios.isAxiosError(err)){
console.log(err.response)
}
return false
}
}
}