41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import axios, {AxiosResponse, AxiosError} from 'axios'
|
|
|
|
export const useUser = defineStore('userStore', {
|
|
state: () => {
|
|
return {
|
|
user: '',
|
|
role: '',
|
|
isLoggedIn: false
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
async login(username: string, password: string) : Promise<boolean> {
|
|
|
|
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
|
|
|
|
return true
|
|
} catch(err) {
|
|
if (axios.isAxiosError(err)){
|
|
console.log(err.response)
|
|
}
|
|
return false
|
|
}
|
|
|
|
}
|
|
}
|
|
}) |