added typed response to login-api-call
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import axios from 'axios'
|
import axios, {AxiosResponse, AxiosError} from 'axios'
|
||||||
import router from '@/router'
|
|
||||||
|
|
||||||
|
|
||||||
export const useUser = defineStore('userStore', {
|
export const useUser = defineStore('userStore', {
|
||||||
state: () => {
|
state: () => {
|
||||||
@@ -13,23 +11,30 @@ export const useUser = defineStore('userStore', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
async login(username: string, password: string) {
|
async login(username: string, password: string) : Promise<boolean> {
|
||||||
|
|
||||||
axios.post('http://localhost:3333/api/v1/login', {
|
try {
|
||||||
username: username,
|
const response: AxiosResponse<{
|
||||||
password: password
|
user: string,
|
||||||
}).then((response) => {
|
role: string,
|
||||||
console.log(response)
|
Message: string
|
||||||
|
}> = await axios.post('http://localhost:3333/api/v1/login', {
|
||||||
|
username: username,
|
||||||
|
password: password
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(response.data)
|
||||||
this.isLoggedIn = true
|
this.isLoggedIn = true
|
||||||
// this.user = response?.data?.user
|
this.user = response.data.user
|
||||||
// this.role = response?.data?.role
|
this.role = response.data.role
|
||||||
|
|
||||||
router.push({name: 'Home'})
|
|
||||||
}).catch((error) => {
|
|
||||||
console.log(error.response)
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
|
return true
|
||||||
|
} catch(err) {
|
||||||
|
if (axios.isAxiosError(err)){
|
||||||
|
console.log(err.response)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user