async function login() {
try {
const res = await $api('/auth/login', {
method: 'POST',
body: {
email: credentials.value.email,
password: credentials.value.password,
},
})
const { accessToken, userData, userAbilityRules } = res
useCookie('userAbilityRules').value = userAbilityRules
ability.update(userAbilityRules)
useCookie('userData').value = userData
useCookie('accessToken').value = accessToken
// Redirect to `to` query if exists, otherwise redirect to index route
await router.replace(route.query.to ? String(route.query.to) : '/dashboards/default')
}
catch (error) {
// Handle errors
console.error(error)
errors.value.apiError = 'Invalid email or password' // Set error message for invalid credentials
}
}