added typed response to login-api-call
This commit is contained in:
@@ -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)
|
||||
})
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user